How to plot partial sums of fourier series?

49 views (last 30 days)
I am trying to plot a partial sum n=1,2,3 and 10 using for loop function and then plot all the partial sums in one graph.
Below is the code I wrote but somehow it keep giving me error at n==2.
Also how do i plot them in one graph?
x=-pi:0.01:pi;
sum=0;
for n=1:10
sum=sum+(pi+(-2/n*cos(n*pi)*sin(n*t)));
if n==1
plot(x,sum)
Elseif n==2
plot(t,sum)
Elseif n==3
plot(t,sum)
Elseif n==10
plot(t,sum)
end
end

Accepted Answer

Image Analyst
Image Analyst on 2 Dec 2020
Not exactly sure what formula you want to plot but I think it would be more like this:
clc; % Clear the command window.
fprintf('Beginning to run %s.m.\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
clear global;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
x = -pi : 0.01 : pi;
theSum = zeros(1, length(x));
for n = 1 : 10
theSum = theSum + (pi + (-2./n*cos(n*pi*x) .* sin(n*x)));
subplot(10, 1, n);
plot(x, theSum, 'b.-', 'LineWidth', 2)
grid on;
drawnow;
caption = sprintf('n = %d', n);
title(caption, 'FontSize', 15);
end
g = gcf;
g.WindowState = 'maximized';
fprintf('Done running %s.m.\n', mfilename);

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!