# Iterating an equation through a range of values to satisfy a condition

12 vues (au cours des 30 derniers jours)
Mary Jane le 1 Nov 2022
Modifié(e) : Torsten le 1 Nov 2022
I have two equations, A and B, that utilize two variables, X and Y.
I aim to find the minimum value Y that makes it so that equation B is less than equation A. X must also be a value between 11 and 15. I also need to store the values of X and Y throughout each iteration. I am unsure how to use nested while loops to accomplish this, as well as how to store the values. I have tried other variations of this, the condition is always overriden. Thank you for the help!
X = 11.1; % Initiate the loop
while 11 < X < 15 % Needs to be a range of values from 11 to 15
diff = 1; % Initiate the loop, not sure about placement
while diff > 0 % Implies equation B < A
Y = 1; % Random initial value for the equations to run
% By manually typing in numbers until it works, 816 is desired value
V_i = 1; % Random initial value for the equations to run
A = (abs((sqrt((2*Y)/(1+Y))-1))+abs(sqrt(2/Y)*((sqrt((1)/(1+(Y/X))))-sqrt(1/(1+Y))))+abs(sqrt(1/X)*(sqrt((2*Y)/(X+Y))-1)))*V_i;
B = ((1-(1/X))*sqrt((2*X)/(1+X))+sqrt(1/X)-1)*V_i;
diff = A-B; % To calculate condition that must be met
Y = Y+1; % Y should increase by some amount (ex: 1) each time until condition is met
end
X = X+1
end
% Not sure how to store X and Y values through each iteration
##### 0 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (2)

KALYAN ACHARJYA le 1 Nov 2022
Modifié(e) : KALYAN ACHARJYA le 1 Nov 2022
Sufficint Hints:
X = 11:increment spacing:15; % Initiate the loop. check the increment spacing
Y = 1;
V_i = 1; % Random initial value for the equations to run
% Any A & B initial value
while B>A
A=
B=
Y=Y+1;
end
Y+1 % This Y+1 minimum vlaue B<A (Iteration number too)
% Be careful on typical values & conditions,
% so that no loop runs for infinite times
If you wish to store the A & B Value too, use the array A(i) & B(i)
##### 1 commentaireAfficher AucuneMasquer Aucune
Mary Jane le 1 Nov 2022
Thank you, I am still confused on how to then initiate the while loop if there is no B or A value specified before hand. Whenever I try and give an arbitrary value for A and B to initiate the while loop, it only iterates one more time and does not continue despite the condition still being true. For Y it only does the first step and does not continue the iterations, but for everything else, it will continue the entire loop.

Connectez-vous pour commenter.

Torsten le 1 Nov 2022
Modifié(e) : Torsten le 1 Nov 2022
count = 0;
for X = 11:15 % Needs to be a range of values from 11 to 15
count = count + 1;
X_array(count) = X;
diff = 1; % Initiate the loop, not sure about placement
Y = 0; % Random initial value for the equations to run
while diff > 0 % Implies equation B < A
Y = Y+1; % Y should increase by some amount (ex: 1) each time until condition is met
% By manually typing in numbers until it works, 816 is desired value
V_i = 1; % Random initial value for the equations to run
A = (abs((sqrt((2*Y)/(1+Y))-1))+abs(sqrt(2/Y)*((sqrt((1)/(1+(Y/X))))-sqrt(1/(1+Y))))+abs(sqrt(1/X)*(sqrt((2*Y)/(X+Y))-1)))*V_i
B = ((1-(1/X))*sqrt((2*X)/(1+X))+sqrt(1/X)-1)*V_i
diff = A-B % To calculate condition that must be met
end
Y_array(count) = Y;
end
A = 0.5324
B = 0.5324
diff = 0
A = 0.5342
B = 0.5342
diff = 0
A = 0.5353
B = 0.5353
diff = 0
A = 0.5359
B = 0.5359
diff = 0
A = 0.5362
B = 0.5362
diff = 0
X_array
X_array = 1×5
11 12 13 14 15
Y_array
Y_array = 1×5
1 1 1 1 1
##### 2 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Torsten le 1 Nov 2022
Modifié(e) : Torsten le 1 Nov 2022
Thanks, what is the -10 inside the parenthesis for?
X goes from 11 to 15, but in order to start the array index for X_array and Y_array with 1, you must subtract 10.
Maybe it's easier to use a counter ( see above).
When I do individual iterations, my Y value decreases as my X value increases. But in this instance, I get 2 for each iteration
Maybe you copied A and/or B wrong for use in this code.
As you can see, A and B are equal already for Y = 1 in all cases - thus the while loop is exited here.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by