Program code with solve function
|
06-19-2024, 02:23 PM
Post: #1
|
|||
|
|||
Program code with solve function
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:
Thank you guys |
|||
06-20-2024, 10:04 PM
Post: #2
|
|||
|
|||
RE: Program code with solve function
Here are some examples. In your example you have to use a 'HOME' variable like 'X', and not 'x'
Code:
|
|||
06-21-2024, 02:13 PM
Post: #3
|
|||
|
|||
RE: Program code with solve function
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) |
|||
06-25-2024, 08:17 PM
(This post was last modified: 11-25-2024 01:26 PM by Gene222.)
Post: #4
|
|||
|
|||
RE: Program code with solve function
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. 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), Newton or 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; 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; 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; 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; 5. Finding roots, Method 1 double quotes a. To find the roots of the equation, set y = 0. Code: LOCAL gX,gY,gAns; b. Or, to find the roots, replace y with 0 in the equation. Code: LOCAL gX,gY,gAns; 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; 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; 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; b. Solving 3 equations and 3 unknowns with [ ] using method 2 double quotes. Code: LOCAL gX,gY,gZ,gAns, eq1,eq2,eq3; 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; 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; 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; 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) 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; 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; 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; 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; |
|||
06-26-2024, 04:10 AM
(This post was last modified: 07-19-2024 01:21 AM by Gene222.)
Post: #5
|
|||
|
|||
RE: Program code with solve function
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 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 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 |
|||
06-27-2024, 10:30 AM
(This post was last modified: 06-29-2024 05:00 PM by Gene222.)
Post: #6
|
|||
|
|||
RE: Program code with solve function
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; |
|||
06-28-2024, 05:54 PM
Post: #7
|
|||
|
|||
RE: Program code with solve function
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; My apologies if this is covered in the user guide. |
|||
06-29-2024, 02:28 AM
(This post was last modified: 06-29-2024 03:30 AM by ftneek.)
Post: #8
|
|||
|
|||
RE: Program code with solve function
(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" - neek |
|||
06-29-2024, 04:31 PM
Post: #9
|
|||
|
|||
RE: Program code with solve function
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? |
|||
07-09-2024, 09:19 PM
(This post was last modified: 07-12-2024 02:53 AM by Gene222.)
Post: #10
|
|||
|
|||
RE: Program code with solve function
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. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)