Where is the problem with my code. it never ends !!

1 回表示 (過去 30 日間)
geometry geometry
geometry geometry 2018 年 5 月 18 日
回答済み: Geoff Hayes 2018 年 5 月 18 日
What is the problem with the following code?
It calculates the exponential coefficients fo fourier series of periodic function f(t).
when I run it the compilation never ends !! and I must close matlab and run it again.
function [an]=fc(f,T)
syms t n
n=-20:1:20;
an=(1/T)*int(f*exp(-1i*n*2*pi*t/T),t,0,T);
end
syms t n
f=rectangularPulse(t/4)*(abs(t)-1);
%f=rectangularPulse(t);
% f=sin(t);
Y=fc(f,20);
subplot(211)
stem(-20:1:20,abs(Y));
ylabel('|an|')
grid;
subplot(212)
stem(-20:1:20,angle(Y));
ylabel('<an')
grid;

回答 (1 件)

Geoff Hayes
Geoff Hayes 2018 年 5 月 18 日
geometry - your function appears to be recursive
function [an]=fc(f,T)
syms t n
n=-20:1:20;
an=(1/T)*int(f*exp(-1i*n*2*pi*t/T),t,0,T);
end
syms t n
f=rectangularPulse(t/4)*(abs(t)-1);
%f=rectangularPulse(t);
% f=sin(t);
Y=fc(f,20); % <------------------
So it continually calls itself and without a stopping condition, it will call itself forever (!).

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by