HP Forums
Can you calculate Pi using a Solver (on the prime)? - 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: Can you calculate Pi using a Solver (on the prime)? (/thread-14184.html)



Can you calculate Pi using a Solver (on the prime)? - Stevetuc - 12-17-2019 08:08 AM

In this thread post #26
https://www.hpmuseum.org/forum/thread-14132-post-125210.html#pid125210

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.


RE: Can you calculate Pi using a Solver (on the prime)? - Valentin Albillo - 12-17-2019 03:54 PM

.
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.


RE: Can you calculate Pi using a Solver (on the prime)? - Stevetuc - 12-17-2019 04:16 PM

(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!


RE: Can you calculate Pi using a Solver (on the prime)? - Valentin Albillo - 12-18-2019 03:02 AM

(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.


RE: Can you calculate Pi using a Solver (on the prime)? - Stevetuc - 12-18-2019 05:51 AM

(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 ?


RE: Can you calculate Pi using a Solver (on the prime)? - Albert Chan - 12-18-2019 01:24 PM

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"