How can I plot this Fourier series in a for loop?

12 次查看(过去 30 天)
I am trying to plot the sine from of a fourier series using the pre-determined equations/ values for A0, Ak, and Bk. I cannot figure out how to plot the fourier series. I keep getting errors where I noted "ERROR POINT" saying "Unrecognized function or variable 't'." I'm not sure why or how to fix it, as everything I have tried, including plotting outside of the for loop and implementing another for loop for the t/ time variable, results in nothing being plotted. Any help would be much appreciated!!
A0 = 1.4;
x = A0/2; %initializing value of x for k = 0
for k=1:100
Ak = (1/(k*pi))*sin(7*k*pi/5); %hand calculated equation
Bk = -(1/(k*pi))*(cos(7*k*pi/5)-1); %hand calculated equation
Mk = sqrt(Ak^2+Bk^2);
psi_k = atan(Ak/Bk);
x = x + Mk*sin(k*t*pi/5 + psi_k); %ERROR POINT, sine form of fourier series
plot(t,x);
end

回答(1 个)

Alexander
Alexander 2023-11-22
t is not defined.
  2 个评论
Allie Ludovice
Allie Ludovice 2023-11-22
How do I define it so it isn't just a constant? I need t to be the variable on the x axis of the plot.
Alexander
Alexander 2023-11-22
I don't know if the computation makes sense but it runs w/o error:
A0 = 1.4;
xs = A0/2; %initializing value of x for k = 0
t = 1:100;
for k=1:100
Ak = (1/(k*pi))*sin(7*k*pi/5); %hand calculated equation
Bk = -(1/(k*pi))*(cos(7*k*pi/5)-1); %hand calculated equation
Mk = sqrt(Ak^2+Bk^2);
psi_k = atan(Ak/Bk);
x(k) = xs + Mk*sin(k*t(k)*pi/5 + psi_k); %former ERROR POINT, sine form of fourier series
xs = x(k);
end
plot(t,x)
Hopefully it helps.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by