HP Forums
HP 50g Solve on object? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: HP 50g Solve on object? (/thread-12888.html)



HP 50g Solve on object? - iainiain32 - 04-29-2019 09:22 AM

Hi all,
Just purchased a HP50g as I enjoyed using a HP12C and wanted more.

I was trying to programmatically use the SOLVE command and I think the following exemplifies what I am trying to do. Say I have a small program like this:

«
« X 5 + »
'X'
SOLVE
»

The SOLVE command fails on running this program. However, if I use the solver via the menus ("NUM.SLV" and select "1. Solve equation") and enter << X 5 + >> in the equation input the solver correctly returns -5 (screenshot attached). Is there a way I can use the solver in a program on an object <<>> (hopefully I'm using the right terms..)?

As a background on why - I created a program for the value of a call option (which would replace <<X 5 +>> above) and wanted to use the solver to back out the implied volatility (one of the inputs to price of call).

Thank you!


RE: HP 50g Solve on object? - grsbanks - 04-29-2019 11:27 AM

The 50g expects an expression or an equation on level 2, not a program.

So, this works:

Code:
\<<
  'X+5' 'X' SOLVE
\>>



RE: HP 50g Solve on object? - iainiain32 - 04-29-2019 12:22 PM

Thanks for the reply! Perhaps I am misusing the solver or there is an alternative method to do what I'm looking for.

This is my program, BSCALL (I've added '->' since I copied it over):

Code:

«
  -> S K v t r @ spot, strike, annual vol, time (years), risk free
 «
 '(1/(v*ƒ(t)))*(LN(S/K) + (r + SQ(v)/2)*(t))' ->NUM
  d1
 «
  'd1 - v*ƒ(t)' ->NUM
  -> d2
 «
  1 
  0 1 d1 UTPN @ This is upper of the normal cdf so 1 - UTPN 
  - 
  S *
  1
  0 1 d2 UTPN
  -
  'K*EXP(-r*(t))' ->NUM
  *
  -
 »
 »
 »
»

Overall, I'm looking to use the calculator to solve for one of the inputs, volatility, v when BSCALL() = .25. This seems to work when I use the solver via the solver in the menu with these inputs:

<< 25 30 X .5 .03 BSCALL .25 - >>
solves for X = .2069... correct!

or this:
'BSCALL(25,30,X,.5,.03) = .25' works too.

However, neither of these methods works via the key input on the stack.

So in a general sense is there any way in the calculator to solve for one of the inputs of another program (without using the menus)?


RE: HP 50g Solve on object? - Giuseppe Donnini - 04-29-2019 04:41 PM

I would rather rely on the good ol' HP-48 routines still present in the HP-50G, and use the ROOT command.

ROOT is the programmable form of HP Solve and takes the following arguments:

+---------------+---------------------+------------------------------------------+
| Stack level 3 |  'alg' / << prog >> | 1. An algebraic expression or a program  |
|               |                     |    acting as the current equation.  Note |
|               |                     |    that the variable EQ is *not* used by |
|               |                     |    ROOT.                                 |
|               |                     |                                          |
| Stack level 2 |  'global'           | 2. The name of the unknown.              |
|               |                     |                                          |
| Stack level 1 |  guess / {guesses}  | 3. A number or a list of 1, 2, or 3      |
|               |                     |    numbers acting as initial guess(es).  |
+---------------+---------------------+------------------------------------------+

Don't forget the third argument! In contrast to its interactive implementation, the programmable version of the root-finder requires you to supply an initial guess. (The HP Solve environment uses a default guess if you don't supply one, which is simply the current value of the unknown variable, if it already exists, or zero, if it doesn't [formal variable]).

Note that ROOT only displays a message if an error occurs (Bad Guess(es) or Constant?). As it is not meant to be used interactively, it does not provide intrepretative messages when a root (or extremum) is actually found (Zero, Sign Reversal, or Extremum), nor does it allow you to monitor the root-finding process as during an HP Solve session.

Applying this to your example, we get the following program:

\<<             @ Begin program
  \<< X 5 + \>> @ Current equation
  'X'           @ Unknown
  0             @ Initial guess
  ROOT          @ Call root-finder
\>>             @ End program

Executing the program puts -5 on stack level 1, just as you wanted.


RE: HP 50g Solve on object? - iainiain32 - 04-30-2019 04:02 PM

That's amazing, thank you!! Works perfectly with my original code too. Thanks again!


RE: HP 50g Solve on object? - Gilles - 04-30-2019 07:47 PM

(04-29-2019 09:22 AM)iainiain32 Wrote:  Hi all,
Just purchased a HP50g as I enjoyed using a HP12C and wanted more.

the HP50g is a jewel for me ;D
with your example the simplest way is :

'X+5' SOLVEVX

in 7 keystrokes

'
X+5 ENTER
LeftShift S.SVL
SOLVEVX

result :

X=-5

Another example in |R~ mode

'X^3.+7.*X^2.+9.*X-12.'
SOLVEVX

{ 'X=.791287847478' 'X=-3.79128784748' 'X=-4.' }

This works for rational expression. for non rational expression you can you the MSLV command. exemple

[ '√(x^2+y^2)=5' '√(2*a^2-(2*x+2*y)*a+(x^2+y^2))=3' '√(a^2-2*y*a+(x^2+y^2))=4' ]
[ 'x' 'y' 'a' ]
[ 3 3 6] @ initial guess
MSLV

result

[ 3.44599325458 3.62286219575 5.65390392093 ]


RE: HP 50g Solve on object? - Giuseppe Donnini - 04-30-2019 08:14 PM

But that's not exactly what the original poster wanted. As a matter of fact, SOLVEVX does not work with an equation/expression in the form of a program. So, for the given problem, the best solution is still to use the ROOT command.


RE: HP 50g Solve on object? - Gilles - 04-30-2019 09:10 PM

**Deleted **


RE: HP 50g Solve on object? - Gilles - 04-30-2019 09:18 PM

(04-30-2019 08:14 PM)Giuseppe Donnini Wrote:  But that's not exactly what the original poster wanted. As a matter of fact, SOLVEVX does not work with an equation/expression in the form of a program. So, for the given problem, the best solution is still to use the ROOT command.

You are right. I though UTPN was a equation/expression but it is a command/program.