# HP Forums

Full Version: Can you calculate Pi using a Solver (on the prime)?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Valentin gives a recursive method to solve for Pi using fnroot solver.
Valentin Albillo ' Wrote: [ -> ][...]For instance, if your Solver admits nesting and can use the Gamma function, then this will do:Code for the HP-71B, where FNROOT (Find Root) is the "official" HP-71B's "Solver":

1   DEF FNF(X) = FNROOT(1, 1, GAMMA(FVAR) - X)
2   DISP FNROOT(3, 4, FNF(SQR(FVAR)) - 1/2)

>RUN

3.14159265359

This produces as a root the theoretically exact (not an approximation) value of Pi.

V.

Tried this on the prime but it appears that the solver doesnt support recursion
Code:
```EXPORT FVAR:=0; EXPORT FNF(X) BEGIN FNROOT(Gamma(FVAR)-X,FVAR,1,1); END;```

And then at Home screen:

Code:
`FNROOT(FNF(√(FVAR))-(1/2),FVAR,3,4)`

produces the error Error: Bad Guess(es)

I also tried defining FNF as a user function using Define key, changing and omitting initial guesses, but same error. ROOT instead of FNROOT gives Error:No solution found.
.
Hi, Stevetuc:

(12-17-2019 08:08 AM)Stevetuc Wrote: [ -> ]Valentin gives a recursive method to solve for Pi using fnroot solver.

Valentin Albillo ' Wrote: [ -> ][...]For instance, if your Solver admits nesting and can use the Gamma [...]

Tried this on the prime but it appears that the solver doesnt support recursion

Just a terminology issue: it's not recursion, as you say, it's nesting.

The Solver (FNROOT) instance isn't calling itself (recursion), it's the function being solved the one who's calling another instance of the solver applied to a different equation (nesting).

Think of integration. A double or triple integral isn't recursive integration, it's just nested integration.

Regards.
V.
(12-17-2019 03:54 PM)Valentin Albillo Wrote: [ -> ].
Hi, Stevetuc:

(12-17-2019 08:08 AM)Stevetuc Wrote: [ -> ]Valentin gives a recursive method to solve for Pi using fnroot solver.

Tried this on the prime but it appears that the solver doesnt support recursion

Just a terminology issue: it's not recursion, as you say, it's nesting.

The Solver (FNROOT) instance isn't calling itself (recursion), it's the function being solved the one who's calling another instance of the solver applied to a different equation (nesting).

Think of integration. A double or triple integral isn't recursive integration, it's just nested integration.

Regards.
V.

Thanks for the interesting clarification!
(12-17-2019 04:16 PM)Stevetuc Wrote: [ -> ]Thanks for the interesting clarification

You're welcome.

Getting a Solver to be nestable isn't easy, that's why many aren't. If you're interested to know how the HP-71B's FNROOT does it, have a look at this enlightening explanation in the HP Journal July 1984 issue, beginning at page 30 (nesting is explained at page 32; up to 4 additional levels of nesting are possible, for a total of 5 levels, which allows for solving systems of up to 5 non-linear equations.)

Regards.
V.
(12-18-2019 03:02 AM)Valentin Albillo Wrote: [ -> ]
(12-17-2019 04:16 PM)Stevetuc Wrote: [ -> ]Thanks for the interesting clarification

You're welcome.

Getting a Solver to be nestable isn't easy, that's why many aren't. If you're interested to know how the HP-71B's FNROOT does it, have a look at this enlightening explanation in the HP Journal July 1984 issue, beginning at page 30 (nesting is explained at page 32; up to 4 additional levels of nesting are possible, for a total of 5 levels, which allows for solving systems of up to 5 non-linear equations.)

Regards.
V.

Very informative article, thanks.
I'm wondering if some of this functionality might be achieved on the prime by sending the arguments unevaluated using Quote()- would this allow nesting? (I tried..unsuccessfully). I'm sure there are many subtleties! The issue on the HP71B was that stack passing could not be used so the arguments needed to be passed unevaluated to the function. Also passing and keeping track of FVAR. Much of this is native to the prime.
Maybe Cyrille or Tim could answer - was nesting FNROOT a design consideration on the prime ?
This shows (on XCas) that FNROOT can not be nested

XCas>FNF(v):={local FVAR; purge(FVAR); print(v); FNROOT(Gamma(FVAR)=v,FVAR=0..1)[0]}

XCas>m := makelist(FNF, 1.75, 1.8, 0.01)
v:1.75
v:1.76
v:1.77
v:1.78
v:1.79
v:1.8
→ [0.506546500849,0.503607372216,0.500706197505,0.497842178366,0.495014540318,0.492​222531811]

XCas>map(m, Gamma) ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿﻿// confirm FNF = Gamma-1, at least for domain [1.75,1.8]
→ [1.75,1.76,1.77,1.78,1.79,1.8]

XCas>FNROOT(FNF(sqrt(x))=0.5, x=3) ﻿ ﻿ ﻿// unable to get value of sqrt(x) into FNF
v:sqrt(x)
"Error: Invalid dimension"
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :