partial sum of a series
3 views (last 30 days)
i have this function and i wrote the following code to calculate the Fourier coefficients
im just stuck with writing a loop which calculate the partial sum of the series till a given number N as well as Plotting the function and the corresponding partial sum (N = 12) on one figure.
can any one help?
Paul on 6 May 2022
Edited: Paul on 7 May 2022
To plot the function, we need a set of x-values to plot over.
As you've done, define the function and the CFS coefficients symbolically
syms n integer positive % missing from original code!
f = 1/2*(sin(x)+abs(sin(x)));
% use sym(pi) when doing symbolic math
Pi = sym(pi);
a0_sym = (1/Pi)*int(f,x,-Pi,Pi); % this equation has been corrected
a_sym(n) = (1/Pi)*int(f*cos(n*x),x,-Pi,Pi)
b_sym(n) = (1/Pi)*int(f*sin(n*x),x,-Pi,Pi)
Now define the values of x to make the plot
xvals = -pi:.01:pi;
Now we loop over the CFS coefficients to sum them all up over all values of x
N = 12;
% intialize with a0
fr = double(a0_sym)/2;
for n = 1:N;
a = double(a_sym(n));
b = double(b_sym(n));
% at this point, fill in the RHS
fr = fr + ...
plot(xvals,1/2*(sin(xvals) + abs(sin(xvals))),xvals,fr)