Post Reply 
[BASIC HELP] CAS.fsolve problem [Solved]
03-22-2019, 01:17 PM (This post was last modified: 03-23-2019 08:09 PM by Ziz.)
Post: #1
[BASIC HELP] CAS.fsolve problem [Solved]
Hello,

I'm trying to do a little program to solve an equation but the fsolve returns an error.

Pws(T)
BEGIN
T:=T+273.15;
LOCAL C1,C2,C3,C4,C5,C6,C7;
LOCAL Ps,P;

C1:=-5.6745359*10^3;
C2 := 6.3925247*10;
C3 := −9.6778430/10^(3);
C4 := 6.2215701/10^(7);
C5 := 2.0747825/10^(9);
C6 := −9.4840240/10^(13);
C7 := 4.1635019*10;
Ps:=CAS.fsolve(C1/T+C2+C3*T+C4*T^2+C5*T^3+C6*T^4+C7*ln(T)=ln(P),P,1000);
PRINT("Ps="+Ps);
RETURN Ps;
END;
Find all posts by this user
Quote this message in a reply
03-22-2019, 04:09 PM (This post was last modified: 03-22-2019 04:18 PM by roadrunner.)
Post: #2
RE: [BASIC HELP] CAS.fsolve problem
You have to send CAS commands as a string and then EVAL the string and the variable you are solving for can't be local. Make those two changes and you get:

PHP Code:
EXPORT Pws(T)
BEGIN
 T
:=T+273.15;
 
LOCAL C1,C2,C3,C4,C5,C6,C7;
 
LOCAL Ps;

 
C1:=-5.6745359*10^3;
 
C2 := 6.3925247*10;
 
C3 := −9.6778430/10^(3);
 
C4 := 6.2215701/10^(7);
 
C5 := 2.0747825/10^(9);
 
C6 := −9.4840240/10^(13);
 
C7 := 4.1635019*10;
 
Ps:=CAS.EVAL("fsolve(C1/T+C2+C3*T+C4*T^2+C5*T^3+C6*T^4+C7*ln(T)=ln(P),P,1000)");
 PRINT(
"Ps="+Ps);
 RETURN 
Ps;
END

which returns an answer.


edit: Hmm, it appears you dont' have to EVAL the string because this:

Ps:=CAS("fsolve(C1/T+C2+C3*T+C4*T^2+C5*T^3+C6*T^4+C7*ln(T)=ln(P),P,1000)");

also works.

-road
Find all posts by this user
Quote this message in a reply
03-22-2019, 04:30 PM (This post was last modified: 03-22-2019 04:31 PM by Helge Gabert.)
Post: #3
RE: [BASIC HELP] CAS.fsolve problem
Either that, or write it as a CAS program to begin with. I started doing the latter for most of my programs, as the back and forth CAS conversions in a regular program just became too cumbersome and error-prone. There may be a downside to that (speed? - - are CAS programs slower than Home programs?), but it sure is convenient!

In your example, you could have perhaps also used FNROOT() without leaving the Home environment.
Find all posts by this user
Quote this message in a reply
03-22-2019, 04:35 PM
Post: #4
RE: [BASIC HELP] CAS.fsolve problem
I see that Road has a workable program, and I was just about to post the same thing. The big issues are:

1. EXPORT the program.
2. T and P are RESERVED variables, so don't make a local variable P.
3. Use a string inside the CAS command.

Here is what I ended up with: Pws(5) ==> 3.45704603787ᴇ119

Code:

EXPORT Pws(T)
BEGIN

  LOCAL C1,C2,C3,C4,C5,C6,C7;
  LOCAL Ps;  //  Omitted Var P, as it is a reserved variable

  T:=T+273.15;  // T is reserved variable also

  C1:=-5.6745359*10^3;
  C2 := 6.3925247*10;
  C3 := −9.6778430/10^(3);
  C4 := 6.2215701/10^(7);
  C5 := 2.0747825/10^(9);
  C6 := −9.4840240/10^(13);
  C7 := 4.1635019*10;

  Ps:=CAS.fsolve("C1/T+C2+C3*T+C4*T^2+C5*T^3+C6*T^4+C7*ln(T)=ln(P),P,1000"); // Note:  Usage of double quotes in CAS command

// PRINT("Ps="+Ps);

  RETURN Ps;
END;
Find all posts by this user
Quote this message in a reply
03-23-2019, 08:04 PM
Post: #5
RE: [BASIC HELP] CAS.fsolve problem
Thank you! This clarifies the things.

(03-22-2019 04:35 PM)DrD Wrote:  I see that Road has a workable program, and I was just about to post the same thing. The big issues are:

1. EXPORT the program.
2. T and P are RESERVED variables, so don't make a local variable P.
3. Use a string inside the CAS command.

Here is what I ended up with: Pws(5) ==> 3.45704603787ᴇ119

Code:

EXPORT Pws(T)
BEGIN

  LOCAL C1,C2,C3,C4,C5,C6,C7;
  LOCAL Ps;  //  Omitted Var P, as it is a reserved variable

  T:=T+273.15;  // T is reserved variable also

  C1:=-5.6745359*10^3;
  C2 := 6.3925247*10;
  C3 := −9.6778430/10^(3);
  C4 := 6.2215701/10^(7);
  C5 := 2.0747825/10^(9);
  C6 := −9.4840240/10^(13);
  C7 := 4.1635019*10;

  Ps:=CAS.fsolve("C1/T+C2+C3*T+C4*T^2+C5*T^3+C6*T^4+C7*ln(T)=ln(P),P,1000"); // Note:  Usage of double quotes in CAS command

// PRINT("Ps="+Ps);

  RETURN Ps;
END;
Find all posts by this user
Quote this message in a reply
03-23-2019, 08:08 PM
Post: #6
RE: [BASIC HELP] CAS.fsolve problem
Thank you! You solved my problem. I spent some time trying to make the code work.


(03-22-2019 04:09 PM)roadrunner Wrote:  You have to send CAS commands as a string and then EVAL the string and the variable you are solving for can't be local. Make those two changes and you get:

PHP Code:
EXPORT Pws(T)
BEGIN
 T
:=T+273.15;
 
LOCAL C1,C2,C3,C4,C5,C6,C7;
 
LOCAL Ps;

 
C1:=-5.6745359*10^3;
 
C2 := 6.3925247*10;
 
C3 := −9.6778430/10^(3);
 
C4 := 6.2215701/10^(7);
 
C5 := 2.0747825/10^(9);
 
C6 := −9.4840240/10^(13);
 
C7 := 4.1635019*10;
 
Ps:=CAS.EVAL("fsolve(C1/T+C2+C3*T+C4*T^2+C5*T^3+C6*T^4+C7*ln(T)=ln(P),P,1000)");
 PRINT(
"Ps="+Ps);
 RETURN 
Ps;
END

which returns an answer.


edit: Hmm, it appears you dont' have to EVAL the string because this:

Ps:=CAS("fsolve(C1/T+C2+C3*T+C4*T^2+C5*T^3+C6*T^4+C7*ln(T)=ln(P),P,1000)");

also works.

-road
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: