Filter löschen
Filter löschen

How to plot partial sums of fourier series?

16 Ansichten (letzte 30 Tage)
Teb Keb
Teb Keb am 2 Dez. 2020
Beantwortet: Image Analyst am 2 Dez. 2020
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

Akzeptierte Antwort

Image Analyst
Image Analyst am 2 Dez. 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);

Weitere Antworten (0)

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by