Trying to plot Fourier series, but just getting a straight line??

5 visualizaciones (últimos 30 días)
Hi, i am trying to plot the a fourier series
i have the code below but all i seem to be getting is a straight line? i cannot for the life of me see what is wrong? i should be expecting something like :-
but sll i get is
Could anyone shed any light on this issue please, the code below is run with .......FourierSq(1 , 0.25 , 6)
ie a0=1 , T=0.25 and n=6
the code is meant to plot the first 6 terms of fourier series and the resulting sum of them also on the same plot
function [t,f] = FourierSq(A0,T,n)
t = 0 : T/256 : 4*T;
nn = length(t);
f = zeros(n,nn);
s = zeros(nn);
for ii = 1:n
for j = 1:nn
f(ii) = f(ii,j) + (4*A0/(2*ii-1)*pi) * sin(2*pi*(ii-1)*t(j));
s(j) = s(j) + f(ii,j);
end
end
hold on
for ii = 1:n
plot (t,f(ii,:),'r:')
end
plot(t,s,'k-','linewidth',2)
hold off
end

Respuesta aceptada

Alan Stevens
Alan Stevens el 28 de Nov. de 2020
Editada: Alan Stevens el 28 de Nov. de 2020
Replace
f(ii) = f(ii,j) + (4*A0/(2*ii-1)*pi) * sin(2*pi*(ii-1)*t(j));
with
f(ii,j) = f(ii,j) + 4*A0/((2*ii-1)*pi) * sin(2*pi*(2*ii-1)*t(j)/T);
  2 comentarios
Alan Stevens
Alan Stevens el 28 de Nov. de 2020
Editada: Alan Stevens el 28 de Nov. de 2020
Also, to reproduce exactly the figure you show, change T to T = 1, and t to t = 0:T/256:T
scott lamb
scott lamb el 28 de Nov. de 2020
thank you worked a treat!

Iniciar sesión para comentar.

Más respuestas (1)

Alan Stevens
Alan Stevens el 29 de Nov. de 2020
When I run this
A0=1; T=1; n=6;
t = 0 : T/256 : T;
nn = length(t);
f = zeros(n,nn);
s = zeros(nn);
for ii = 1:n
for j = 1:nn
f(ii,j) = f(ii,j) + 4*A0/((2*ii-1)*pi) * sin(2*pi*(2*ii-1)*t(j)/T);
s(j) = s(j) + f(ii,j);
end
end
hold on
for ii = 1:n
plot (t,f(ii,:),'r:')
end
plot(t,s,'k-','linewidth',2)
hold off
I get
If I make the following changes
A0=1; T=0.25; n=6;
t = 0 : T/256 : 4*T;
I get

Categorías

Más información sobre Spectral Measurements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by