HP Forums
HP Prime Program using Solve function - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: HP Prime Program using Solve function (/thread-14453.html)



HP Prime Program using Solve function - Grayhek - 02-01-2020 06:43 PM

I am trying to write a program on my HP Prime to solve for Z but when I use the solve function it is only returning 0. When I do the same equation on the CAS it works but not in my actual program. This is my code:

Code:

export Tc, Pc, Tr, Pr;
export β,α,фi,fi;
export s,Є,Ω,ѱ;
EXPORT Fugacity()

BEGIN

local Zv,Zl,Iv,Il;
local w,c,q;

Ω:=.0778;
Є:=1-√2;
s:=1+√2;
ѱ:=.45724;

input({T,Tc,P,Pc});
input({w});
Tr:=T/Tc;
Pr:=P/Pc;
c:=1+(0.37464+1.54226*w-0.26992*w^(2));
α:=(c*(1-Tr^(1/2)))^2;

β:=(Ω*(Pr/Tr));
q:=ѱ*α/Tr/Ω;

//Z for vapor
solve(0:=1+β-q*β*((Z-β)/(Z+β*Є)/(Z+s*β)),Z,1);
 msgbox(Z); //testing to see if this solves for Z
Zv:=Z;

I cant seem to figure out how to get the solver function to work inside my program. If anyone knows how to fix this problem I would greatly appreciate the help.


RE: HP Prime Program using Solve function - Wes Loewer - 02-02-2020 10:11 AM

I see a few problems pertaining to the lines

Code:
solve(0:=1+β-q*β*((Z-β)/(Z+β*Є)/(Z+s*β)),Z,1);
msgbox(Z); //testing to see if this solves for Z

I assume "0:=" was a typo for "0=" (otherwise you are trying to assign a value to zero).

By using capital Z here you are inserting the value of the permanent global numeric variable Z, not the symbol Z. So if Z happened to be 5, then you are essentially typing:

Code:
solve(0:=1+β-q*β*((5-β)/(5+β*Є)/(5+s*β)),Z,1);

The solve command does not assign a value to the variable it is solving for. For example, in CAS, if you enter solve((x^2-3) = 0,x) it returns {{-sqrt(3),sqrt(3)} but does not assign this list to x. In your case, you might be tempted to use
Code:
Z:=solve(0:=1+β-q*β*((5-β)/(5+β*Є)/(5+s*β)),5,1);
But the global Z can only store real numbers, not lists. You could get around this problem by declaring Z local.

I assume there were no other lines intended before the final END.

Finally, I'm not familiar with fugacity, but
((Z-β)/(Z+β*Є)/(Z+s*β)) has redundant ()'s and is the same as
(Z-β)/((Z+β*Є)*(Z+s*β)). I thought I should ask if this really what you intended?

Hope this helps at least a little.


RE: HP Prime Program using Solve function - Grayhek - 02-03-2020 01:46 AM

Thank you for the reply. The "0:=" was a typo but I am not trying to assign a list to the Z variable. I am trying to solve for a numerical value of Z. I want the solve function to assign that value to Z. I am able to do is in the solve app if I insert the equation there then solve for Z but I want to be able to do that without having to leave the program. When I have run the program it will return a value of 0. the whole code is as follows:
Code:

export Tc, Pc, Tr, Pr;
export β,α,фi,fi;
export s,Є,Ω,ѱ;
EXPORT Pure_Fugacity()
BEGIN

local Zv,Zl,Iv,Il;
local w,c,q,Z;
Ω:=.0778;
Є:=1-√2;
s:=1+√2;
ѱ:=.45724;

input({T,Tc,P,Pc});
input({w});
Tr:=T/Tc;
Pr:=P/Pc;
c:=1+(0.37464+1.54226*w-0.26992*w^(2));
α:=(c*(1-Tr^(1/2)))^2;

β:=(Ω*(Pr/Tr));
q:=ѱ*α/Tr/Ω;

//Z for vapor
solve(0=1+β-q*β*(Z-β)/(Z+β*Є)/(Z+s*β),Z,1);
 Zv:=Z;
msgbox(Zv);

//Z for liquid
solve(0=1+β-q*β*(Z-β)/(Z+β*Є)/(Z+s*β),Z,β);
Zl:=Z; 

//I for vapor
Iv:=(1/(s-β))*ln((Zv+s*β)/(Zv+β*Є));

//I for liquid
Il:=(1/(s-β))*ln((Zl+s*β)/(Zl+β*Є));

фi:=e^(Zv-1-ln(Zv-β)-q*Iv);
fi:=фi*P;

print();
print("Tr="+Tr);
print("Pr="+Pr);
print("α="+α);
print("β="+β);
print("q="+q);
print("Zv="+Zv);
print("Lv="+Iv);
print("фi="+фi);
print("fi="+fi);


END;
This is the specific part that is the trouble. There are 3 roots and when there is an initial guess of 1 it will give the root for the vapor and when the initial guess is β it gives the root for the liquid.

Code:
//Z for vapor
solve(0=1+β-q*β*(Z-β)/(Z+β*Є)/(Z+s*β),Z,1);
 Zv:=Z;
msgbox(Zv);

//Z for liquid
solve(0=1+β-q*β*(Z-β)/(Z+β*Є)/(Z+s*β),Z,β);
Zl:=Z; 

//I for vapor
Iv:=(1/(s-β))*ln((Zv+s*β)/(Zv+β*Є));
I appreciate the help. I am new at this programming language and am trying to figure how it all works.


RE: HP Prime Program using Solve function - Albert Chan - 02-03-2020 05:09 PM

We had similar issues with limits, which got resolve with assume(¬x,symbol)

https://www.hpmuseum.org/forum/thread-11569-post-106011.html#pid106011

(02-03-2020 01:46 AM)Grayhek Wrote:  
Code:
//Z for vapor
solve(0=1+β-q*β*(Z-β)/(Z+β*Є)/(Z+s*β),Z,1);
 Zv:=Z;
msgbox(Zv);

//Z for liquid
solve(0=1+β-q*β*(Z-β)/(Z+β*Є)/(Z+s*β),Z,β);
Zl:=Z;

Looks like Z is just a quadratics.
You can provide Z quadratic coefficients, and use proot, solving both roots.


RE: HP Prime Program using Solve function - Grayhek - 02-03-2020 07:37 PM

This is a cubic equation on states. unfortunately it is not a quadratic equation but it is a cubic equation.


RE: HP Prime Program using Solve function - roadrunner - 02-04-2020 12:29 AM

Three comments:

1. From a program, solve() has to be sent to CAS via a string;
2. The variable you are solving for should not exist. In the edited program below I used lower case z. You have to use a variable that doesn't exist anywhere on your calculator;
3. Solve doesn't assign the answer to the variable, it just returns an answer; you have to assign that to a variable in your program.

With this in mind, here is a modified program:

Code:

export Tc, Pc, Tr, Pr;
export β,α,фi,fi;
export s,Є,Ω,ѱ;

EXPORT Pure_Fugacity()
BEGIN
 local Zv,Zl,Iv,Il;
 local w,c,q,Z;
 Ω:=.0778;
 Є:=1-√2;
 s:=1+√2;
 ѱ:=.45724;
// input({T,Tc,P,Pc});
// input({w});

T:=25;
Tc:=350;
P:=15;
Pc:=450;
w:=0.5;

 Tr:=T/Tc;
 Pr:=P/Pc;
 c:=1+(0.37464+1.54226*w-0.26992*w^(2));
 α:=(c*(1-Tr^(1/2)))^2;
 β:=(Ω*(Pr/Tr));
 q:=ѱ*α/Tr/Ω;
//Z for vapor
 Z:=CAS("solve(0=1+β-q*β*(z-β)/(z+β*Є)/(z+s*β),z,1)");
 Zv:=Z;
 msgbox(Zv);
//Z for liquid
 Z:=CAS("solve(0=1+β-q*β*(z-β)/(z+β*Є)/(z+s*β),z,β)");
 Zl:=Z;
//I for vapor
 Iv:=(1/(s-β))*ln((Zv+s*β)/(Zv+β*Є));
//I for liquid
 Il:=(1/(s-β))*ln((Zl+s*β)/(Zl+β*Є));
 фi:=e^(Zv-1-ln(Zv-β)-q*Iv);
 fi:=фi*P;
 print();
 print("Tr="+Tr);
 print("Pr="+Pr);
 print("α="+α);
 print("β="+β);
 print("q="+q);
 print("Zv="+Zv);
 print("Lv="+Iv);
 print("фi="+фi);
 print("fi="+fi);
END;

Hope this helps.

-road


RE: HP Prime Program using Solve function - Gene222 - 02-05-2020 04:52 AM

(02-04-2020 12:29 AM)roadrunner Wrote:  Three comments:

1. From a program, solve() has to be sent to CAS via a string;
2. The variable you are solving for should not exist. In the edited program below I used lower case z. You have to use a variable that doesn't exist anywhere on your calculator;
3. Solve doesn't assign the answer to the variable, it just returns an answer; you have to assign that to a variable in your program.

Can you tell us why in a program using fsolve, the variable you are solving for should not exist anywhere on the calculator?


RE: HP Prime Program using Solve function - cyrille de brébisson - 02-05-2020 06:14 AM

Hello,

Well, it is not a must, but it avoids unwanted evaluation which might happen sometimes, depending on the function.

Also, any program involving the cas would work better as a CAS native program rather than as a PPL to cas function call...

All UI works better in PPL, but if you have all your variables as CAS variable, you will save a lot of time in CAS to PPL transforms of all of this.

you can use the #cas #end syntax in your program to include a cas block...

Cyrille


RE: HP Prime Program using Solve function - Carlos295pz - 02-06-2020 04:59 AM

(02-05-2020 06:14 AM)cyrille de brébisson Wrote:  Also, any program involving the cas would work better as a CAS native program rather than as a PPL to cas function call...

All UI works better in PPL, but if you have all your variables as CAS variable, you will save a lot of time in CAS to PPL transforms of all of this.

you can use the #cas #end syntax in your program to include a cas block...

I am not a supporter of #cas #end, since if someone uses "restart" every program created there would be deleted and the source container should be recompiled. That for a common user may not be intuitive. "restart" is often used a lot to quickly solve the residual configurations and variables that many programs leave indiscriminately.

The alternative is that we can use local CAS variables, that is very good, but that there are no local CAS functions is problematic, maybe that is impossible perhaps, so I have always opted for the Home-CAS interaction, in 2017 it seemed to be standardized but it has been broken a few times and recently I have found that Home-CAS works differently in G2.


RE: HP Prime Program using Solve function - Grayhek - 02-20-2020 01:53 AM

The solution to this problem was actually simple. Once I typed "solve" in capital letters ("SOLVE") the CAS function worked properly.


RE: HP Prime Program using Solve function - cyrille de brébisson - 02-20-2020 06:03 AM

Hello,

The uppercase SOLVE function is normally the function app SOLVE function and uses the HP numerical solver.
the lowercase, cas, solve function uses the CAS which will first try sybolic solving before moving on to numerical methods.

Cyrille