Creating a loop and loop variable

Hi guys,
How would I write a for loop in which the loop variable runs from 0 to pi/2 in steps of 0.01?
Am i right in saying:
for
A = 0:0.01:pi/2
end
Is this correct?

 採用された回答

Yusuf Suer Erdem
Yusuf Suer Erdem 2021 年 12 月 7 日
編集済み: Yusuf Suer Erdem 2021 年 12 月 7 日

2 投票

for a=0:a+0.01:pi/2
disp(a)
end

4 件のコメント

Laura T
Laura T 2021 年 12 月 7 日
Thank you! And if i were to add inside this loop do i need to disp(i) every time i enter a new line of code?
Yusuf Suer Erdem
Yusuf Suer Erdem 2021 年 12 月 7 日
disp(i) is just to show you that the loop works. you could do anything you want with that i value.
Stephen23
Stephen23 2021 年 12 月 7 日
The colon operation given in the answer uses the imaginary unit in the step, which leads to this warning:
0:i+0.01:pi/2
Warning: Colon operands must be real scalars.
ans = 1×158
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900
I don't see any reason why the step needs to use the imaginary unit.
Yusuf Suer Erdem
Yusuf Suer Erdem 2021 年 12 月 7 日
maybe used i as a character. i changed it with a. it should be okey now.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by