Index exceeds array bounds.

for k=3:Ns
% 1st derivative
ikd=(it(k+1)-it(k-1))/(2*h);
% 2nd derivative
ikdd=(it(k+1)-2*it(k)+it(k-1))/(h^2);
% Peak current using 1st derivative and 2nd derivative
Ipeak(k)=sqrt(((ikd/wo)^2)+((ikdd/(wo^2))^2))
end
figure(4)
plot(t,Ipeak,'*k',t,Im_correct,'r','LineWidth',1)
title('first and second derivative algorithm')
legend('Calc. Im using 1st derivative and 2nd derivative','Correct Im')
xlabel('Time(s)')
ylabel(' RMS Current (A)')
grid
---------------
Index exceeds array bounds.
Error in First_Second_Derivative (line 11)
ikd=(it(k+1)-it(k-1))/(2*h);

1 个评论

i tryed to put for k=3:Ns -1
but another error apears
Error using plot
Vectors must be the same length.
Error in First_Second_Derivative (line 21)
plot(t,Ipeak,'*k',t,Im_correct,'r','LineWidth',1)

请先登录,再进行评论。

回答(1 个)

Cris LaPierre
Cris LaPierre 2021-12-29

0 个投票

We don't know what your variables it, Ns, wo, t, or Im_correct are, so we can't say for certain, but here are some thoughts.
It looks like Ns = length(it) since using Ns-1 in your for-loop counter fixed the exceeding array bounds errors. This is because you index it using k+1, so in the last loop when k=Ns, the index k+1 exceeds the size or it.
As for the new error, this error is because t does not have the same length as either Ipeak or Im_correct. You will need to check your variables to determine which it is. When plotting, each y value has to have a corresponding x value, and vice versa.

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by