Iterating an equation through a range of values to satisfy a condition
17 次查看(过去 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
0 个评论
回答(2 个)
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)
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
X_array
Y_array
2 个评论
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!