How to run a while loop for each element in a row matrix?

4 ビュー (過去 30 日間)
Udhaya K
Udhaya K 2022 年 2 月 13 日
コメント済み: VBBV 2022 年 2 月 17 日
With my very basic understanding of matlab scripts, I wrote the following one to perform an iterative calculation to find x:
inv_alpha = .32
x = 0
while y < round (inv_alpha, 4)
x = x+.01
y =(round (tand (x) - deg2rad (x), 4))
end
disp (x)
disp (y)
This ran quite exactly as I expected. But now I want to run this for an array of the variable "inv_alpha", for example:
inv_alpha = linspace (0, 0.3, 10)
Therefore I expect to see 10 output values for "y". Can someone pl. help what changes I should do to the script?

採用された回答

VBBV
VBBV 2022 年 2 月 13 日
編集済み: VBBV 2022 年 2 月 13 日
inv_alpha = linspace (0, 0.3, 10)
inv_alpha = 1×10
0 0.0333 0.0667 0.1000 0.1333 0.1667 0.2000 0.2333 0.2667 0.3000
x = 0
x = 0
y = zeros(size(inv_alpha))
y = 1×10
0 0 0 0 0 0 0 0 0 0
for k = 1:length(inv_alpha)
if y(k) < round (inv_alpha(k), 4)
x = x +0.01
end
y(k) =(round (tan (x) - deg2rad (x), 4))
% k = k+1;
end
y = 1×10
0 0 0 0 0 0 0 0 0 0
x = 0.0100
y = 1×10
0 0.0098 0 0 0 0 0 0 0 0
x = 0.0200
y = 1×10
0 0.0098 0.0197 0 0 0 0 0 0 0
x = 0.0300
y = 1×10
0 0.0098 0.0197 0.0295 0 0 0 0 0 0
x = 0.0400
y = 1×10
0 0.0098 0.0197 0.0295 0.0393 0 0 0 0 0
x = 0.0500
y = 1×10
0 0.0098 0.0197 0.0295 0.0393 0.0492 0 0 0 0
x = 0.0600
y = 1×10
0 0.0098 0.0197 0.0295 0.0393 0.0492 0.0590 0 0 0
x = 0.0700
y = 1×10
0 0.0098 0.0197 0.0295 0.0393 0.0492 0.0590 0.0689 0 0
x = 0.0800
y = 1×10
0 0.0098 0.0197 0.0295 0.0393 0.0492 0.0590 0.0689 0.0788 0
x = 0.0900
y = 1×10
0 0.0098 0.0197 0.0295 0.0393 0.0492 0.0590 0.0689 0.0788 0.0887
disp (x)
0.0900
disp (y)
0 0.0098 0.0197 0.0295 0.0393 0.0492 0.0590 0.0689 0.0788 0.0887
  4 件のコメント
Udhaya K
Udhaya K 2022 年 2 月 16 日
Hi,
It worked perfectly. I changed a bit of the programme so that I get same number of x values as the number of "inv_alpha" values:
inv_alpha =[0 0.3 0.35]
x = zeros(size(inv_alpha))
y = zeros(size(inv_alpha))
for k=1:length(inv_alpha)
while y(k) < round (inv_alpha(k), 4)
x(k) = x(k)+.01
y(k) =(round (tand (x(k)) - deg2rad (x(k)), 4))
end
end
disp (x)
disp (y)
disp (k)
Thank you so much for helping!
VBBV
VBBV 2022 年 2 月 17 日
if it worked, please accept the answer

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by