Post Reply 
Program code with solve function
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;
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;
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Program code with solve function - SoxxoZ - 06-19-2024, 02:23 PM
RE: Program code with solve function - Gene222 - 06-27-2024 10:30 AM



User(s) browsing this thread: 1 Guest(s)