Program code with solve function
|
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; |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Program code with solve function - SoxxoZ - 06-19-2024, 02:23 PM
RE: Program code with solve function - gehakte_bits - 06-20-2024, 10:04 PM
RE: Program code with solve function - Gene222 - 06-25-2024 08:17 PM
RE: Program code with solve function - Gene222 - 06-26-2024, 04:10 AM
RE: Program code with solve function - Gene222 - 06-27-2024, 10:30 AM
RE: Program code with solve function - Gene222 - 06-28-2024, 05:54 PM
RE: Program code with solve function - ftneek - 06-29-2024, 02:28 AM
RE: Program code with solve function - Gene222 - 06-29-2024, 04:31 PM
RE: Program code with solve function - gehakte_bits - 06-21-2024, 02:13 PM
RE: Program code with solve function - Gene222 - 07-09-2024, 09:19 PM
|
User(s) browsing this thread: 1 Guest(s)