I am trying to solve some equations using a while loop. I have a known and fixed output. Workflow is as follows:
P = 100; %Desired output
x = 1; %Initial guess
while abs(something) > 1e-6
x1 = (25 * x)/2
x2 = 10x - x1
x3 = 20x - (x - x1 - x2)*2
x4 = (x - x1 - x2 -x3)*12
x5 = (x - x1 - x2 -x3) * 10 + x4
P1 = 2005x3
P2 = 1500x5
Pnew = P1 + P2
end
I was hoping to calculate x1, x2, ..., x5 and stop looping when the condition P = Pnew is reached. Due to my modest knowledge of MATLAB any help would be appreciated.
Many thanks in advance.
APPENDIX: Perhaps i didn't explained well. My goal was to stop looping when the condition P = Pnew is achieved. I have a known value P and initial value of x. Pnew should be generated through a given series of equations. When the condition is met x, x1, x2, ..., x5 will have their values. So x is not only the initial value for calculating other unknowns (x1, x2, ..., x5). I tried to modify it but got NaN Pnew, P1, error and inf for P2, x, ...
P = 100; %Desired output
x = 1; %Initial guess
Pnew = P + 1
while abs(P - Pnew) > 1e-6
x1 = (25 * x)/2;
x2 = 10*x - x1;
x3 = 20*x - (x - x1 - x2)*2;
x4 = (x - x1 - x2 -x3)*12;
x5 = (x - x1 - x2 -x3) * 10 + x4
P1 = 2005 * x3;
P2 = 150 * x5;
Pnew = P1 - P2;
error = abs(P - Pnew);
x_new = x - .001 * error;
x = x_new;
end
somethingis. Please clarify if my answer is the issue you're getting. If not, please clarify what your exact issue is.