HP Forums
Program code with 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: Program code with solve function (/thread-21917.html)



Program code with solve function - SoxxoZ - 06-19-2024 02:23 PM

Hi, sorry but I need help
I'm able to create little program for my utility, but I can't use solve. I have tried all type (solve, fsolve, CAS.solve, zeros) and many modifications of format.
Can you share with me a little portion of code with a solve function?

For exampio this doesn't work:
Code:


EXPORT SOL()
BEGIN
  LOCAL S;

 S := CAS.solve('x^2+3*x-4=0', x, 1);


  PRINT("Solution for x: ");
  PRINT(S);

END;

Thank you guys


RE: Program code with solve function - gehakte_bits - 06-20-2024 10:04 PM

Here are some examples. In your example you have to use a 'HOME' variable like 'X', and not 'x'

Code:

rooty();
EXPORT rooty()
BEGIN
LOCAL y1=0;

// working SOLVES  
//CAS
y1:=CAS("solve(X^2=2 , X,1)"); 
y1:=fsolve(x^2-2,x);

// variable as a function (in CAS)
LOCAL ss1:="x^2-2";
y1:= CAS.solve(EVAL(ss1) , x,1);
y1:=CAS.fsolve(EVAL(ss1),x,1);

//HOME FNROOT part of Solve App
y1:=FNROOT(X^2-2,X,300); // or
y1:=FNROOT(X^2-2=0,X,300); 
y1:=FNROOT({'X^2-2'},{'X'},{300}); 

// use Function ROOT 
y1:= Function.ROOT( X^2-2, 300);

F1:='X^2-4';
y1:= Function.ROOT( F1, 3);

//use Solve.Solve 
y1:= Solve.SOLVE(X^2-2, X);

E0:="x^2-2";
y1:= Solve.SOLVE(E0, x);
END;



RE: Program code with solve function - gehakte_bits - 06-21-2024 02:13 PM

FYI if you predefine 'x' in CAS mode, (i.e. x=1 in CAS),
your solution should work, or add a 'LOCAL x' to the program)

(I had 'x' define in CAS already)


RE: Program code with solve function - Gene222 - 06-25-2024 08:17 PM

Last edited 10/29/2024.

When using solve on the CAS screen, one can input ordinary or symbolic variables and expressions without using quotes. When using solve in a program, the equation or expression needs to be enclosed in quotes. Unfortunately, it is not clear on how to use the quotes. Using solve can be frustrating at times. This post was modified over the past month to show how to use the solve function, and less on the why aspect. The following examples were run using software version 2.1.14730 (2023 04 13). Some of these examples may not be the recommended way of using the solve function, and the terminology may not be correct.

The solve function has three solve methods: symbolic (CAS), iterative, and bisection methods. The solve method is specified by the way the solve variable is specified, as shown in the example below.

When using the solve function in a program, the variable to be solved can be: 1) a local variable created outside of the procedural function (local program variable), 2) export variable, 3) non-existent variable (an undeclared and undefined variable used by the solve function), or 4) real varable (global system variable). See HP Prime variables and their priorities and HP Prime CAS Programming.

When the solve variable was a local variable created within the procedural function (local function variable) or function argument (function parameter variable), the program seemed to always return { } or an error message. At least, it did for the equations that I used on a hardware A calculator.

If one does not have a good understanding of the equation being solved, it may be better to use a solve variable that "does not exist", as demonstrated in the examples at the end of this post. Further, it might be helpful, if the equation and known variables are in the exact form. Your program may work differently on the Home and CAS screens, so it may be benificial to test your program on both screens. For symbolic calculations, when the "use i" CAS setting was checked, there were some equations that could hang the program, where the calculator had to be cleared with a paper clip on a hardware A calculator. In those cases, it was better to use the "complex" setting. But in general, it is suggested to use the default CAS settings. Finally, you may want to try running your program in radian mode.

Examples using the equation y = 3x^2 - 2x - 4. Given y = 1.5, find x.

1. Method 1 double quotes

In Method 1 double quotes, all of the solve arguments are enclosed in one set of double qoutes.

For this demonstration, the solve variable (noX) does not exist, meaning the solve variable was not declared or defined within the program, and was not defined as a CAS or user variable.

Code:
LOCAL gY,gAns;
EXPORT rooty1()
BEGIN
  gY:=1.5;
  gAns := CAS.solve("gY = 3*noX^2 - 2*noX - 4, noX");  // symbolic solve method
  PRINT(gAns);

  gY:=1.5;
  gAns := CAS.solve("gY = 3*noX^2 - 2*noX - 4, noX=1");  // Newton or secant method
  PRINT(gAns);

  gY:=1.5;
  gAns := CAS.solve("gY = 3*noX^2 - 2*noX - 4, noX=-5..5");  // bisection method
  PRINT(gAns);
END;

2. Method 2 double quotes

In Method 2 double quotes, each of the individual solve arguments are enclosed in double qoutes.

For this demonstration, the solve variable (X) is a real variable. Real variables are predefined by the calculator. It is important that variable X is not declared in the LOCAL statement.

Code:
LOCAL gY,gAns;
EXPORT rooty2()
BEGIN
  gY:=1.5;
  gAns := CAS.solve("gY = 3*X^2 - 2*X - 4", "X");
  PRINT(gAns);

  gY:=1.5;
  gAns := CAS.solve("gY = 3*X^2 - 2*X - 4", "X=1");
  PRINT(gAns);

  gY:=1.5;
  gAns := CAS.solve("gY = 3*X^2 - 2*X - 4", "X=-5..5");
  PRINT(gAns);
END;

3. Method 2 double quotes, where the equation is stored in a string variable (TYPE 2 object)

For this demonstration, the solve variable (gX) is a local variable created outside the procedural function. In the CAS.solve statement, EVAL(str1) is not enclosed in double quotes.

Code:
LOCAL gX,gY,gAns,str1;
EXPORT rooty3()
BEGIN
  gY:=1.5;
  str1 := "gY = 3*gX^2 - 2*gX - 4";
  gAns := CAS.solve(EVAL(str1), "gX");
  PRINT(gAns);

  gY:=1.5;
  str1 := "gY = 3*gX^2 - 2*gX - 4";
  gAns := CAS.solve(EVAL(str1), "gX=1");
  PRINT(gAns);

  gY:=1.5;
  str1 := "gY = 3*gX^2 - 2*gX - 4";
  gAns := CAS.solve(EVAL(str1), "gX=-5..5");
  PRINT(gAns);
END;

4. Method 2 double quotes, where the equation is stored in a function variable (TYPE 8 object)

For this demonstration, the solve variable (gX) is a local variable created outside the procedural function. When storing the equation in a function variable, the solve variable cannot be a variable that "does not exists". Using this variable as the solve variable would generate a syntax error.

The equation is stored in variable eq1, which is a function variable. In the equation, the solve variable (gX) is enclosed in single quotes. When using single quotes, any value stored in gX is ignored. In the CAS.solve statement, eq1 is not enclosed with double quotes.

On the Home screen, program 1 returns the correct answer of {-1.0611, 1.7278}. On the CAS screen, program 1 prints a warning message, "Warning, argument is not an equation, solving (1.5==(3*gX^2-2*gX-4))=0", followed by the correct answer, {-1.0611, 1.7278}.

In program 2, an attempt was made to avoid the "not an equation" error message by rewritting the equation. Unfortunately the revised equation still returns the "not an equation" error message on the CAS screen, followed by the correct answer.

Code:
LOCAL gX,gY,gAns,eq1;
EXPORT rooty4()
BEGIN
  // program 1
  gY:=1.5;
  gX:=90000;  // for testing single quotes
  eq1 := gY = 3*'gX'^2 - 2*'gX' - 4;
  gAns := CAS.solve(eq1, "gX");
  PRINT(gAns);
  
  // program 2
  gY:=1.5;
  gX:=90000;  // for testing single quotes
  eq1 := 3*'gX'^2 - 2*'gX' - 4 - gY == 0;
  gAns := CAS.solve(eq1, "gX");
  PRINT(gAns);
END;


5. Finding roots, Method 1 double quotes

a. To find the roots of the equation, set y = 0.

Code:
LOCAL gX,gY,gAns;
EXPORT rooty5()
BEGIN
  gY:=0;
  gAns := CAS.solve("gY = 3*gX^2 - 2*gX - 4, gX");
  PRINT(gAns);
END;

b. Or, to find the roots, replace y with 0 in the equation.

Code:
LOCAL gX,gY,gAns;
EXPORT rooty6()
BEGIN
  gAns := CAS.solve("0 = 3*gX^2 - 2*gX - 4, gX");
  PRINT(gAns);
END;

c. Or, to find the roots, just use the right side of the equation which is equal to zero as the expression. If you run this on the CAS screen, you may get print terminal message saying "Warning, argument is not an equation, solving 3*x^2-2*x-4=0". Press the Esc key, and the calculator will display the answer.

Code:
LOCAL gX,gY,gAns;
EXPORT rooty7()
BEGIN
  gAns := CAS.solve("3*gX^2 - 2*gX - 4, gX");
  PRINT(gAns);
END;

d. Or, to find the roots, use the zeros command. When using zeros, the equation or expression must be equal to zero.

In programs 3 and 4 below, only the right side of the equation was used. The syntax for zeros is a little different compared to the syntax of solve. In program 3, a noX solve variable would not work. So, an x solve variable was used, where x is not declared or defined within the program, and was not defined as a CAS or user variable. In program 4, a real variable X was used as the solve variable. In both programs 3 and 4, the order of the results was reversed as compared to programs 1 and 2.

Code:
LOCAL gX,gY,str1,gAns;
EXPORT rooty8()
BEGIN
  // program 1
  gY:=0;
  str1 := "gY = 3*gX^2 - 2*gX - 4";
  gAns := CAS.zeros(EVAL(str1), "gX");
  PRINT(gAns);
  
  // program 2
  str1 := "0 = 3*gX^2 - 2*gX - 4";
  gAns := CAS.zeros(EVAL(str1), "gX");
  PRINT(gAns);

  // program 3
  str1 := "3*x^2 - 2*x - 4";
  gAns := CAS.zeros(EVAL(str1), "x");
  PRINT(gAns);

  // program 4
  str1 := "3*X^2 - 2*X - 4";
  gAns := CAS.zeros(EVAL(str1), "X");
  PRINT(gAns);
END;

6. Solving a system of equations

a. Solving 2 equations and 2 unknowns with [ ] using method 1 double quotes

Code:
LOCAL gX,gY,gAns, eq1,eq2;
EXPORT rooty9()
BEGIN
  gAns := CAS.solve("[3*gY = -3*gX + 24, 2*gX + gY = 13], [gX,gY]");
  PRINT(gAns);
 
  eq1 := '3*gY = -3*gX + 24';
  eq2 := '2*gX + gY = 13';
  gAns := CAS.solve("[eq1, eq2], [gX,gY]");
  PRINT(gAns);
END;

b. Solving 3 equations and 3 unknowns with [ ] using method 2 double quotes.

Code:
LOCAL gX,gY,gZ,gAns, eq1,eq2,eq3;
EXPORT rooty11()
BEGIN
  gAns := CAS.solve("[gX + 3*gY + 4*gZ = 10, 5*gX - gY - 2*gZ = 7, 3*gX - 2*gY + 5*gZ = 21]", "[gX,gY,gZ]");
  PRINT(gAns);

  eq1 := 'gX + 3*gY + 4*gZ = 10';
  eq2 := '5*gX - gY - 2*gZ = 7';
  eq3 := '3*gX - 2*gY + 5*gZ = 21';
  gAns := CAS.solve("[eq1, eq2, eq3]", "[gX,gY,gZ]");
  PRINT(gAns);
END;

c. Solving 2 trig equations with 2 unknowns that has multiple solutions with [ ] using method 1 double quotes

If the system of equations has many solutions, one needs to provide the initial guesses for the solve variables, [x guess, y guess, ...], as near as I could figure out. For a system of equations, the method of specifying the x and y guesses are different than what is stated in the help text. In the example below, the initial x and y guesses are [0, 0]. If the guesses are not provided, solve returns { }. I could not figure out how to input a range for the x and y guesses. See solve command with trig equations.

Answer in radian mode is [0.7997, -0.2762]. The answer in degree mode is [45.82, -15.83].

Code:
LOCAL gX,gY,gAns, eq1,eq2;
EXPORT rooty10()
BEGIN
  gAns := CAS.solve("[3*SIN(gX) + 4*COS(gY) = 6, 4*SIN(gY) + 3*COS(gX) = 1], [gX,gY], [0,0]");
  PRINT(gAns);

  eq1 := '3*SIN(gX) + 4*COS(gY) = 6';
  eq2 := '4*SIN(gY) + 3*COS(gX) = 1';
  gAns := CAS.solve("[eq1, eq2], [gX,gY], [0,0]");
  PRINT(gAns);
END;

d. Solving 2 equations, 2 unknown variables, and 3 known variables with [ ] using method 1 double quotes.

In program 2 below, the equations are stored in variables eq1 and eq2, and the solve variables (gX and gY) are enclosed in single quotes. Any values stores in these variables are ignored, and the variables are treated as symbolic variables. See help for quote. In contrast, variables ga, gb, and gc are not enclosed with single quotes. The values stored in these variables are used in the equation.

In program 1, the use of single quotes is not necessary.

Code:
LOCAL gX,gY,gAns, ga,gb,gc;
LOCAL eq1,eq2;
EXPORT rooty12()
BEGIN
  // program 1
  ga:=3;
  gb:=-3;
  gc:=13;
  gAns := CAS.solve("[ga*gY = gb*gX + 24, 2*gX + gY = gc], [gX,gY]");
  PRINT(gAns);
  
  // program 2
  ga:=3;
  gb:=-3;
  gc:=13;
  eq1 := ga*'gY' = gb*'gX' + 24;
  eq2 := 2*'gX' + 'gY' = gc;
  gAns := CAS.solve("[eq1, eq2], [gX,gY]");
  PRINT(gAns);
END;

7. Symbolic calculations

Symbolicly solving for variable x in terms of y from the approximate equation y = 3.5x^2 - 2x - 4.3, where the solve arguments are enclosed in single quotes. Single quotes are used in a symbolic calculation in order to avoid using any values stored in the variables. See help for quote. In this example, the approximate equation must be converted to an exact equation.

When the below program is run on the Home screen, the program works fine. However, when the program is run on the CAS screen, an error message is displayed saying "Warning, argument is not an equation, solving (gY==(7/2*gX^2-2*gX-43/10))=0, follow by the answer. If the program is run from the Program key, the program works fine.

Code:
LOCAL gX,gY,gAns,eq1;
EXPORT rooty13()
BEGIN
  // manually converting the equation to the exact form

  gAns := CAS.solve('gY = (35/10)*gX^2 - 2*gX - (43/10)', 'gX');
  PRINT(gAns);

  eq1 := 'gY = (35/10)*gX^2 - 2*gX - (43/10)';
  gAns := CAS.solve(eq1, 'gX');
  PRINT(gAns);

  // using the exact function to convert the equation

  gAns := CAS.solve(exact('gY = 3.5*gX^2 - 2*gX - 4.3'), 'gX');
  PRINT(gAns);

  eq1 := exact('gY = 3.5*gX^2 - 2*gX - 4.3');
  gAns := CAS.solve(eq1, 'gX');
  PRINT(gAns);
END;

8. Function with arguments

Quadratic equation, where variable x does not exist, meaning the variable was not declared or defined within the program, and was not defined as a CAS or user variable. Method 1 double quotes.

solve1_abc(3,-2,-4) -> {-1.0611, 1.7278}

Code:
EXPORT solve1_abc(a,b,c)
BEGIN
  RETURN CAS.solve("a*x^2 + b*x + c = 1.5, x");
END;

Quadratic equation, where variable x is a local variable created outside of the procedural function. Method 1 double quotes.

solve2_abc(3,-2,-4) -> {-1.0611, 1.7278}

Code:
LOCAL x;
EXPORT solve2_abc(a,b,c)
BEGIN
  RETURN CAS.solve("a*x^2 + b*x + c = 1.5, x");
END;

9. Issues with difficult equations

Some solve methods can be more accurate than other methods, depending upon the difficulty of the equation, solve variable type, and whether the equation and known variables are in the exact or approximate form.

a. Approx. equation with approx. values stored in the known variables

In the program below, the equation is in the approximate form. This was done by making the 8 value in the equation to 8.0. The values stored in the known variables are approximate values.

In program 1, the solve variable (gTheta) is a local variable that was created outside the procedural function. The first solve method, the CAS or symbolic method, returned { }. The second solve method, the iterative method, returned "undef". The third solve method, the bisection method, returned the correct answer, [3].

In program 2, the solve variable (eTheta) is an export variable that was created outside the procedural function. The first solve method, the CAS or symbolic method, returned { }. The second solve method, the iterative method, returned "undef". The third solve method returned the correct answer, [3].

In program 3, the solve variable (noTheta) was not declared or defined, and is said to "not exist". Further, the solve variable was not defined as a CAS or user variable. All of the solve methods were able to find the correct solution. However, when solving the first method on the CAS screen, the program said "Solving by bisection with change of variable x= tan(t) and t=-1.57..1.57. Try fsolve(equation,x=guess) for iterartive solver or fsolve(equation,x=xmin..xmax) for bisection." This message was followed by the correct answer.

In program 4, the solve variable is a real variable (X). The first solve method gave an incorrect answer of {2.245}. Both the second and third solve methods gave the correct answer, 3 and [3].

Code:
EXPORT eTheta;
LOCAL gAns, str1, gQ, gV, gD, gTheta;
EXPORT rooty14()
BEGIN
  gQ:=0.6030449983;
  gV:=0.75;
  gD:=1.5;

  // program 1, where the solve variable is a local variable created outside the procedural function (gTheta)

  str1 := "gQ = gV * gD^2 * (gTheta - SIN(gTheta)) / 8.0";
  gAns := CAS.solve(EVAL(str1), "gTheta");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "gTheta=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "gTheta=0..6.283");
  PRINT(gAns);

  // program 2, where the solve variable is an export variable created outside the procedural function (eTheta)

  str1 := "gQ = gV * gD^2 * (eTheta - SIN(eTheta)) / 8.0";
  gAns := CAS.solve(EVAL(str1), "eTheta");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "eTheta=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "eTheta=0..6.283");
  PRINT(gAns);

  // program 3, where the solve variable does not exist (noTheta)

  str1 := "gQ = gV * gD^2 * (noTheta - SIN(noTheta)) / 8.0";
  gAns := CAS.solve(EVAL(str1), "noTheta");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "noTheta=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "noTheta=0..6.283");
  PRINT(gAns);

  // program 4, where the solve variable is a real variable (X)

  str1 := "gQ = gV * gD^2 * (X - SIN(X)) / 8.0";
  gAns := CAS.solve(EVAL(str1), "X");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "X=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "X=0..6.283");
  PRINT(gAns);
END;

b. Exact equation with approx. values stored in the known variables

For the following program, the equation to be solved is in the exact form. Notice that the value 8 in the equation is in the exact form. However, the values stored in the known variables are approximate values.

In program 1, the solve variable (gTheta) is a local variable that was created outside the procedural function. This program must be run in radian angle mode. The first solve method gave an incorrect answer of {2.859}. The second solve method was not able to find the answer and return "undef". The third solve method, bisection method, gave the correct answer, [3].

In program 2, the solve variable (eTheta) is an export variable that was created outside the procedural function. As with program 1, the first solve method gave an incorrect answer of {2.859}. The second solve method was not able to find the answer and return "undef". The third solve method, bisection method, gave the correct answer, [3].

In program 3, the solve variable (noTheta) was not declared or defined, and is said to "not exist". All of the solve methods were able to find the solution. However, when solving the first method on the CAS screen, the program said "Solving by bisection with change of variable x= tan(t) and t=-1.57..1.57. Try fsolve(equation,x=guess) for iterartive solver or fsolve(equation,x=xmin..xmax) for bisection." This message was followed by the correct answer.

In program 4, the solve variable is a real variable (X). The first solve method gave an incorrect answer of {2.245}. Both the second and third solve methods gave the correct answer, 3 and [3]. In earlier test, the first method gave the correct answer for reasons unknown.

Code:
EXPORT eTheta;
LOCAL gAns, str1, gQ, gV, gD, gTheta;
EXPORT rooty15()
BEGIN
  gQ:=0.6030449983;
  gV:=0.75;
  gD:=1.5;

  // program 1, where the solve variable is a local variable created outside the procedural function (gTheta)

  str1 := "gQ = gV * gD^2 * (gTheta - SIN(gTheta)) / 8";
  gAns := CAS.solve(EVAL(str1), "gTheta");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "gTheta=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "gTheta=0..6.283");
  PRINT(gAns);

  // program 2, where the solve variable is an export variable created outside the procedural function (eTheta)

  str1 := "gQ = gV * gD^2 * (eTheta - SIN(eTheta)) / 8";
  gAns := CAS.solve(EVAL(str1), "eTheta");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "eTheta=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "eTheta=0..6.283");
  PRINT(gAns);

  // program 3, where the solve variable does not exist (noTheta)

  str1 := "gQ = gV * gD^2 * (noTheta - SIN(noTheta)) / 8";
  gAns := CAS.solve(EVAL(str1), "noTheta");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "noTheta=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "noTheta=0..6.283");
  PRINT(gAns);

  // program 4, where the solve variable is a real variable (X)

  str1 := "gQ = gV * gD^2 * (X - SIN(X)) / 8";
  gAns := CAS.solve(EVAL(str1), "X");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "X=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "X=0..6.283");
  PRINT(gAns);
END;

c. Exact equation with known variable converted to exact values using exact command

For the following program, the equation to be solved is in the exact form. The approximate values stored in the known variables are converted to exact form using the exact command.

In program 1, the solve variable (gTheta) is a local variable that was created outside the procedural function. This program must be run in radian angle mode. When run on the Home screen, the first solve method returned { }. The second solve method returned the correct answer, 3. The third solve method, bisection method, gave the correct answer, [3]. When run on the CAS screen, the program gave the same answers as before, except that in solve method 1, a warning was given that said "Unable to isolate gTheta in ..., switching to approx. solutions".

In program 2, the solve variable (eTheta) is an export variable that was created outside the procedural function. When run on the Home screen, the first solve method returned { }. The second solve method returned the correct answer, 3. The third solve method, bisection method, gave the correct answer, [3]. When run on the CAS screen, the same answers were given. However, when solving for the first solve method, a warning was given that it was unable to isolate the solve variable, and it was switching to approximate solutions.

In program 3, the solve variable (noTheta) was not declared or defined, and is said to "not exist". All of the solve methods were able to find the solution. However, when solving the first method on the CAS screen, the program said "Unable to isolate noTheta in ..., switching to approx. solutions. Solving by bisection with change of variable x= tan(t) and t=-1.57..1.57. Try fsolve(equation,x=guess) for iterartive solver or fsolve(equation,x=xmin..xmax) for bisection." This message was followed by the correct answer.

In program 4, the solve variable is a real variable (X). When run on the Home screen, the first solve method returned { }. Both the second and third solve methods gave the correct answer, 3 and [3]. When run on the CAS screen, the same answers were given. However, when solving for the first solve method, a warning was given that it was unable to isolate the solve variable, and it was switching to approximate solutions.

Code:
EXPORT eTheta;
LOCAL gAns, str1, gQ, gV, gD, gTheta;
EXPORT rooty16()
BEGIN
  gQ:=0.6030449983;
  gV:=0.75;
  gD:=1.5;
  gQ:=exact(gQ);
  gV:=exact(gV);
  gD:=exact(gD);

  // program 1, where the solve variable is a local variable created outside the procedural function (gTheta)

  str1 := "gQ = gV * gD^2 * (gTheta - SIN(gTheta)) / 8";
  gAns := CAS.solve(EVAL(str1), "gTheta");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "gTheta=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "gTheta=0..6.283");
  PRINT(gAns);

  // program 2, where the solve variable is an export variable created outside the procedural function (eTheta)

  str1 := "gQ = gV * gD^2 * (eTheta - SIN(eTheta)) / 8";
  gAns := CAS.solve(EVAL(str1), "eTheta");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "eTheta=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "eTheta=0..6.283");
  PRINT(gAns);

  // program 3, where the solve variable does not exist (noTheta)

  str1 := "gQ = gV * gD^2 * (noTheta - SIN(noTheta)) / 8";
  gAns := CAS.solve(EVAL(str1), "noTheta");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "noTheta=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "noTheta=0..6.283");
  PRINT(gAns);

  // program 4, where the solve variable is a real variable (X)

  str1 := "gQ = gV * gD^2 * (X - SIN(X)) / 8";
  gAns := CAS.solve(EVAL(str1), "X");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "X=2.8");
  PRINT(gAns);
  gAns := CAS.solve(EVAL(str1), "X=0..6.283");
  PRINT(gAns);
END;



RE: Program code with solve function - Gene222 - 06-26-2024 04:10 AM

A. CAS program, where the x variable does not exist

The following CAS program is for the equation y = 3x^2 - 2x - 4. This program uses two function argument variables (px and py), one local variable created inside the procedural function (lAns), and one cas variable (x), where the cas variable x is not declared or defined within the program. The cas variable x is said to "not exist", meaning the cas variable x is not shown in the Vars key, CAS tab, under the "all" sub-menu. As is, this program can only be run on the CAS screen. Running this program on the Home screen will give a syntax error, because the cas variable x was not declared.

Code:
#cas
f1_xy(px, py):=
begin
  local lAns;
  lAns := solve(py = 3*px^2 - 2*px - 4, x);
  return approx(lAns);
end;
#end

To find x, given y = 1.5, on the CAS screen, enter f1_xy(x, 1.5) or f1_xy('x', 1.5), which returns {-1.0611, 1.7278}.

To find the roots where y=0, on the CAS screen, enter f1_xy(x, 0) or f1_xy('x', 0), which returns {-0.86852, 1.5352}.

To find y, given x = -1.0611, on the CAS screen, enter f1_xy(-1.0611, x) or f1_xy(-1.0611, 'x'), which returns {1.5}.

When running the program on the CAS screen, use the default CAS settings for page 2. The CAS program can be accessed using the Vars key and CAS tab. However, the program is written and editted using the Shift Program key. On the G1 calculator, when creating a new program, the CAS checkbox does not appear to do anything. 'x' is the un-evaluated variable x. See help for QUOTE. x in the solve equation is a CAS variable.

In the solve function, the solve variable cannot be a local variable created within the procedural function. So, we cannot create the solve x variable using the local statement shown above. In a CAS program, to my knowledge, CAS does not allow a local statement outside the procedural function, nor an export statement in the program. This is why the cas variable x is not declared within this cas program.

B. Same CAS program, but manual create cas variable x

To run this program on the Home screen, you must manually create the CAS variable by typing x:=0 on the CAS screen and pressing the Enter key. CAS variable x can now be seen by the Vars key, CAS tab, and all sub-menu, and the program can now be run on the Home screen, with one exception. One cannot use x as a function argument. Only 'x' can be used.

For example, to find x, given y = 1.5, on the Home or CAS screen, enter f1_xy('x', 1.5), which returns {-1.0611, 1.7278}.

Using f1_xy(x, 1.5) on the Home or CAS screen will return [[ ]].

C. Adding a purge statement to create the cas variable x

To make the program automatically create a CAS variable x, you can add a purge statement to the program, as shown below. However, the program will only create the x variable, when the program is run on the CAS screen. So, before you can run the program on the Home screen, you must first run the program on the CAS screen. CAS will create the variable x, which can be verified by using the Vars key. Now, the program can be run on the Home screen.

Code:
#cas
f1_xy(px, py):=
begin
  local lAns;
  purge(x);
  lAns := solve(py = 3*px^2 - 2*px - 4, x);
  return approx(lAns);
end;
#end

D. Use system variable X as the solve variable

Alternatively, the system variable X can be used as the solve variable. The system variable X already exists, so it will not generate a syntax error when running the program on the Home screen. The system variable X is a real variable, and it seems to work fine with solve in the CAS program. The only change is that the user must input 'X' instead of 'x'. So, the program below works on both the Home and CAS screens.

Code:
#cas
f1_xy(px, py):=
begin
  local lAns;
  lAns := solve(py = 3*px^2 - 2*px - 4, X);
  return approx(lAns);
end;
#end



RE: Program code with solve function - Gene222 - 06-27-2024 10:30 AM

If the equation being solved is difficult, solve can give a wrong or unexpected answer, as discussed in the following posts.
Solve wrong answer
Question on solve. See Tim's reply.
This occures when solve cannot symbolically solve the equation and reverts to using fsolve. fsolve will take the valve stored in the solve variable, gX, and possibly uses it like an initial guess or something else that may effects the results. In some instances, this can lead to the wrong answer.

On difficult equations, to help prevent solve from reverting to fsolve, it was suggested to write the equation in the exact form, instead of the approximate form. Apparently, the increased accuracy can make a difference on some equations, and the exact form helps prevent solve from reverting to fsolve. For the solve variable, it was suggested to purge the variable to remove anything stored in the variable and return it to its symbolic state. See purge help. Alternatively, one can use the fsolve bisection method, provided one knows where the solution exists.

I added a purge statement. However, for me, it was more important to write the equation in the exact form for the improved accuracy.

Revised program shown below.

Code:
LOCAL gX,gY,gAns;
EXPORT rooty2()
BEGIN
  gY := 15/10;
  CAS.purge("gX");             // gX:='gX' does not do anything.  See ftneek's post 8 below.
  PRINT(CAS.type("gX"));
  gAns := CAS.solve("gY = 3*gX^2 - 2*gX - 4, gX");
  PRINT(gAns);
END;



RE: Program code with solve function - Gene222 - 06-28-2024 05:54 PM

Can someone confirm the following?

1. In the previous post, the line gX := 'gX' simply converts the variable gX into a symbolic variable. Is this an accurate statement?

2. In the program below, the variable noX is a purge variable that "does not exist". Is the variable noX just a symbolic variable within the confines of the solve function?

Code:
LOCAL gX,gY,gAns;
EXPORT rooty2()
BEGIN
  gY:=1.5;
  gAns := CAS.solve("gY = 3*noX^2 - 2*noX - 4, noX");
  PRINT(gAns);
END;

My apologies if this is covered in the user guide.


RE: Program code with solve function - ftneek - 06-29-2024 02:28 AM

(06-28-2024 05:54 PM)Gene222 Wrote:  2. In the program below, the variable noX is a purge variable that "does not exist". Is the variable noX just a symbolic variable within the confines of the solve function?

The first argument of the solve command is Expr, an expression in any variable. By default solve solves tries to isolate x so if your expression has an x it can be omitted. If your expression does not have an x and the second argument is omitted, then no solution is provided (it solves for Var=x by default). Most of that can be inferred by reading the help text for the solve() command, though it doesn't seem to explicitly state the default is x.

In CAS, any variable that has not been assigned a value is 'symbolic'.
purge(x)
type(x) -> DOM_IDENT
x:=1
type(x) -> DOM_INT

Also gX:='gX' shouldn't really do anything. In CAS View:
purge(x)
x:='x' -> "No such variable x"


RE: Program code with solve function - Gene222 - 06-29-2024 04:31 PM

Thank you for your explanation. I corrected some of the older posts.

(06-29-2024 02:28 AM)ftneek Wrote:  
(06-28-2024 05:54 PM)Gene222 Wrote:  2. In the program below, the variable noX is a purge variable that "does not exist". Is the variable noX just a symbolic variable within the confines of the solve function?

The first argument of the solve command is Expr, an expression in any variable. By default solve solves tries to isolate x so if your expression has an x it can be omitted. If your expression does not have an x and the second argument is omitted, then no solution is provided (it solves for Var=x by default). Most of that can be inferred by reading the help text for the solve() command, though it doesn't seem to explicitly state the default is x.

In CAS, any variable that has not been assigned a value is 'symbolic'.
purge(x)
type(x) -> DOM_IDENT
x:=1
type(x) -> DOM_INT

Also gX:='gX' shouldn't really do anything. In CAS View:
purge(x)
x:='x' -> "No such variable x"



RE: Program code with solve function - Gene222 - 07-09-2024 09:19 PM

Posts 4 and 5 were revised to correct errors and add more examples on using "solve".

7/11/2024. Revised the solving 2 eq and 2 unknowns by adding an example where the equations are stored in variables.