# MATLAB program error help

Discussion in 'Programmer's Corner' started by killer6008, Feb 1, 2010.

1. ### killer6008 Thread Starter New Member

Jan 26, 2010
20
0
Helllo

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? This is all the code there is to it.

Thankss

2. ### guitarguy12387 Active Member

Apr 10, 2008
359
12
Well it would help to know what error it is giving you.