Replacing for loops with vectorization

21 ビュー (過去 30 日間)
Kyle Baldwin
Kyle Baldwin 2021 年 2 月 19 日
コメント済み: Kyle Baldwin 2021 年 2 月 21 日
Questions similar to this have been asked before, but I've struggled to understand the answers. I am looking to speed up my Matlab programs by avoiding for loops. I have seen the remark that Matlab is a high level programming language and so things like for loops shouldn't be necessary, but if anyone could help me figure out how to avoid them in my simple example I would very much appreciate it.
The following script simulates the motion of a pendulum quite well:
length=0.1;
g=9.81;
omega(1)=0;
theta(1)=2;
t(1)=0;
timeStep = 0.01;
t = t(1):timeStep:t(1)+(timeStep*9999);
for i=2:10000
omega(i) = omega(i-1) + (g/length)*sin(theta(i-1))*timeStep;
theta(i) = theta(i-1)+omega(i)*timeStep;
end
plot(t,theta);
xlabel('Time (s)');
ylabel('Theta (rad)');
I have figured out how to vectorize t, shown before the start of the for loop. The issue is that omega and theta are both part of two simultaneous equations, where the next value of theta needs to be calculated based on the previous value of omega, and the next value of omega needs to be calculated based on the previous value of theta.
Is it possible to do this to use vectorization instead of a for loop?
Thank you,
Kyle
P.S. This is for teaching, my students will appreciate anything that speeds up their work!
  5 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 2 月 19 日
編集済み: KALYAN ACHARJYA 2021 年 2 月 19 日
@Stephen Cobeldick Finally I have confirmed in this case. Sir, thanks for your valuable advices.
Kyle Baldwin
Kyle Baldwin 2021 年 2 月 19 日
@Stephen Cobeldick Thank you Stephen. I must admit that I was surprised when I kept seeing this advice, but I am still fairly new to this language, so I assumed it must be correct. I'll try and keep this in mind next time I can't decide whether a loop or vectors are the way to go.

サインインしてコメントする。

採用された回答

James Tursa
James Tursa 2021 年 2 月 20 日
編集済み: James Tursa 2021 年 2 月 20 日
No, in general you cannot vectorize loops such as this. What you are doing in this particular loop is solving a 2nd order differential equation numerically. At each step, the derivative depends on the current state. Thus, you have to iterate through the time steps to integrate from state to state. Since you don't know all of these states ahead of time, you can't vectorize the results with some type of matrix calculation. You will need to have the loop as you are currently doing. Even if you were to call a function such as ode45( ) for this, there would still be iteration loops embedded in ode45( ) to calculate the result.
  1 件のコメント
Kyle Baldwin
Kyle Baldwin 2021 年 2 月 21 日
Thank you. I guess I let the general advice that for loops should never be used in Matlab convince me that there must be a way to vectorize it! Your answer makes sense.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by