How to plot partial sums of fourier series?
12 次查看(过去 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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!