curves are incorrect. Fourier Series.

1 view (last 30 days)
Nabhdeep Bansal
Nabhdeep Bansal on 23 Aug 2014
Answered: Rick Rosson on 24 Aug 2014
I WANT TO PLOT A FOURIER SERIES FOR DIFFERENT HARMONICS AS PER THE Y~t RELATION DEFINED BELOW. (TIME PERIOD=2 SECS. Y=1 for first 0.8 secs and Y=-1 for next 1.2 secs).
THE CURVES ARE NOT CORRECT,I GUESS.
t=[-1,0,0,0.8,0.8,1]';
Y=[-1,-1,1,1,-1,-1];
f=0;
for n=1:20:40
line(t,Y,'color','r','linewidth',2)
grid on;
hold on;
A=0.5*trapz(t,Y);
P=Y.*(cos(n*pi*t))';
Q=Y.*(sin(n*pi*t))';
Ax=trapz(t,P);
Bx=trapz(t,Q);
f=f+(Ax*cos(pi*n*t)+Bx*sin(pi*n*t));
final=A+f;
plot(t,final,'linewidth',2)
end
  1 Comment
Image Analyst
Image Analyst on 23 Aug 2014
and these:
"THE CURVES ARE NOT CORRECT,I GUESS." is not very descriptive. The code (after I fixed the formatting) did run and produce a plot. What would correct curves look like? Come on, make it easy for us to help you, not hard. Post a plot of what the curve should look like. If you can't use the debugger to figure out what's going wrong in this alphabet soup of code, then put in some comments, give the variables descriptive names to help up, and come back with any questions about your lastest, improved code.

Sign in to comment.

Answers (1)

Rick Rosson
Rick Rosson on 24 Aug 2014
I would recommend using the fft function.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!