Trouble with syntax and variable types in ODE45

Hello!
Tahnks in advance. This is a piece of my code
syms t_dum
mult_Heb_P_11 = @(t,J) K_heb_p(t).*CC11(t,J);
mult_Heb_M_11 = @(t,J) K_heb_m(t).*CC11(t,J);
Sol_Heb_P_11 = @(J) subs ( int ( mult_Heb_P_11(t_dum,J),t_dum,[0,1] ) , [J11_dum,J12_dum,J21_dum,J22_dum] , [J,J12_0,J21_0,J22_0] ) ;
Sol_Heb_M_11 = @(J) subs ( int ( mult_Heb_M_11(t_dum,J),t_dum,[0,1] ) , [J11_dum,J12_dum,J21_dum,J22_dum] , [J,J12_0,J21_0,J22_0] ) ;
f_Heb_11 = @(t,J) lambda.*[ f_p(J).*Sol_Heb_P_11(J) - alpha*f_m(J)*Sol_Heb_M_11(J) ];
figure
hold on
[t1,J1_Heb_11]=ode45(f_Heb_11,[0 1000],J11_0);
plot(t,J1_Heb_11,'color','b');
hold off
I did something very similar before, but now am getting either
Error using odearguments (line 113)
Inputs must be floats, namely single or double.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Untitled3 (line 134)
[t1,J1_Heb_11]=ode45(f_Heb_11,[0 1000],J11_0);
Saw some examples online, but failed all of them. when I try
[t1,J1_Heb_11]=ode45(f_Heb_11(t1,J1_Heb_11),[0 1000],J11_0);
plot(t,J1_Heb_11,'color','b');
I get
Unrecognized function or variable 't1'.
Nor this syntax helped
[t1,J1_Heb_11]=ode45(@(t1,J1_Heb_11) f_Heb_11,[0 1000],J11_0);
plot(t,J1_Heb_11,'color','b');
Its strange for me, because this previous code worked perfectly.
Sol_Heb_P = @(J) abs(integral(@(t) K_heb_p(t).*CC0i(t,J),-Inf,Inf));
Sol_Heb_M = @(J) abs(integral(@(t) K_heb_m(t).*CC0i(t,J),-Inf,Inf));
f_Heb = @(t,J) lambda.*[ f_p(J).*Sol_Heb_P(J)- alpha.*f_m(J).*Sol_Heb_M(J) ];
[t1,J1]=ode45(f_Heb,[0 10.^9],a);
plot(t1,J1,'color','b');
So I guess the problem lies in the line with
subs ( int (......)
Would very much appreciate to know what am I missing, on the syntax level?
Not at all a programmer..
Thanks!

1 Comment

also this doesn't help
[tt, JJ] = ode45(@(tt,JJ) f(tt,JJ,lambda,alpha,f_p(JJ),Sol_Heb_P_11(JJ),f_m(JJ),Sol_Heb_M_11(JJ)),[0 1000],J11_0)
function f_Heb_11 = f(t,J,lambda,alpha,f_p,Sol_Heb_P_11,f_m,Sol_Heb_M_11)
f_Heb_11 = lambda.*[ f_p(J).*Sol_Heb_P_11(J) - alpha*f_m(J)*Sol_Heb_M_11(J) ];
end

Answers (1)

When you use ode45 or any of the ODE solvers in MATLAB, all the functions that you pass into it (especially the ODE function you pass in as the first input but also including any functions in the options structure) must return either double precision or single precision values. Yours violates this rule; it returns a sym array.
If you want to use ode45 to solve your system, before your function returns it needs to convert the output it's going to return to double precision using the double function.
Symbolic Math Toolbox also has functions for solving differential equations. If you want to work fully symbolically, use those functions instead of ode45.

This question is closed.

Asked:

on 7 May 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!