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

12 ビュー (過去 30 日間)
Allie Ludovice
Allie Ludovice 2023 年 11 月 22 日
コメント済み: Alexander 2023 年 11 月 22 日
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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by