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

3 次查看(过去 30 天)
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by