Array indices must be positive integers or logical values.

%RK4 för I och U
% dI/dt = (U(I0.^2 + I.^2))/(L0*I0.^2)
% dU/dt = (-I)/C
% Konstanter
L0 = 0.7; % [H]
I0 = 1; % [A]
C = 0.5e-6; % [F]
% definera funktion
fI = @(t,I,U) (U(I0^2+I^2))/(L0*I0^2);
fU = @(t,I,U) (-I)/C;
%initiella villkor
t(1) = 0;
I(1) = 0;
U(1) = 220;
%step size
h = 0.001;
tslut = 0.01;
N = ceil(tslut/h);
% uppdate loop
for i = 1:N
%update time
t(i+1) = t(i) + h;
%update U & I
k1I = fI(t(i) , I(i), U(i));
k1U = fU(t(i) , I(i), U(i));
k2I = fI(t(i)+h/2, I(i)+h/2*k1I, U(i)+h/2*k1U);
k2U = fU(t(i)+h/2, I(i)+h/2*k1I, U(i)+h/2*k1U);
k3I = fI(t(i)+h/2, I(i)+h/2*k2I, U(i)+h/2*k2U);
k3U = fU(t(i)+h/2, I(i)+h/2*k2I, U(i)+h/2*k2U);
k4I = fI(t(i)+h , I(i)+h *k3I, U(i)+h *k3U);
k4U = fU(t(i)+h , I(i)+h *k3I, U(i)+h *k3U);
I(i+1)=I(i)+h/6*(k1I + 2*k2I + 2*k3I + k4I);
U(i+1)=U(i)+h/6*(k1U + 2*k2U + 2*k3U + k4U);
end
%plot the
plot(t,I)
hold on
plot(t,U)
xlabel('tid')
ylabel('ngt')
legend('I','U')
set(gca, 'fontsize', 16)
Array indices must be positive integers or logical
values.
Error in test2 (line 36)
k2I = fI(t(i)+h/2, I(i)+h/2*k1I, U(i)+h/2*k1U);
why do I have the error in line 36?

Answers (2)

This question is closed.

Asked:

on 30 Mar 2019

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!