How do I fix this

1 回表示 (過去 30 日間)
Daniel Schilling
Daniel Schilling 2018 年 11 月 15 日
回答済み: Rik 2018 年 11 月 15 日
dw = 0.01;
w = -31.4:dw:31.4;
dt = 0.1;
t = -100:dt:100;
G_omega = @(w) 2.*((abs(w)>=5)-(abs(w)>=10));
H_omega = @(w) (5.*abs(w)).*((w>=-20)&(w<=20));
M_omega = @(w) abs(G_omega(w)) .* abs(H_omega(w));
m_t = zeros(size(t));
for ii=1:length(w)
m_t = m_t + M_omega(w(ii))*exp(j*w(ii)*t)*dw/2/pi;
end
energy=trapz(t,abs(m_t(t)).^2)
When running this I get the error "Array indices must be positive integers or logical values" and I'm not completely sure why? The error comes from when I'm calculating the energy. I seem to have this issue often when I'm creating an array from a for loop like this and then to do anything with it other than plot it. Any suggestions would be much appreciated.

採用された回答

Rik
Rik 2018 年 11 月 15 日
You are using t as an index to m_t, but that contains negative values. (in the call to trapz)
I suspect you want to run the line below instead.
energy=trapz(t,abs(m_t).^2);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by