Filter löschen
Filter löschen

How to do a Fourier series with two for loops for a Periodic Function ?

2 Ansichten (letzte 30 Tage)
enrico Rothe
enrico Rothe am 7 Mär. 2022
Kommentiert: enrico Rothe am 8 Mär. 2022
Hey guys i need your help:
I m new to matlab and i m trying to do a Fourier Series with two for loops for a periodic Function.
I m getting the following error and i dont rly understand why and could need some help :D
Array indices must be positive integers or logical values.
Error in mHsl (line 39)
a_mhsl(j)=0;
clear
clc
close all
% Fourierkoeffizienten von Bewegungsverlauf aus mHsl
sk = [0.0014-0.0870276855443134 0.295151170426641 0.416092219888434...
-0.0299873173682189 -0.290504199655492 -0.111557021687448 ...
0.0451411005902953 0.0283060937157195 -0.000416681595205368];
ck = [0 0.220518023785692 0.275639019529607 -0.198509704076127 ...
-0.413001715701831 -0.0889925072469301 0.143231479398419 ...
0.0726292539758752 -0.00629044571050385 -0.00521997196551305];
%Taktzeit in s
tT = 1;
%Übertragungsfaktor phi = omega * t mit Omega in rad/s
omega = tT*2*pi; %Zeitvektor für neues Signal
t_mHsl = linspace(0,tT,1000);
p = 2;
%Bestimmung des Beschleunigungsvorgabe
a_mhsl = zeros(length(1),length(t_mHsl));
for j = 0:length(t_mHsl)
a_mhsl(j)=0;
for k = 0:1:length(sk)-1
a_mhsl(j) = a_mhsl(j)-k^4*(ck(k+1)*cos((p*pi/2)+k*omega*t_mHsl(j))
+sk(k+1)*sin((p*pi/2)+k*omega*t_mHsl(j)));
end
end
%Ausgabe des Beschleunigungsverlaufes
mhsl.time = t_mHsl';
mhsl.a = a_mhsl;
figure()
plot(mhsl.time,mhsl.a)
  7 Kommentare
Torsten
Torsten am 8 Mär. 2022
I just see that shifting the k-loop was not necessary because the arrays ck and sk are addressed with indices running between 1 and length(sk). So you can just use your first code and only shift the j-loop by one unit:
for j = 1:length(t_mHsl)
a_mhsl(j) = 0;
for k = 0:1:length(sk)-1
a_mhsl(j) = a_mhsl(j)-k^4*(ck(k+1)*cos((p*pi/2)+k*omega*t_mHsl(j))+sk(k+1)*sin((p*pi/2)+k*omega*t_mHsl(j)));
end
end
But if you shift the k-loop by one unit, you will have to replace all k's in your equation for a_mhsl by k-1, thus
for j = 1:length(t_mHsl)
a_mhsl(j) = 0;
for k = 1:length(sk)
a_mhsl(j) = a_mhsl(j)-(k-1)^4*(ck(k)*cos((p*pi/2)+(k-1)*omega*t_mHsl(j))+sk(k)*sin((p*pi/2)+(k-1)*omega*t_mHsl(j)));
end
end
what do u exactly mean by inserting a space between the first two elements ?
Look at your matrix sk. There is no space between 0.0014 and -0.0870276855443134.
enrico Rothe
enrico Rothe am 8 Mär. 2022
thx again. the space problem would have cost me a lot of time to find out ^^
i guess i m not the Charma-god but helping strangers on the internet can t be to bad for it. thanks again and take care :D

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by