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

16 次查看(过去 30 天)
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

回答(2 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2022-11-1
编辑:KALYAN ACHARJYA 2022-11-1
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 个评论
Mary Jane
Mary Jane 2022-11-1
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.

请先登录,再进行评论。


Torsten
Torsten 2022-11-1
编辑:Torsten 2022-11-1
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 个评论
Mary Jane
Mary Jane 2022-11-1
Thanks, what is the -10 inside the parenthesis for?
When I do individual iterations, my Y value decreases as my X value increases. But in this instance, I get 2 for each iteration
Torsten
Torsten 2022-11-1
编辑:Torsten 2022-11-1
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by