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 CenterFile Exchange에서 Loops 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