Can I call another ode45 inside the function file of an ode45 ?

I have an ode45 function file with equations from a matrix, but few variable in these equations are time dependent and are solutions of other few time dependent coupled equations. Can I use ode45 here to find the solution of these time dependet variables for each time step? I tried to put at the end of the function file (after and before end both ways). But that shows error.
I am giving structure of the function and main file here
%function file
function dvdt=outfun(t,vec)
dvdt=zeros(36,1);
y0=[ 1; 2; 3; 4;]
%alpha and beta are time dependent nonlieanr coupled equations.
[T0 z]= ode45('infun',t0,y0)% can it be t instead of t0?
for ii=1:length(T0)
alpha=z(ii,1);
beta=z(ii,3);
g3r=g*real(alpha)%
g3im=g*imag(alpha)%
g4r=g*real(beta)%
g4im=g*imag(beta)%
a=[.....]%matrix
v=vec;%Initial conditions in the matrix form
N=[n1 n2 n3 n4 n5 n6];
dvrdt= a*v + diag(N) ;% some matrix equations
dvdt(1)=dvrdt(1,1);%equations
....
...
dvdt(36)=dvrdt(6,6);
end
function dydt=infun(t,y)
dydt=zeros(4,1);
dydt(1)= .....;%alpha
dydt(2)=.....;%conj(alpha)
dydt(3)=......;%beta
dydt(4)=......;%conj(beta)
end
end
%main file
tsapn=0:0.01:10
x=[];
%initial condition
vv=[v11_0, v21_0,v31_0,v41_0,v51_0,v61_0,...
.
.
.
v16_0, v26_0,v36_0,v46_0,v56_0,v66_0];
[T, V]=ode45(@outfun,tspan,vv);% calling
[mm,n]=size(V);
for ii=1:mm
v1=reshape(V(ii,:),6,6);
a=max(eig(v1))
x=[x; T(ii) a]
.
.
.
.
end
Any suggestions please, thanks. Edit: just modified function names

6 Comments

Do not call your function MEAN or use the name of any other inbuilt MATLAB function. I would even avoid TEST.
"Cam I call another ode45 inside the function file of an ode45 ?"
Of course, there is absolutly no restriction on nesting ODE solvers.
[t,y] = ode45(@myfun, [0,5], 0)
t = 65×1
1.0e+00 * 0 0.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0001 0.0001 0.0003
y = 65×1
0 0.0001 0.0001 0.0002 0.0002 0.0005 0.0007 0.0010 0.0012 0.0025
function d = myfun(t,y)
[t,y] = ode45(@(t,y) 2*t, [0,5], 0);
d = mean(y);
end
Dear @Stephen23, Thanks for that. Its working now. My doubt is that how can I make sure that the time dependent couplings are working fine for each time step while being used in the dvrdt equations. For exmaple, size(z) below will depent on step size of time.
[T0 z]= ode45('infun',t0,y0)
[mt, nt]=size(z);
for ii=1:mt %or length(T0)
%for ii=1:length(T0)
alpha=z(ii,1);
beta=z(ii,3);
g3r=g*real(alpha)%
g3im=g*imag(alpha)%
Thus, when using that 'for' loop for 'ii', how can I make sure that each time step of tspan and corresponding value of z(ii,1) is being used for the same time step in dvrdt. Thanks
"how can I make sure that each time step of tspan and corresponding value of z(ii,1) is being used for the same time step in dvrdt."
As far as I understand your situation you have some data sampled at particular times** which you want to use in an ODE-solver objective function. In such a situation, the usual approach is to interpolate that data within the objective function using e.g. INTERP1. In pseudo-code:
z_query = interp1(t_samples,z_samples,t_query)
where t_query is the time currently being provided by the ODE solver.
If you need to share any data between the workspaces then define the objective functions as nested functions:
As an aside: instead of providing the function name as text I strongly recommend using a function handle.
** that you generated using ODE45, but really it does not make much difference where it came from.
Dear @Stephen23, Thanks for that. I am using a function handle for an inner ode45 function. But for each time step this inner ode45 function will return a vector form of solution and the ode function in the main file will return output for the last value of the for loop (loop used in the inner ode45 function) only.
Fo example, when my tspan=0, t0 will run for a full range of time [...]. Then, next step of tspan=0.01 and again full raneg of t0 for the inner ode45 function. But the output in the main file will be shown for the last value of tspan=10. Any way to store all the values of output solution for each time step of tspan.
You should state your problem mathematically without code.
My guess is that you don't need two calls to ode45 with 36 and 4 unknowns, respectively, but only one call with 40 unknowns.
Dear @Torsten, Thanks for that. That looks much simpler than the earlier complex version. I did that but the only doubt was different equations making me confsued and I went for the two ODE45. Thank you very much @Stephen23 and @Torsten for your help.

Sign in to comment.

Answers (0)

Products

Release

R2023b

Asked:

on 1 Feb 2024

Edited:

on 19 Feb 2024

Community Treasure Hunt

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

Start Hunting!