ode45 problem: Index in position 1 is invalid

2 views (last 30 days)
Hello, I am trying to compute solutions for 2 ODEs for different t values. This always works the first time after I clear the workspace but then I get the following error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in @(t,y)b(t,y,alfa,sigma)
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
I have defined the function b as:
function dydt = b(t,y,alfa,sigma)
dydt = alfa.*y - 0.5.*sigma.^2.*y^2 + 1;
end
and the function a as:
function dydt = a(t,y,alfa,gamma,b)
dydt = -alfa.*gamma.*b;
end
And then:
T = 30;
maturities = 0:0.25:T;
sigma = 0.02;
gamma = 0.02;
alfa = 0.1;
b = deval(ode45(@(t,y) b(t,y,alfa,sigma),[T 0], 0), flip(maturities));
a = deval(ode45(@(t,y) a(t,y,alfa,gamma,deval(ode45(@(t,y) b(t,y,alfa,sigma),[T 0], 0), t)),[T 0], 0), flip(maturities));
How can I solve this error? To which index is Matlab referring when saying it's invalid?

Answers (1)

Walter Roberson
Walter Roberson on 3 Dec 2022
b = deval(ode45(@(t,y) b(t,y,alfa,sigma),[T 0], 0), flip(maturities));
Before that line, b refers to the function b(t,y,alfa,sigma) that you define.
After that line, b refers to the numeric results of the deval call.
a = deval(ode45(@(t,y) a(t,y,alfa,gamma,deval(ode45(@(t,y) b(t,y,alfa,sigma),[T 0], 0), t)),[T 0], 0), flip(maturities));
There you use b(t,y,alfa,sigma) inside the deval() portion. But b is now an array, not the function.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!