Need help , how to code if statement to redo equation if desired answer isnt achieved?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hello!
I am currently trying to code the following. The problem is I want the equation to update and re-run with a new Z value if it doesnt fit the conditions and only display the correct answer.
For example, this code currently works out the answer to the first C(n) line and uses that through all the if statements but I want it to work out the answer, then say if the answer is 120 I want the equation to rerun with the value now using Z(n) instead of Z(100) and so forth down.
Any help would be appreciated!
Thanks
C(n) = ((((500*V1/Z(500)) + ((Ps*Vc)/Zc)))*Z(100))/ (V1+Vc)
if C(n) < 100
disp('Error, configuration not possible.')
elseif C(n) > 100 && C(n) < 125, Z(n) = Z(100)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 125 && C(n) < 150, Z(n) = Z(125)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 150 && C(n) < 175, Z(n) = Z(150)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 175 && C(n) < 200, Z(n) = Z(175)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 200 && C(n) < 225, Z(n) = Z(200)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 225 && C(n) < 250, Z(n) = Z(225)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 250 && C(n) < 275, Z(n) = Z(250)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 275 && C(n) < 300, Z(n) = Z(275)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 300 && C(n) < 325, Z(n) = Z(300)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 325 && C(n) < 350, Z(n) = Z(325)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 350 && C(n) < 375, Z(n) = Z(350)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 375 && C(n) < 400, Z(n) = Z(375)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 400 && C(n) < 425, Z(n) = Z(400)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
elseif C(n) > 425 && C(n) < 450, Z(n) = Z(450)
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
end
3 comentarios
Walter Roberson
el 1 de Mzo. de 2023
In your assignments to C(n), in every case the assignment is exactly
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc)
If you had some code the figured out the appropriate Z(n) value, you could structure your code along the lines of
if C(n) is in range
bin = floor(C(n) / 25) * 25;
Z(n) = Z(bin);
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc);
end
Except that you need to examine the boundary conditions for C(n) being exactly 125 or exactly 150, and so on.
Likewise for your assignments to C(n+1), in every case the assignment is exactly
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
and the breakpoints are the same, so the outline would be
if C(n+1) is in range
bin = floor(C(n+1) / 25) * 25;
Z(n) = Z(bin);
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
end
and so on.
Respuestas (1)
Cameron
el 27 de Feb. de 2023
Editada: Cameron
el 27 de Feb. de 2023
What is n? I have a feeling this isn't correct because of the way you set up your equation.
C(n) = ((((500*V1/Z(500)) + ((Ps*Vc)/Zc)))*Z(100))/ (V1+Vc);
prevAns = C(n);
check = 0;
while check == 0
if C(n) < 100
disp('Error, configuration not possible.')
elseif C(n) >= 100 && C(n) < 125
Z(n) = Z(100);
elseif C(n) >= 125 && C(n) < 150
Z(n) = Z(125);
elseif C(n) >= 150 && C(n) < 175
Z(n) = Z(150);
elseif C(n) >= 175 && C(n) < 200,
Z(n) = Z(175);
elseif C(n) >= 200 && C(n) < 225,
Z(n) = Z(200);
elseif C(n) >= 225 && C(n) < 250
Z(n) = Z(225) ;
elseif C(n) >= 250 && C(n) < 275
Z(n) = Z(250);
elseif C(n) >= 275 && C(n) < 300,
Z(n) = Z(275);
elseif C(n) >= 300 && C(n) < 325
Z(n) = Z(300);
elseif C(n) >= 325 && C(n) < 350
Z(n) = Z(325);
elseif C(n) >= 350 && C(n) < 375
Z(n) = Z(350);
elseif C(n) >= 375 && C(n) < 400
Z(n) = Z(375);
elseif C(n) >= 400 && C(n) < 425
Z(n) = Z(400);
elseif C(n) >= 425 && C(n) < 450
Z(n) = Z(450) ;
end
C(n) = ((((500*V1/Z(500)) + ((P0*Vc)/Zc)))*Z(n))/ (V1+Vc);
if prevAns == C(n)
check = 1;
else
prevAns = C(n);
end
end
disp(C(n))
2 comentarios
Ver también
Categorías
Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!