I need to plot the first six partial sums of a fourier series. returning "error using plot" when I try
6 views (last 30 days)
Show older comments
I am trying to calculate the first six partial sums of a piecewise function using a fourier series and plot their approximations on six separate sub plots. I am almost there but now when I try to plot the partial on the interval I previously defined I am returning the errors:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
Error in q1t (line 34)
subplot (3, 2, n), plot(t,orig, t, f, '--')
my code is also included. any help is much appreciated
clc
clear
syms x;
f1=(1+x);
f2=1;
p=1;
orig=0;
a0=(1/p)*(int(f1,x,-1,0)+int(f2,x,0,1));
N=6;
for n=1:N
an=(1/p)*(int(f1*cos(n*pi*x/p),x,-1,0)+int(f2*cos(n*pi*x/p),x,0,1));
bn=(1/p)*(int(f1*sin(n*pi*x/p),x,-1,0)+int(f2*sin(n*pi*x/p),x,0,1));
orig=orig+(an*cos(n*pi*x/p)+bn*sin(n*pi*x/p));
end
t=linspace(-1,1); % create time points in plot
f= zeros (size(t)); % initialize function f (t)
for k = 1:length(t) % construct function f (t)
if t(k)<=0; f(k)=t(k)+1;end;
if t(k)>0; f(k) = 1;end;
end
% initialize fourier series with the mean term
orig = (a0/2)*ones(size(t));
clf % clear any figures
w=(int(f1*cos(n*pi*x/p),x,-1,0));
q=(int(f1*cos(n*pi*x/p),x,-1,0));
e=(int(f1*sin(n*pi*x/p),x,-1,0));
r=(int(f2*sin(n*pi*x/p),x,0,1));
for n = 1:6
% create plot of truncated FS with only n harmonic
an=(1/p)*(w+q);
bn=(1/p)*(e+r);
orig=orig+(an*cos(n*pi*x/p)+bn*sin(n*pi*x/p));
subplot (3, 2, n), plot(t,orig, t, f, '--')
if n==1
legend ('mean plus 1 term','f (t)'); legend boxoff;
else
legend(['mean plus ',num2str(n),'terms'],'f(t)')
legend boxoff
end
if n >= 5; xlabel('t'); end;
end
0 Comments
Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!