HP Forums
Solver inside of HP basic - 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: Solver inside of HP basic (/thread-3193.html)



Solver inside of HP basic - Powersoft - 02-25-2015 01:06 PM

I wont to use the HP solver inside of the HP basic envoirment.
And think this should be the code.
But I dont know how to use the solve statement, the manual is'nt clear.

Export Kepler()
Begin
Local M=60,e=0.15,E;
HAngle:=0;
solve('E-sin(e)-M=0',E); < dont know how to do this???????
Print(E);
hAngle:=1;
End;

Thanks for any help.

Jan Kromhout
hellevoetsluis-NL


RE: Solver inside of HP basic - Angus - 02-25-2015 01:41 PM

Hi,

Han has written a pretty good article about cas programming http://www.hpmuseum.org/forum/thread-3149.html. You might want to take a look at that.
I am sure e is a reserved name so you won't be able to use it and there are at least a lot of typos in your code.
Plus I am not sure if using capital Variables is advised. I never know if they will be treated symbolically or if their numerical value from home takes effect. For a local variable I think that should not happen, but I try to avoid single capital variables. I just can't get used to the home/cas thingy in the calculator.

To make it short: a cas function would do it nicely and very straightforward:
#cas
kepler(mass):=
BEGIN
local vale:=0.15;

ret_val:= solve(valE-sin(vale)-mass=0,valE);
return ret_val;
END;
#end

I left out angle. I don't know what it does.


RE: Solver inside of HP basic - Tim Wessman - 02-25-2015 02:04 PM

Since this is just a plain numeric solving, just use the numeric solver command. FNROOT is what you are looking for. No other changes are needed in your first posted program.


RE: Solver inside of HP basic - Thomas Ritschel - 02-26-2015 09:17 AM

(02-25-2015 02:04 PM)Tim Wessman Wrote:  Since this is just a plain numeric solving, just use the numeric solver command. FNROOT is what you are looking for. No other changes are needed in your first posted program.

By changing Jan's code into the following I get "Kepler Error: Bad Guess(es)":
Code:
EXPORT Kepler()
BEGIN
  LOCAL M=60, e=0.15, E;
  HAngle:=0;
  FNROOT(E-SIN(e)-M,E);
  PRINT(E);
  HAngle:=1;
END;

I also tried other variants, e.g. FNROOT('E-SIN(e)-M=0',E) or by providing some proper guess, but it keeps showing the error message. The problem may be caused by the use of local variables: By using the single letter HOME variables, e.g. by removing the LOCAL statement (and changing "e" into some other capital letter), the error message disappears. But then it's just printing "0" instead of the correct result...

BTW.: I'm using the latest (?) calculator software version 6975.


RE: Solver inside of HP basic - Powersoft - 02-26-2015 02:59 PM

(02-26-2015 09:17 AM)Thomas Ritschel Wrote:  
(02-25-2015 02:04 PM)Tim Wessman Wrote:  Since this is just a plain numeric solving, just use the numeric solver command. FNROOT is what you are looking for. No other changes are needed in your first posted program.

By changing Jan's code into the following I get "Kepler Error: Bad Guess(es)":
Code:
EXPORT Kepler()
BEGIN
  LOCAL M=60, e=0.15, E;
  HAngle:=0;
  FNROOT(E-SIN(e)-M,E);
  PRINT(E);
  HAngle:=1;
END;

I also tried other variants, e.g. FNROOT('E-SIN(e)-M=0',E) or by providing some proper guess, but it keeps showing the error message. The problem may be caused by the use of local variables: By using the single letter HOME variables, e.g. by removing the LOCAL statement (and changing "e" into some other capital letter), the error message disappears. But then it's just printing "0" instead of the correct result...

BTW.: I'm using the latest (?) calculator software version 6975.

I have modified the code a little bit, because it shoeld solved in radians.
Still I get the same massage.
After change this code
EXPORT Kepler()
LOCAL M=60*pi/180,e=0.15,X;
X:=FNROOT(E-e*sin(E)-M,E);
PRINT(X*180/pi);
END;

And change the line of FNROOT into

X:=FNROOT(E-0.15*sin(E)-60*pi/180,E);

than it working, this tells me that FNROOT is not able to use variable.
Strange behavior.


RE: Solver inside of HP basic - Thomas Ritschel - 02-26-2015 03:33 PM

I've got it working using the following code without the use of local variables:
Code:
EXPORT Kepler()
BEGIN
  M:=60*pi/180;   <-- use the "pi" key (shift + 3)
  A:=0.15;
  HAngle:=0;
  PRINT(FNROOT(E-A*SIN(E)-M,E));
  HAngle:=1;
END;

Obviously the solution isn't stored in the variable E. Therefore one needs to get the return value of FNROOT instead. This wasn't clear to me...

Anyway, I'm still wondering why one cannot use local variables here.

Edit: Equation corrected...


RE: Solver inside of HP basic - Tim Wessman - 02-26-2015 04:20 PM

Turns out that local variables aren't fully carried through with with the solver call which I did not know. The primary reason is that you could run into a situation where you are doing something like LOCAL A; FNROOT(F1(X)-A,X) where F1 is defined as A+5. The question then arises: which value of A is being referred to here? The global A or the local A.

A similar issue could arise on the 50g for example when you try to solve a formula defined in directories up from your current and no defines in those location. It could result in all sorts of unexpected behavior.

I've put this on the list of things to look at and see what we might be able to do.


RE: Solver inside of HP basic - Powersoft - 03-06-2015 04:18 PM

Thanks for any help so far. The solver is working great.
Now I understand how it works.
One question is still open for me. What is the accuracy of the the solver?
Is it posible to set this accuracy with a variable?
As an example, suggest this variable is eps.
And now when I set this to eps=0.001 the solver is find fast the condition to stop solving and give back the answer.



Jan Kromhout
Hellevoetsluis-NL