Need help , how to code if statement to redo equation if desired answer isnt achieved?

1 view (last 30 days)
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 Comments
lily
lily on 1 Mar 2023
The C(n)s change for C(n+1) and C(n+2) and use the answers for the previous , ive included code for these belows , so don’t know how to make the code include this
Walter Roberson
Walter Roberson on 1 Mar 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.

Sign in to comment.

Answers (1)

Cameron
Cameron on 27 Feb 2023
Edited: Cameron on 27 Feb 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 Comments
lily
lily on 28 Feb 2023
N is just representing one as there is c(n+1) etc etc other equations. The other equations follow on with the answer to the previous ones. Hope this helps you! Thanks for the response
if C(n+1) < 100
disp('Error, configuration not possible.')
elseif C(n+1) > 100 && C(n+1) < 125, Z(n+1) = Z(100)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 125 && C(n+1) < 150, Z(n+1) = Z(125)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 150 && C(n+1) < 175, Z(n+1) = Z(150)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 175 && C(n+1) < 200, Z(n+1) = Z(175)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 200 && C(n+1) < 225, Z(n+1) = Z(200)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 225 && C(n+1) < 250, Z(n+1) = Z(225)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 250 && C(n+1) < 275, Z(n+1) = Z(250)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 275 && C(n+1) < 300, Z(n+1) = Z(275)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 300 && C(n+1) < 325, Z(n+1) = Z(300)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 325 && C(n+1) < 350, Z(n+1) = Z(325)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 350 && C(n+1) < 375, Z(n+1) = Z(350)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 375 && C(n+1) < 400, Z(n+1) = Z(375)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 400 && C(n+1) < 425, Z(n+1) = Z(400)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
elseif C(n+1) > 425 && C(n+1) < 450, Z(n+1) = Z(450)
C(n+1) = ((((500*V2/Z(500)) + ((C(n)*Vc)/Z(n))))*Z(n+1))/ (V2+Vc)
end
% Cascade 3
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+1))/ (V3+Vc)
if C(n+2) < 100
disp('Error, configuration not possible.')
elseif C(n+2) > 100 && C(n+2) < 125, Z(n+2) = Z(100)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 125 && C(n+2) < 150, Z(n+2) = Z(125)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 150 && C(n+2) < 175, Z(n+2) = Z(150)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 175 && C(n+2) < 200, Z(n+2) = Z(175)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 200 && C(n+2) < 225, Z(n+2) = Z(200)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 225 && C(n+2) < 250, Z(n+2) = Z(225)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 250 && C(n+2) < 275, Z(n+2) = Z(250)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 275 && C(n+2) < 300, Z(n+2) = Z(275)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 300 && C(n+2) < 325, Z(n+2) = Z(300)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 325 && C(n+2) < 350, Z(n+2) = Z(325)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 350 && C(n+2) < 375, Z(n+2) = Z(350)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 375 && C(n+2) < 400, Z(n+2) = Z(375)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 400 && C(n+2) < 425, Z(n+2) = Z(400)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
elseif C(n+2) > 425 && C(n+2) < 450, Z(n+2) = Z(450)
C(n+2) = ((((500*V3/Z(500)) + ((C(n+1)*Vc)/Z(n+1))))*Z(n+2))/ (V3+Vc)
end
lily
lily on 28 Feb 2023
So instead of just having the Z value change each time based on the first equation, I need to first equation to give me an answer, and then depending on what the answer is use a different z value.
This is a bit hard to explain but for example C(n) could give me a value of 220. But If I use the value of Z(200) due to this in the C(n) equation I then get a value of 240 and then would want to use the value of Z as Z(225) as this is the clostest to 240 in 25 intervals, so I need to equation to redo each time with the different Z value to determine which Z value to use.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!