How can I improve this really slow code, which consists of several nested for loops?

1 vue (au cours des 30 derniers jours)
Hi,
So I've written these nested for loops, but it takes a very long time to execute this code:
for T = linspace( 1, 5, 10 )
for xdot_0 = linspace( -3, 3, 10 )
for ydot_0 = linspace( 16, 20, 10 )
for thetadot_0 = linspace( -5, 0, 10 )
z = DifferenceMap( xdot_0, ydot_0, thetadot_0, T );
if norm(z) < 1e-3
disp( xdot_0, ydot_0, thetadot_0, T )
disp( z )
end
end
end
end
end
The DifferenceMap( ) function calls another function that gives solutions from the ode45 solver at final time, T.
Is there a better way to write this code so that it's faster?
Thanks,
  4 commentaires
Noob
Noob le 29 Sep 2020
Modifié(e) : Noob le 29 Sep 2020
Hi Sindar,
Should I do both -- use a parfor and look into paralleizing DifferenceMap -- or are there "diminishing returns", so that doing one of the two is the best I can hope for? (In any case, I'll start reading up on both suggestions.)
Thanks,
Sindar
Sindar le 29 Sep 2020
There are definitely diminishing returns. parfor is easy to implement, but if you figure out lower-level parallelization (esp. vectorization), it has a good chance to be more effective

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 29 Sep 2020
If you can, arrange DifferenceMap to accept a vector of T values. In the ode45() call, instead of passing in the two-element vector [0 T] for scalar T, pass in [0 vector_of_T] . ode45() will report back results at only those times. Discard the first row (corresponding to T = 0) and the rest of the rows hold the results required for each of the values in the vector of T values.
At present, your code is evaluating the same system of ode equations for a number of different T values, but at quite different times -- you do not do T = 1.4 until you have finished all of the T = 1 cases. But in order to simulate to T = 1.4, ode45 would have to integrate starting from scratch, from 0, re-doing the integrations done for time 0 to 1. And then eventually T would become 1.9 and ode45 would have to integrate the same system from scratch again, going all the way through starting from 0 to 1.4 .
My proposal is to do all the times in the same call for a given configuration, so that within the same call after making the prediction for T = 1, it would be able to use the data it already has in order to move on to T = 1.4, and through to T = 1.9 and so on, not needing to re-do the [0 1], [0 1.4] and so on integrations.
  3 commentaires
Walter Roberson
Walter Roberson le 29 Sep 2020
Right, you would remove the T loop.
You should let the solve use adaptive time steps.
Noob
Noob le 29 Sep 2020
Ok, I'll try this. Thanks Walter - have a great day / evening.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by