Iteration Involving 1x4 matrix

Hello... Would like to ask the proper procedure for iteration. Say instance below
Given P: Pgiven = [2.42 4.52 10.01 15.32]
rho=function of eta (1x4 matrix)
Z=function of eta and rho (1x4 matrix)
P=function of eta, rho and Z (1x4 matrix)-----> the P that should be calculated here needs to be equal the Pgiven
Step 1. Assumption of eta
Step 2. Calculation of rho
Step 3. Calculation of Z
Step 4. calculation of P---> end of iteration 1
What function in matlab would allow to automatically iterate until it reached an eta that provides the calculated P be equal to Pgiven?
Thank u so much!

 Accepted Answer

while abs(P - Pgiven) > 1e-6
...
end

6 Comments

Hello, thank you for this... In the case that while loop doesn't result to convergence, is there any other way or procedure for iteration on matlab? Thanks.
Yes, of course. As in other programming languages:
maxIter = 1e6;
iter = 0;
while abs(P - Pgiven) > 1e-6 && iter < maxIter
...
iter = iter + 1;
end
Or:
maxIter = 1e6;
converged = false;
for k = 1:maxIter
converged = (abs(P - Pgiven) <= 1e-6);
if converged
break;
end
end
Hello @Jan, thank you so much again for helping... In my below code (with hightlighted steps from my original question above, steps 1-4), I followed your advised procedure:
maxIter = 1e6;
iter = 0;
while abs(P - Pgiven) > 1e-6 && iter < maxIter
...
iter = iter + 1;
end
but when I ran it, I got a "Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values"
Please note that I have 2 while conditions-----> calculated Pcompliq and Pcompvap must be (almost) equal to Pgiven. Hoping for your help here.
Kind regards,
Pcompliq=1; %assumed only
Pcompvap=1; %assumed only
Pgiven=[2.42*10^6 4.29*10^6 10.01*10^6 12.21*10^6];
maxIter = 1e6;
iter = 0;
while abs(Pcompliq - Pgiven) > 1e-6 && abs(Pcompvap - Pgiven) > 1e-6 && iter < maxIter
iter = iter + 1;
end
Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values.
Jan
Jan on 19 Oct 2021
Edited: Jan on 19 Oct 2021
The error message means, that the operands abs(Pcompliq - Pgiven) > 1e-6 and abs(Pcompvap - Pgiven) > 1e-6 are not scalars. && needs scalar operands. What exactly is the criterion for convergence?
  • all(abs(Pcompliq - Pgiven) > 1e-6)
  • mean(abs(Pcompliq - Pgiven)) > 1e-6
  • sum(abs(Pcompliq - Pgiven)) > 1e-6
  • norm(abs(Pcompliq - Pgiven)) > 1e-6
All of these methods reply a scalar output and the && operator is working.
By the way: (10^10).^3 are two expensive power operations. 1e30 is a cheap constant.
a and b are constants. Then it saves time to define them once outside the loop.
Hi Jan, thank you so much for explaining and thank you so much for helping me, there really is so much to learn and am glad this community is in for some enlightenment. Now my code is proceeding to run... Have a good day!
You are welcome.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Asked:

on 17 Oct 2021

Commented:

Jan
on 20 Oct 2021

Community Treasure Hunt

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

Start Hunting!