필터 지우기
필터 지우기

How do I fix this

조회 수: 2 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by