What function should I use to speed up code instead of GLOBAL

2 visualizaciones (últimos 30 días)
Babak
Babak el 24 de Feb. de 2015
Respondida: John D'Errico el 24 de Feb. de 2015
Hello,
Here is my problem. I have a ODE45 solver which use part of 5 big precalculated matrixes (fixed matrxices which will not be updated) to simulate my program. When I run the program it is very time consuming and I am looking for a way to speed up this process. I know that using global value is not a good idea. what can I used instead of global to speed up the process?? My code is like this:
clc
clear all
Load A B C D F
global A B C D F
[tout,y]=ode45('sys',(0:1/3600:0.5),zeros(50,1));
% ___________________________________
%%Inside the function (sys) %%
function xdot = sys(t,x)
global A B C D F
...

Respuestas (1)

John D'Errico
John D'Errico el 24 de Feb. de 2015
Um, while I dislike the use of global variables anywhere as they are essentially never needed, They are surely not the problem here, but a function handle can help you to pass in those variables trivially into your function sys. It won't change the speed by much at all though.
What is the problem is anyone's guess, since we have not a clue as to what is inside sys. You may have a stiff system, so use of a tool that is designed to work with stiff systems may help. Or it may be nothing more that you are impatient, and don't want to wait for the ODE solver to solve a problem with 50 variables. Bigger problems take more time.
I might suggest learning how to use the profiling tool, as it can often help you to speed up your code.
Sorry, but there is rarely a magic salve you can apply to a problem to make things run faster. Ok, a faster computer helps.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by