Speed reduction in ode45 when i pass parameters from workspace

1 view (last 30 days)
I'm trying to simulate a system with ode45.
I have tried two strategy which they have very different run times.
At first I declare necessary variables and assign it's values inside the function solved by ODE :
function dy = Fun2_004(t,y,frequency)
dy=zeros(11,1)
%Initialization of variables
K_t=.559;
f_Theta=.002;
J=5e-7;
K_Theta=9.45e-4;
.
.
.
%equations
dy=...
end
[T2,Y2] = ode45(@(t,y)Fun2_004(t,y,Omega_Hertz),[0 Time],Inits,options);
This code take 0.5 seconds to execute.
I want to use this code in an optimization process so I pass it's variables as arguments of this function :
function dy = Fun2_004_PSO(t,y,frequency,InputVars)
dy=zeros(11,1)
%Initialization of variables
K_t=InputVars(1);
f_Theta=InputVars(2);
J=InputVars(3);
K_Theta=InputVars(4);
.
.
.
%equations
dy=...
end
This code take 40 second to execute!!!
I tried global variables , codegen and persistent variables but it has no considerable effect.
Does any body exactly knows source and solution of this problem?

Answers (0)

Categories

Find more on Programming 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!