Index exceeds the number of array elements in ODE using anonymous function

1 view (last 30 days)
I dont't understand what is wrong with this scirpt. For different set of equations I was able to use anonymous functions inside ODE handle and everuthing was just perfect, but now, in much larger system, I am constantly getting this error
Index exceeds the number of array elements (34).
Error in full_DAO>@(t,x)[1/L1*(.............
Error in odearguments (line 90) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 150) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in full_DAO (line 118) [t,sol] = ode15s(deq,[0 100e-6],zeros(1,34));
Do you have idea what can be wrong? Clearly, number of equations is 34 and nowhere I am calling higher index so I don't get it. How to avoid this error?
clear
close all
clc
format shorteng
m = 0.6;
L1 = 40e-6;
L2 = 40e-6;
L3 = 125e-9;
L4 = 125e-9;
L5 = 125e-9;
L6 = 40e-6;
L7 = 40e-6;
L8 = 125e-9;
L9 = 125e-9;
L10 = 40e-6;
L11 = 40e-6;
L12 = (1/2 + m/2)*125e-9;
L13 = (1/2 + m/2)*125e-9;
L14 = 40e-6;
L15 = 40e-6;
L16 = 125e-9*(1 - m^2)/(2*m);
L17 = 125e-9*(1 - m^2)/(2*m);
U1 = -2;
U2 = 3;
U3 = 7;
Udd = 11;
Ru1 = 100;
Ru2 = 60;
Ru3 = 20;
Rudd = 7;
R1 = 50;
R2 = 50;
%RF loss modelling
Rc3 = 1e9;
Rc4 = 5e3;
Rc7 = 200e3;
Rc8 = 500e3;
Rc11 = 15e3;
Rc12 = 700e3;
RL3 = 5;
RL4 = 2;
RL5 = 7;
RL8 = 3;
RL9 = 5;
RL13 = 5;
RL12 = 4;
C1 = 10e-9;
C2 = 10e-9;
C3 = 50e-12;
C4 = 50e-12;
C5 = 10e-9;
C6 = 10e-9;
C7 = 50e-12;
C8 = 50e-12;
C9 = 10e-9;
C10 = 10e-9;
C11 = 50e-12;
C12 = 50e-12;
C13 = 10e-9;
C14 = m/2*50e-12;
C15 = 10e-9;
C16 = m/2*50e-12;
C17 = 10e-9;
%%% Shockley diode equation
Is = 1e-14;
k = 1.38e-23;
q = 1.602e-19;
T = 300;
Vt = k*T/q;
id = @(x) 0*Is*(exp(x/Vt) - 1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
deq = @(t,x) [...
1/L1*(U1 - x(1)*Ru1 - x(2));
1/C1*(x(1) - x(3));
1/L2*(x(2) - (x(4)/C2 + x(6)));
x(3) - id(x(4)/C2 + x(6));
1/L3*(x(8) - x(5)*RL3 - x(6));
1/C3*(x(5) - x(7) + (x(3) - id(x(4)/C2 + x(6))) - x(6)/Rc3);
1/L4*(x(6) - x(14) - x(7)*RL4);
1/C4*(x(9) - x(5) - x(8)/Rc4);
1/L5*(x(16) - x(9)*RL5 - x(8));
1/C5*(x(11) - x(13));
1/L6*(U2 - x(11)*Ru2 - x(10));
x(13) - id(x(12)/C6 + x(14));
1/L7*(x(10) - (x(12)/C6 + x(14)));
1/C7*(x(7) - x(15) + (x(13) - id(x(12)/C6 + x(14))) - x(14)/Rc7);
1/L8*(x(14) - x(22) - x(15)*RL8);
1/C8*(x(17) - x(16)/Rc8 - x(9));
1/L9*(x(24) - x(17)*RL9 - x(16));
1/C9*(x(19) - x(21));
1/L10*(U3 - x(19)*Ru3 - x(18));
x(21) - id(x(20)/C10 + x(22));
1/L11*(x(18) - (x(20)/C10 + x(22)));
1/C11*(x(15) - x(23) - x(22)/Rc11 + (x(21) - id(x(20)/C10 + x(22))));
1/L12*(x(22) - (x(34)/C17 + (x(23) - x(33))*R2) - x(23)*RL12);
1/C12*(-x(29) - x(25) - x(17) - x(24)/Rc12);
1/L13*(x(24) - (x(30)/C15 + (x(25) - x(31))*R1) - x(25*RL13));
x(29) - x(27);
1/L14*(x(26)/C13 - x(27)*Rudd - Udd);
x(31);
1/L15*(x(24) - x(26)/C13);
x(25) - x(31);
1/L16*(x(30)/C15 + (x(25) - x(31))*R2 - x(28)/C14);
x(33);
1/L17*(x(34)/C17 + (x(23) - x(33))*R2 - x(32)/C16);
x(23) - x(33);
];
[t,sol] = ode15s(deq,[0 100e-6],zeros(1,34));
figure(1)
plot(t,sol)

Accepted Answer

Star Strider
Star Strider on 15 Oct 2021
There’s a typo:
1/L13*(x(24) - (x(30)/C15 + (x(25) - x(31))*R1) - x(25*RL13));
↑ ← HERE
Correct that:
1/L13*(x(24) - (x(30)/C15 + (x(25) - x(31))*R1) - x(25)*RL13);
and it works!
format shorteng
m = 0.6;
L1 = 40e-6;
L2 = 40e-6;
L3 = 125e-9;
L4 = 125e-9;
L5 = 125e-9;
L6 = 40e-6;
L7 = 40e-6;
L8 = 125e-9;
L9 = 125e-9;
L10 = 40e-6;
L11 = 40e-6;
L12 = (1/2 + m/2)*125e-9;
L13 = (1/2 + m/2)*125e-9;
L14 = 40e-6;
L15 = 40e-6;
L16 = 125e-9*(1 - m^2)/(2*m);
L17 = 125e-9*(1 - m^2)/(2*m);
U1 = -2;
U2 = 3;
U3 = 7;
Udd = 11;
Ru1 = 100;
Ru2 = 60;
Ru3 = 20;
Rudd = 7;
R1 = 50;
R2 = 50;
%RF loss modelling
Rc3 = 1e9;
Rc4 = 5e3;
Rc7 = 200e3;
Rc8 = 500e3;
Rc11 = 15e3;
Rc12 = 700e3;
RL3 = 5;
RL4 = 2;
RL5 = 7;
RL8 = 3;
RL9 = 5;
RL13 = 5;
RL12 = 4;
C1 = 10e-9;
C2 = 10e-9;
C3 = 50e-12;
C4 = 50e-12;
C5 = 10e-9;
C6 = 10e-9;
C7 = 50e-12;
C8 = 50e-12;
C9 = 10e-9;
C10 = 10e-9;
C11 = 50e-12;
C12 = 50e-12;
C13 = 10e-9;
C14 = m/2*50e-12;
C15 = 10e-9;
C16 = m/2*50e-12;
C17 = 10e-9;
%%% Shockley diode equation
Is = 1e-14;
k = 1.38e-23;
q = 1.602e-19;
T = 300;
Vt = k*T/q;
id = @(x) 0*Is*(exp(x/Vt) - 1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
deq = @(t,x) [...
1/L1*(U1 - x(1)*Ru1 - x(2));
1/C1*(x(1) - x(3));
1/L2*(x(2) - (x(4)/C2 + x(6)));
x(3) - id(x(4)/C2 + x(6));
1/L3*(x(8) - x(5)*RL3 - x(6));
1/C3*(x(5) - x(7) + (x(3) - id(x(4)/C2 + x(6))) - x(6)/Rc3);
1/L4*(x(6) - x(14) - x(7)*RL4);
1/C4*(x(9) - x(5) - x(8)/Rc4);
1/L5*(x(16) - x(9)*RL5 - x(8));
1/C5*(x(11) - x(13));
1/L6*(U2 - x(11)*Ru2 - x(10));
x(13) - id(x(12)/C6 + x(14));
1/L7*(x(10) - (x(12)/C6 + x(14)));
1/C7*(x(7) - x(15) + (x(13) - id(x(12)/C6 + x(14))) - x(14)/Rc7);
1/L8*(x(14) - x(22) - x(15)*RL8);
1/C8*(x(17) - x(16)/Rc8 - x(9));
1/L9*(x(24) - x(17)*RL9 - x(16));
1/C9*(x(19) - x(21));
1/L10*(U3 - x(19)*Ru3 - x(18));
x(21) - id(x(20)/C10 + x(22));
1/L11*(x(18) - (x(20)/C10 + x(22)));
1/C11*(x(15) - x(23) - x(22)/Rc11 + (x(21) - id(x(20)/C10 + x(22))));
1/L12*(x(22) - (x(34)/C17 + (x(23) - x(33))*R2) - x(23)*RL12);
1/C12*(-x(29) - x(25) - x(17) - x(24)/Rc12);
% 1/L13*(x(24) - (x(30)/C15 + (x(25) - x(31))*R1) - x(25*RL13));
1/L13*(x(24) - (x(30)/C15 + (x(25) - x(31))*R1) - x(25)*RL13);
x(29) - x(27);
1/L14*(x(26)/C13 - x(27)*Rudd - Udd);
x(31);
1/L15*(x(24) - x(26)/C13);
x(25) - x(31);
1/L16*(x(30)/C15 + (x(25) - x(31))*R2 - x(28)/C14);
x(33);
1/L17*(x(34)/C17 + (x(23) - x(33))*R2 - x(32)/C16);
x(23) - x(33);
];
[t,sol] = ode15s(deq,[0 100e-6],zeros(1,34));
figure(1)
plot(t,sol)
.
  3 Comments
Jan Filip
Jan Filip on 15 Oct 2021
Also just in case that I encounter it again, how did you spot it? By stepping into ode solver?
Star Strider
Star Strider on 15 Oct 2021
Oh god. You sir are genius...as always :)
Thank you!
Also just in case that I encounter it again, how did you spot it? By stepping into ode solver?’
No. I just looked for subscript references that didn’t look right, and since I wasn’t concerned with anything else (syntax and other problems), it was relatively easy to detect.
I did it here in the online Run feature, however doing it offline on my computer could have thrown a line-specific error that would have made it easier to detect. Had I kept getting errors of various types, I might have done that. Fortunately, that wasn’t nbecessary.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!