Passing parameters to dde23 slows it down
Show older comments
I am having a set of Delay Differential Equations which I am trying to solve using dde23. When I define the function in the form,
function change = eqn(t, X, Z)
%Defining parameter 'k' here
%Inserting the set of equations here.
end
, the code runs.
However if I try to pass a parameter into the function
function change = eqn(t, X, Z, k)
%Inserting the set of equations here.
end
And accordingly modify dde23 line in the main code:
K = 1;
sol = dde23(@(t, x, z) eqn(t, x, z, K), tau, init_vals, tspan, options);
The code takes a lot of time to run. If anyone knows why and how I can improve this, please let me know!
I have to be able to pass the parameter to the function since I am going to use dde23 in a loop with a range of the parameter values.
1 Comment
Walter Roberson
on 26 Sep 2020
Can you post your two different versions of the code ?
Answers (1)
Peter O
on 26 Sep 2020
Anonymous functions carry some overhead. I wonder if the way it's parsing the arguments to dde23 is causing it to rebuild the anonymous function on each subcall.
Does defining the anonymous function ahead of sending it as an argument help?
for ix=1:N
k = rand(1);
dfcn = @(t,x,y,z) eqn(t,x,y,z,k);
sol = dde23(dfcn, tau, init_vals, tspan, options)
end
6 Comments
Sneha Srikanth
on 26 Sep 2020
Peter O
on 26 Sep 2020
Okay -- something else to try: Profile the code. Turn on the profiler using the instructions at the link below. Does the report it generates give you any insight on where it's spending the time?
https://www.mathworks.com/help/matlab/matlab_prog/profiling-for-improving-performance.html
Walter Roberson
on 26 Sep 2020
sol = dde23(@(t, x, z) eqn(t, x, z, K), tau, init_vals, tspan, options);
would not rebuild the function every time. It would evaluate each of the arguments in turn, getting out a function handle for the first argument, and that function handle would be passed to dde23 -- the same as if you had constructed the function handle before the function.
Sneha Srikanth
on 28 Sep 2020
Peter O
on 29 Sep 2020
I'm still a little confused, but I think there's another thread to pull.
When you said there was an error in a formula that led to a change in the size of the derivatives, my first impression is that the wrong parameters alter the stiffness of the problem. When, the derivatives become more sensitive to changes in their values, dde23 has to take smaller steps to keep its error tolerances within bounds, which leads to (a lot) more function calls and longer time to run.
But you're saying that in both of your cases you had the error in computing k and there was a noticeable difference in overhead? Since Z seems to affect the parameters, where does it get calculated and is it dependent on anything? There also seems to be a big K and a lowercase k in your solutions. Is everything supposed to be one k? Finally, assuming state is 6 elements long, in the code you posted there seems to be an element-wise [2x1] x [3x1] x [3x1] operation in changeP, which MATLAB should throw an error on for bad dimensions. Is damp missing a third value there?
Sneha Srikanth
on 29 Sep 2020
Categories
Find more on JSON Format 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!