How to accelerate the ode45 solver?
Mostrar comentarios más antiguos
Hi, everyone, I'm working with this code (see below) but it takes hours to finish and thus, to show me the results, I want to know if there is a way that I can speed up this, because maybe I'll need to add it a greater tspan, I tried to adjust the step size (as you can see in the code) but I'm not sure it is working, is there anything wrong in here? This is just one of the five codes that I'll need to solve this way, this is the simplest (the other ones have more than 2 ODE to be solved) so I need it to be solve quicker, how can I do it?
Thanks
function [t,x]= cobellilee
global out1
tspan=[0:5:180];
x0=[80; 0];
options=odeset('InitialStep',5,'MaxStep',5)
[t,x]=ode45(@f,tspan,x0,options);
out1=[t,x]
assignin('base','out1',out1);
figure
subplot(2,1,1)
plot(t,x(:,1));
subplot(2,1,2)
plot(t,x(:,2));
function dxdt= f(t,x,uuno)
global tiempo
tiempo=t
assignin('base','tiempo',tiempo)
dxdt=[
(-4.9e-2-x(2))*x(1) + 4.42;
-9.1e-2*x(2) + 8.96*U1(t)
];
function [uuno]=U1(t)
global uu1 tiempo s1
uu1=importdata('datossinmodi.mat')
assignin('base','uu1',uu1)
s1=size(uu1,1)
assignin('base','s1',s1)
tiempo=t
assignin('base','tiempo',tiempo)
tiempo=round(tiempo)
if (s1>=tiempo)
if (tiempo==0)
uuno=0;
else
uuno=uu1(tiempo)
end
else
uuno=0;
end
Note: 'datossinmodi.mat' is a 288x1 vector that I need the code to read depending on time, is what I did fine? Thanks again!
Respuestas (2)
Walter Roberson
el 5 de Oct. de 2015
0 votos
To speed up the routine:
- do not use global
- do not use assignin
- read in datossinmodi.mat once at the beginning and pass the value in, rather than reading it every iteration
2 comentarios
Annie
el 5 de Oct. de 2015
Walter Roberson
el 5 de Oct. de 2015
global and assignin are guaranteed to make the code slower, not faster. If you search the blogs for information on which kind of data references are faster or slower, global is the second worst.
[t,x]=ode45(@(t,y) f(t,y,uu1), tspan, x0, options);
Kelly Kearney
el 5 de Oct. de 2015
If I understand your code correctly, the U1 function is simply interpolating the uu1 vector to the appropriate time, right? If so, you can eliminate all the loading and assigning, as Walter suggests:
uu1 = importdata('datossinmodi.mat');
ufun = @(t) interp1(1:length(uu1), uu1, t, 'nearest', 0);
f = @(t,x) [(-4.9e-2-x(2))*x(1) + 4.42; ...
-9.1e-2*x(2) + 8.96*ufun(t)];
tspan=[0:5:10];
x0=[80; 0];
options=odeset('InitialStep',5,'MaxStep',5)
[t,x]=ode45(f,tspan,x0,options);
Because it's a variable step solver, ode45 may still be slow for your set of equations if it has to reduce its step size a lot while solving. The MaxStep option only controls the largest possible step it takes, but not the smallest. If your solver code is still pretty slow after you've eliminated the unnecessary loads, try experimenting with other solvers.
6 comentarios
Annie
el 6 de Oct. de 2015
Walter Roberson
el 6 de Oct. de 2015
Nested would be more efficient. However if you are creating the GUI using GUIDE then it does not create nested functions, so you should set something in the handles structure, then call guidata() to save the change in the master copy of handles, then access the value from the handles structure in the place you need it.
assignin() does work, but it is not recommended for various reasons.
Kelly Kearney
el 6 de Oct. de 2015
I'd have to see a small snippet of your GUI code to see what might be the best way to pass things back and forth. I do very little GUI programming myself, never with GUIDE, and usually just stick variables in the figure's application data when it needs to be shared between multiple scopes. That may or may not work for you.
Annie
el 30 de Nov. de 2015
Walter Roberson
el 30 de Nov. de 2015
[t,x]=ode45(f,tspan,x0);
Categorías
Más información sobre Parallel and Cloud en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
