How to plot partial sums of fourier series?

16 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2020-12-2
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by