How do I extract an intermediate variable calculated and used inside my ode45 function?
Mostra commenti meno recenti
Hello,
I'm using ode45 to produce solutions to ODE's and It works perfectly with me. However, I have a variable that depends on the current differentiated variable ,say x as in my example below. This variable is computed inside my function which then used in my differential equations. This is my ode call:
[T,X] = ode45(@(t,x) myode(t,x,P,BK,R), tspan, x0);
and here is my function:(the variable I need to extract at each instant, i.e. it should have same length as X and T, is u)
function dx=myode(t,x,P,BK,R)
x1=x(1); x2=x(2);
N=size(BK,1);
f1=x2;
f2=sin(x1)
f=[f1;f2]; %system vector field
gx=[0;cos(x1)];
phi=(ones(length(P),1)*x.').^BK(1:length(P),:);
phi=prod(phi,2);
u=-inv(R)*gx'*P'*phi;
if u>=1
u=1;
end
dx=f+gx*u;
end
Is it possible to get u? what is the best way to do that? I know similar questions were and are being asked a lot but I searched for the solution and couldn't find a workable one to my problem!
For a simpler example (because the variables I have as my inputs above are very large matrices) and we can work on this since the main goal is the same:
x0=[1;1]; tspan=[0 1]; R=1;
[T,X] = ode45(@(t,x) myode(t,x,R), tspan, x0);
function dx=myode(t,x,R)
x1=x(1); x2=x(2);
f1=x2;
f2=sin(x1);
f=[f1;f2]; %system vector field
gx=[0;1];
u=-inv(R)*gx'*x;
if u>=1
u=1;
end
dx=f+gx*u;
end
I want to get the computed values of u at each time instant with t with x. In other words, I want to get an array with the computed values of u.
Thanks in advance. Your help is appreciated.
2 Commenti
madhan ravi
il 11 Nov 2018
upload your question(equation) as latex form and provide all the necessary details
Note that this line is probably not very efficient or accurate:
u=-inv(R)*gx'*P'*phi;
The inv and * should probably be replaced by mldivide. If you have large arrays then this will be significantly more efficient, and well as being more accurate numerically.
Risposta accettata
Più risposte (3)
madhan ravi
il 11 Nov 2018
Modificato: madhan ravi
il 11 Nov 2018
0 voti
Walter Roberson
il 11 Nov 2018
0 voti
If the variable is to be the same length as t then you need to recalculate it afterwards. The ode* routines do not generally calculate at the returned t: they calculate at multiple times and use them to estimate the values that they return. In general the ode* routines may go forwards or backwards in time and may calculate with multiple boundary conditions at the same time point in order to be sure that they are within the error constraints.
1 Commento
Raghavendra Ragipani
il 6 Feb 2020
0 voti
Hi,
I had similar problem and I created functions to easily achieve this task. I decided to share it with the MATLAB community. Check it out at the following link.
Call save_var_in_ODE() function inside the ODE program and call get_var_in_ODE() in the main script file where you'll further manipulate it or plot it.
Example usage is given along with the package.
Cheers,
Raghav
Categorie
Scopri di più su Ordinary Differential Equations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!