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!

 채택된 답변

Jan
Jan 2021년 10월 17일

0 개 추천

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

댓글 수: 6

lvenG
lvenG 2021년 10월 18일
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
lvenG
lvenG 2021년 10월 19일
편집: lvenG 2021년 10월 20일
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 2021년 10월 19일
편집: Jan 2021년 10월 19일
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.
lvenG
lvenG 2021년 10월 20일
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!
Jan
Jan 2021년 10월 20일
You are welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Objects에 대해 자세히 알아보기

질문:

2021년 10월 17일

댓글:

Jan
2021년 10월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by