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)
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

Answers (1)

Riya
Riya on 6 Feb 2025
Hi Sam,
I understand that you are encountering error while plotting the sub plots for partial sums of a piecewise function. The main reason for this error is the use of symbolic expressions in “orig” without proper conversion.
The “plot” function in MATLAB accepts only numeric values and not symbolic expressions meaning, it only works with numeric values.
The problem occurs from the line:
orig=orig+(an*cos(n*pi*x/p)+bn*sin(n*pi*x/p));
This contains variable “x” , “an” and “bn” which are calculated using “x”. So, “orig” is not an array of numeric values.
To fix the issue,
First, use “double” function to convert “an” and “bn” to numeric values.
an = double((1/p) * (int(f1 * cos(n * pi * x / p), x, -1, 0) + int(f2 * cos(n * pi * x / p), x, 0, 1)));
bn = double((1/p) * (int(f1 * sin(n * pi * x / p), x, -1, 0) + int(f2 * sin(n * pi * x / p), x, 0, 1)));
Secondly, use numeric “t” instead of “x” in the fourier series calculation:
orig = orig + an * cos(n * pi * t / p) + bn * sin(n * pi * t / p);
Here is the output plots for the updated code:
For more information about input arguments for the “plot” function in MATLAB, please refer to the following document:
Thanks!

Categories

Find more on Fractals 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!