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

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?

 Respuesta aceptada

VBBV
VBBV el 13 de Feb. de 2022
Editada: VBBV el 13 de Feb. de 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 comentarios

Udhaya K
Udhaya K el 14 de Feb. de 2022
Editada: Udhaya K el 14 de Feb. de 2022
Hi, I don't understand your solution. I only get "y" as zeroes.
What I expect instead is:
x = [0 0.03333 0.06666 0.10000 and so on for total 10 elements]
y = [25.8400 32.0300 36.19 and so on for all the x values]
VBBV
VBBV el 14 de Feb. de 2022
Modify the while condition accordingly. NB : see the edited answer
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 el 17 de Feb. de 2022
if it worked, please accept the answer

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Etiquetas

Preguntada:

el 13 de Feb. de 2022

Comentada:

el 17 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by