Can you calculate Pi using a Solver (on the prime)?
12-17-2019, 08:08 AM (This post was last modified: 12-17-2019 09:25 AM by Stevetuc.)
Post: #1
 Stevetuc Member Posts: 268 Joined: Jan 2014
Can you calculate Pi using a Solver (on the prime)?
In this thread post #26
https://www.hpmuseum.org/forum/thread-14...#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.
12-17-2019, 03:54 PM
Post: #2
 Valentin Albillo Senior Member Posts: 779 Joined: Feb 2015
RE: Can you calculate Pi using a Solver (on the prime)?
.
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.

All My Articles & other Materials here:  Valentin Albillo's HP Collection

12-17-2019, 04:16 PM
Post: #3
 Stevetuc Member Posts: 268 Joined: Jan 2014
RE: Can you calculate Pi using a Solver (on the prime)?
(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-18-2019, 03:02 AM (This post was last modified: 12-18-2019 03:11 AM by Valentin Albillo.)
Post: #4
 Valentin Albillo Senior Member Posts: 779 Joined: Feb 2015
RE: Can you calculate Pi using a Solver (on the prime)?
(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.

All My Articles & other Materials here:  Valentin Albillo's HP Collection

12-18-2019, 05:51 AM (This post was last modified: 12-18-2019 06:10 AM by Stevetuc.)
Post: #5
 Stevetuc Member Posts: 268 Joined: Jan 2014
RE: Can you calculate Pi using a Solver (on the prime)?
(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 ?
12-18-2019, 01:24 PM
Post: #6
 Albert Chan Senior Member Posts: 1,676 Joined: Jul 2018
RE: Can you calculate Pi using a Solver (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"
 « Next Oldest | Next Newest »

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