How to return an intermediate variable from ode45 in Matlab
Ältere Kommentare anzeigen
What I want
Hi there.
I want to store or return an intermediate calculations in global variable at spacific time instants. For example.
%%%%% Caller Code
x0 = [1 ; 2];
t = 0:0.1:10
[t,x] = ode45(@(t,x) odefun(t,x), t, x0);
%%%%% ODE Function
function dx = odefun(t,x)
if t<3
e = x(1)-x(2) + 2; % variable of my intrest, to be stored or returned each 0.1 sec
elseif t<6
e = x(1)-x(2) - sin(t);
elseif t<10
e = x(1)-x(2)/x(1);
end
dx(1) = -x(2)
dx(2) = sin(2*pi*t)
end
My requirement is that e should be calculated at exact time instants at which x is calculated. this way both would have the same length.
My Approch so far ,
%%%%% Caller Code
global e indx;
e=0;
indx=1;
x0 = [1 ; 2];
t = 0:0.1:10
[t,x] = ode45(@(t,x) odefun(t,x), t, x0);
%%%%% ODE Function
function dx = odefun(t,x)
global e indx;
if t<3
e(indx) = x(1)-x(2) + 2; % variable of my intrest, to be stored or returned each 0.1 sec exactly
elseif t<6
e(indx) = x(1)-x(2) - sin(t);
elseif t<10
e(indx) = x(1)-x(2)/x(1);
end
dx(1) = x(2)
dx(2) = sin(2*pi*t)
indx = indx+1;
end
But using this method the length of e is extremly large, for example, x length is 200, then i have length of e more than 10000. My origional code is way more complex and simulated for longer time, which makes the process exreamply diffficult and inaccurate.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
