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

4 vues (au cours des 30 derniers jours)
Udhaya K
Udhaya K le 13 Fév 2022
Commenté : VBBV le 17 Fév 2022
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?

Réponse acceptée

VBBV
VBBV le 13 Fév 2022
Modifié(e) : VBBV le 13 Fév 2022
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 commentaires
Udhaya K
Udhaya K le 16 Fév 2022
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 le 17 Fév 2022
if it worked, please accept the answer

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by