# Thread: Matlab programming error help

1. ## Matlab programming error help

Hi Guys
I am trying to find a problem with this program.

This is the question I was given:

Using the MATLAB function integ (below) write an m–file to calculate the first 50 pairs of coefficients An and Bn which express a saw-tooth wave as a Fourier Series. Your code should define one period of the saw-tooth wave x(t) and call the function integ to perform the appropriate integration.
function [area] = integ(x,t)
% integ is a function which calculates an approximation
% to the integral of x(t) between the limits t(1) (the
% first value of the array t) and t(end) (the last value
% of the array t). It is assumed that the points in t are
% equally spaced a distance dt apart.
area=0;
dt=t(2) – t(1);
for k=1:length(x)-1,
area = area + (x(k)+x(k+1))*dt/2
end;

This is what I have written:

Code for 50 coefficients:
dt =0.0125
t = [-0.5:dt:0.5];
s=t;
y=0;
T = 1;
f = 1/T;
for n = 1:1:50
x= cos(2*pi*n*f*t);
xa=s.*x;
a(n)=2*integ(xa,t);
z= sin(2*pi*n*f*t);
xb=s.*z;
b(n)=2*integ(xb,t);
end;
for k= 1:1:50
y=y + a(k)*(cos(2*pi*k*f*t))+ b(k)*(sin(2*pi*k*f*t));
end;
plot (t,y);

Its giving an error when you run it.

Can someone help me to fix this please?

Thankss

2. Originally Posted by killer6008
Hi Guys
I am trying to find a problem with this program.

This is the question I was given:

Using the MATLAB function integ (below) write an m–file to calculate the first 50 pairs of coefficients An and Bn which express a saw-tooth wave as a Fourier Series. Your code should define one period of the saw-tooth wave x(t) and call the function integ to perform the appropriate integration.
function [area] = integ(x,t)
% integ is a function which calculates an approximation
% to the integral of x(t) between the limits t(1) (the
% first value of the array t) and t(end) (the last value
% of the array t). It is assumed that the points in t are
% equally spaced a distance dt apart.
area=0;
dt=t(2) – t(1);
for k=1:length(x)-1,
area = area + (x(k)+x(k+1))*dt/2
end;

This is what I have written:

Code for 50 coefficients:
dt =0.0125
t = [-0.5:dt:0.5];
s=t;
y=0;
T = 1;
f = 1/T;
for n = 1:1:50
x= cos(2*pi*n*f*t);
xa=s.*x;
a(n)=2*integ(xa,t);
z= sin(2*pi*n*f*t);
xb=s.*z;
b(n)=2*integ(xb,t);
end;
for k= 1:1:50
y=y + a(k)*(cos(2*pi*k*f*t))+ b(k)*(sin(2*pi*k*f*t));
end;
plot (t,y);

Its giving an error when you run it.

Can someone help me to fix this please?

Thankss
What error message?

CB

3. ??? Undefined function or method 'integ' for input arguments of type 'double'.

Error in ==> untitled3 at 10
a(n)=2*integ(xa,t);

Weird thing is one of my friend has got 2009a matlab student version and it works fine for him and it gives me this error with 2009b full version

4. Originally Posted by killer6008
??? Undefined function or method 'integ' for input arguments of type 'double'.

Error in ==> untitled3 at 10
a(n)=2*integ(xa,t);

Weird thing is one of my friend has got 2009a matlab student version and it works fine for him and it gives me this error with 2009b full version
Is integ in a file called integ.m and on the Matlab path?

For the purposes of this problem there is no significant difference between 2009a and 2009b (and in general the student version is more complete than the "full" version as you do not have to licence every tool box you need for an extra £600 a whack)

CB

5. I don't know about that file do I need to do something with it??

All I do is make a new m file paste this coding in there and it gives me an error. Integ is supposed to be an integration function isnt it?

Thats the same thing my friend does and it works fine for him. It doens't make sense.

6. Originally Posted by killer6008
I don't know about that file do I need to do something with it??

All I do is make a new m file paste this coding in there and it gives me an error. Integ is supposed to be an integration function isnt it?

Thats the same thing my friend does and it works fine for him. It doens't make sense.
Where is integ.m, that is what directory is it in?

CB

7. what is integ.m?? is that another file? because there is no other file?

and if you are talking about same file its not saved with integ.m filename

8. Originally Posted by killer6008
what is integ.m?? is that another file? because there is no other file?

and if you are talking about same file its not saved with integ.m filename
So do you have a single file containing both the function integ and the script?

CB