필터 지우기
필터 지우기

How to plot partial sums of fourier series?

조회 수: 16 (최근 30일)
Teb Keb
Teb Keb 2020년 12월 2일
답변: Image Analyst 2020년 12월 2일
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