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

조회 수: 1 (최근 30일)
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
Noob
Noob 2020년 9월 29일
편집: Noob 2020년 9월 29일
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 2020년 9월 29일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 9월 29일
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
Walter Roberson
Walter Roberson 2020년 9월 29일
Right, you would remove the T loop.
You should let the solve use adaptive time steps.
Noob
Noob 2020년 9월 29일
Ok, I'll try this. Thanks Walter - have a great day / evening.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by