×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

problem with script

problem with script

problem with script

(OP)
i have a project and every time i run it it gives me this "??? Error: File: C:\MATLAB7\work\square.m Line: 15 Column: 26
Missing variable or function." i have spent many days and i still can't figure it out. there are three parts to the script the main (square.m), the function (fsum), and the given material (pwrspec.m). the problem doesn't make sense, at least to a novice like me. here is the all of the script:

square.m

clf
%trying to find a square wave
time = [0:0.01:10];
T=10;
omega=2*pi*1/T;
fsummation=0
k=1;
gc = ['b','c', 'm', 'k', 'y','g', 'r', 'b'];

for ndx=[1:2:19],
farray(k,:) = fsum(time, omega, ndx);
subplot(10,2,ndx)
    for m=1:k,
    hold on
    gc(mod(k,8)+1)
    plot(time,farray(m,:)’gc')
    end
fsummation = fsummation + farray(k,:)
subplot(10,2,ndx+1)
plot(time, fsummation’r’)
k=k+1;
end
gtext('Archie Brentano SN')

figure
pwrspec(fsummation, 1000,100)
gtext(‘other name’)

fsum.m

function fsum(time, omega, ndx)
%fsummation
fsum = 4/pi*1/ndx * sin(ndx*omega*time)

pwrspec.m

function pwrspec(signal, N, srate)
% pwrspec(signal, N, srate)
% pwrspec computes the power spectrum of a signal and creates a plot
% signal = vector containing signal values
% N = desired length of vector, normally obtained from "length()" function
% srate = sampling rate used to calculate signal, typically twice the value of
%         highest frequency

P = fft(signal,N);
n = length(P);
plen = floor(n/2);
power = abs(P(1:plen)).^2;
nyquist = 1/2 * srate;
freq = (1:plen)/(plen)*nyquist;

stem(freq,power)
title('Power spectrum of signal stream')
xlabel('frequency (Hz)')
ylabel('signal intensity')
grid

RE: problem with script

Looks like it doesn't like your plot(time,farray(m,:)’gc')

I'd guess a missing comma.

Cheers

Greg Locock

RE: problem with script

(OP)
putting the comma in, just changes the error say: "??? Error: File: C:\MATLAB7\work\square.m Line: 16 Column: 27
Missing variable or function." so that doesn't seem to change the problem all that much

square.m

clf
%trying to find a square wave
time = [0:0.01:10];
T=10;
omega=2*pi*1/T;
fsummation=0
k=1;
gc = ['b','c', 'm', 'k', 'y','g', 'r', 'b'];

for ndx=[1:2:19],
farray(k,:) = fsum(time, omega, ndx);
subplot(10,2,ndx)
    for m=1:k,
    hold on
    gc(mod(k,8)+1)
    plot(time,farray(m,:),’gc')
    end
fsummation = fsummation + farray(k,:)
subplot(10,2,ndx+1)
plot(time, fsummation’r’)
k=k+1;
end
gtext('Archie Brentano SN')

figure
pwrspec(fsummation, 1000,100)
gtext(‘other name’)

RE: problem with script

Well, the advantage of a modular language is that you can go into debug mode. Write some routines that test each subroutine by itself. Comment out the parts of the code that are not essential to its function, such as plot...

Incidentally I can't say I like the look of gc(mod...

either but that may be you doing something fancy to an array of strings, not the sort of thing I do.


Cheers

Greg Locock

RE: problem with script

There are some errors spread out through your code

  1. You use instead of ' several times.
  2. The fsum function has a VB-like syntax that doesn't work in matlab.

    CODE

    function out = fsum(time, omega, ndx)
    %fsummation
    out = 4/pi*1/ndx * sin(ndx*omega*time);
    is much better.
  3. As Greg said, gc(mod(k,8)+1) is wrong. Something like this is better.

    CODE

    color=gc(mod(k,8)+1);
    plot(time,farray(m,:),color)
The pwrspec.m doesn't seem to have any errors.

This was what I found when giving it a quick look, there might be more problems.

RE: problem with script

(OP)
well i finally got it to work. here is the code if anyone wants to know:

square.m

clf
%trying to find a square wave
time = [0:0.01:10];%gives the increments of measuring for time
T=10;%the amount of time it takes to run through the run
omega=2*pi*1/T;
fsummation=0;%starting parameter for the right graph
k=1;%starting inner and outer variable
gc = [ 'k', 'b', 'g', 'r', 'c', 'm', 'y'];%color array
gh = 1;%labels the title of Square wave
gp = 1;%labels the title of partial series
u = [0, 10, -1.25, 1.25]%limits the axis of the graphs
pr = [0, 6, 0, 60000]
for ndx=[1:2:19],%this is the index which tells the script to run on the odd numbers
    farray(k,:) = fsum(time, omega, ndx);
    subplot(10,2,ndx);
     
    for m=1:k;   
        color=gc(mod(m,7)+1);
        plot(time, farray(m,:), color);
        hold on

        if (gp==1)
            title('Partial series: 1,3,5,7,9,11,13,15,17,19')
        end
        gp = gp + 1
    
  
    axis(u)
   
        if (ndx==19)
            xlabel('sec')
        end
   
   grid on
   end
   
fsummation = fsummation + farray(k,:);
subplot(10,2,ndx+1);
plot(time, fsummation, 'r');
k=k+1;
    axis(u)
   if (ndx==19)
   
       
    xlabel('sec')
   end
    grid on
   
   if (gh == 1)
   title('Square wave synthesis')
   end
   gh = gh + 1
    
end
gtext('Archie Brentano SN: 930-584-308 ECE111 Fall 2004 ')



figure
pwrspec(fsummation, 1000,100)
axis(pr);
gtext('Archie Brentano SN: 930584-308 ECE111 Fall 2004')

fsum.m

function out = fsum(time, omega, ndx)
%fsummation
%time is a time-axis vector
%rfreq is the radian frequency
%idx is the square wave synthesis degree index
%fsum will calculate a term of a series of sine waves that will approach a square wave
out = 4/pi*1/ndx * sin(ndx*omega*time)

pwrspec.m

function pwrspec(signal, N, srate)
% pwrspec(signal, N, srate)
% pwrspec computes the power spectrum of a signal and creates a plot
% signal = vector containing signal values
% N = desired length of vector, normally obtained from "length()" function
% srate = sampling rate used to calculate signal, typically twice the value of
%         highest frequency

P = fft(signal,N);
n = length(P);
plen = floor(n/2);
power = abs(P(1:plen)).^2;
nyquist = 1/2 * srate;
freq = (1:plen)/(plen)*nyquist;

stem(freq,power)
title('Power spectrum of signal stream')
xlabel('frequency (Hz)')
ylabel('signal intensity')
grid

thank you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now