How to make a loop until the same result is obtained

Hello,
I need help how to be used correctly in the loop, I have here a parameter table.
Example: I have k that place in the equation so i get d=23.83 and cheak if that no same ,continue until is same Out=d.
Thanks for the helpers
for k=[2.73 2.56 2.47 2.42 2.39]
d=130-(K*(350/9));
if d/130==[0.1 0.18 0.23 0.26 0.28]
Out=d
end
end

8 commentaires

d/130==[0.1 0.18 0.23 0.26 0.28]
The above line is not correct...
Can please help me how it works
d/130==[0.1 0.18 0.23 0.26 0.28]
The above will give you logical indices...The exact question what you asked is not clear.
I meant that d/130==23.83/130=0.18 no same 0.10
d/130==30.44/130=0.23 no same 0.18
.
.
.
Until it's the same
d/130==37.06/130=0.28 same 0.28
Out=d
Hope this is clear :)
for k=[2.73 2.56 2.47 2.42 2.39]
d=130-(k*(350/9)) ;
d/130
if any(d/130==[0.1 0.18 0.23 0.26 0.28])
Out=d
end
end
ans = 0.1833
ans = 0.2342
ans = 0.2611
ans = 0.2761
ans = 0.2850
The values are close to two decimals...what to you want?
yes that's what I meant.
To make that compare d/130 to [0.1 0.18 0.23 0.26 0.28] until have similar number.
Example the result 0.2850=0.28 ( two decimals) is same to 0.28 so i want that output will Out=d=37.06
Rik
Rik le 30 Nov 2020
So essentially you want to round to 2 decimals? Or do you have a variable number of digits you want to round to? And do you actually want to round the values, or just display them with 2 digits?
yes to round the values so
ans = 0.1833=0.18 compare to 0.1
ans = 0.2342=0.23 compare to 0.18
ans = 0.2611=0.26 compare to 0.23
ans = 0.2761=0.27 compare to 0.26
ans = 0.2850=0.28 compare to 0.28 => same number => Out=d=37.06

Connectez-vous pour commenter.

 Réponse acceptée

Would that do it?
Out = 0;
for k=[2.73 2.56 2.47 2.42 2.39]
d=round((130-(k*350/9))/130,2);
if ismember(d, [0.1 0.18 0.23 0.26 0.28])
Out=d;
end
end
fprintf('Out=d=%3.2f\n',Out)

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by