Can I call another ode45 inside the function file of an ode45 ?
Show older comments
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)
function d = myfun(t,y)
[t,y] = ode45(@(t,y) 2*t, [0,5], 0);
d = mean(y);
end
Anil
on 7 Feb 2024
"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.
Anil
on 19 Feb 2024
Anil
on 19 Feb 2024
Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!