HP Forums

Full Version: Question about the use of the CAS solve function in a program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there any way to have a program use the solve function found in the CAS mode of the Prime? (or any other CAS function for that matter)

if I create a simple program like this:

EXPORT solve_function_test(x, y)
LOCAL answer;
answer := solve(x^2 - 2 * y + n = 0.5 * n^3, n);
RETURN answer;

It gives a syntax error around the variable n, which I would assume is just contained to the solve function. If I create n as a local variable, it doesn't give any errors but always returns 0.

To use a cas function from a home program, you generally need to identify it as a cas function to distinguish it from any similarly-named home function.
So instead of:
cas.fun(...) or

Or, perhaps the tips in this thread may help:
https://www.hpmuseum.org/forum/thread-17051.html (solve problems)
- Douggg suggests Solve.SOLVE [using the Solve app, not CAS], and others have the expression in quotes.

[updated to add link to thread]
(11-11-2023 05:03 AM)ZDog2002 Wrote: [ -> ]If I create n as a local variable, it doesn't give any errors but always returns 0.

The following is what I observed for software version 2.1.14730 (2023 04 13), when using solve in a program.

Solve variable

The solve variable can be a system variable A-Z, export variable, or global variable. The solve variable cannot be a local function variable, function parameter variable, or list variable, such as gD(1). When solving an equation symbolically, the equation variables are not declared or exist anywhere on the calculator. So, the solve variable can also be a variable that does not exist on the calculator, even if the equation is solved approximately, and not symbolically

When the solve function determines the answer, the answer is not stored in the solve variable. The solve function only returns the answer. So, the program must be written to save or use the answer.

In your program, when using x=0, y=0, system variable N as the solve variable, and not specifying a guess for N, the program returns {-sqrt(2), 0, sqrt(2)}. If the solve function tried to store the answer in system variable N, an error would occur, because a list cannot be stored in a real object. So, it is good that solve does not store the answer in the solve variable, and forces the user to specify how the answer is saved.

When a guess or range is not provided, solve tries to find all solutions. The current help for solve implies that this should be done with caution, and advises the user to supply a guess or define an interval. However, providing a guess is not fullproof. Sometimes the guess needs to be reasonnably close to the solution. Personally, when solving an equation that I am not familiar with, I find it helpful to graph the equation, and look for areas that can interfer with solve's ability to find a solution or to graphically see the solutions to the equation. The solve app makes charting a function with several known variables easy, but the solve app has it's own set of rules and issues. However, sometimes charting the equation does not help. In these cases, one needs to known the situations that can can mess up Newton's or bisection methods. Sometimes, it may be necessary to test these methods using excel.

For one equation that I used where a guess or range was not provided, solve returned no answer. When a guess was provided, solve returned one solution, and when a range was provided, solve returned two solutions. On another equation, not providing a guess and providing a guess did not work, but specifying a range did work. This might be the reason the solve help advises the user to supply a guess or define a range.

The solve function does not check the syntac of the equation. In one instance, I had an equation with an extra left parenthesis. I could not get the solve function to return an answer, until I removed the extra parenthesis.

The solve function will ignore any value that was previously stored in the solve variable. A value, list, or complex number stored in the solve variable will be ignored. So, there is no need to clear the solve variable.

Known variables

The known variables can be a system variable (A-Z), export variable, global variable, function parameter variable (x or y in your program), or list variable, such as uD(1). The known variables cannot be local function variables.

Answer variable

The answer variable can be a system variable A-Z (provided that solve does not return a complex number or list), export variable, global variable, local function variable, or list variable, such as uD(1). The answer variable cannot be a function parameter variable.
Regarding the variables used in solve, if the known variables and solve variable are equal to zero and the equation is exact in form, solve will try to find all solutions - symbolically. For example, when running your program with x=0, y=0 and not providing a guess for n (thus n=0), solve will return -sqrt(2), 0, sqrt(2) in brackets. This appears to be another way to symbolically solve an equation in a program.

[Deleted 2nd paragraph, because it was incorrect. The previous post was significantly revised to correct some errors on 11-16-2023 and 11-18-2023.]
Hey, i wasted some time looking for a way to make this work so you don't have to waste it :

Both work for me:
EXPORT solve_function_test()

LOCAL answer;
answer :=solve ('X^2 - 2=0','X');


//other way that also works:
lOCAL a;

Reference URL's