Post Reply 
HP49-50G: help for recursive
02-04-2024, 12:06 PM (This post was last modified: 02-04-2024 01:10 PM by Gil.)
Post: #7
RE: HP49-50G: help for recursive
Here below FLN5, almost the same recursive program as FLN4.

Difference
Here, contrarily to previous recursive program FLN4, we don't play fully with the stack (with such commands as SWAP or others) after the call of FLN5 in the THEN branch, but with the variable x (the different, calculated variables x) explicitly called back each time (2* x² -).

Observation 1
Such a solution might be clearer if the above expression in x in the parenthesis (2* x² -, in our case) were really complicated to play fully with the stack commands such as SWAP — and that we had instead an algebraic expression 'f(x)'.

Observation 2
Regarding the ELSE branch, it concerns normally the last n transformed value of x, x[n].
Therefore, when we jump back, at the end of the ELSE branch, to the THEN branch, the x value of that THEN branch won't be anymore x[n], but the previous one, x[n-1].
In other words, in the THEN branch, the expected execution of 2* x[n]² - will never occurr.
However, it must be executed exactly once before the execution of 2* x[n-1]² in the THEN branch.
Remedy: add that missing special 2* x[n]² - at the end of the ELSE branch, just before the jump to the THEN branch.

Observation 3
The above remedy should never be applied for the special cases when the absolute value of (initial input xo) <0.4, ie when the THEN branch was never executed.
Hence a special test (with the variable 'max') at the end of that ELSE branch.

Observation 4
We don't want that, in the THEN branch, the sequence 2* x²- to be carried out when last called back value of x is equal to initial input xo. In other words, the sequence 2* x²- in the THEN branch is ok for x[n-1], x[n-2], ..., x[1], but not for x[0].
Hence a special test (with the variable 'max') at the end of that THEN branch.

« 0 0 -> x X2 X4
«
IF x ABS .4 >≠
THEN
IFERR 'max' RCL
THEN x SWAP STO
ELSE DROP
END 'x/(sqrt(1+x)+1)' ->NUM FNL5
IF x max ≠
THEN 2 * x SQ -
ELSE 'max' PURGE
END
ELSE 'x/(x+2)' ->NUM 'X2' STO X2 DUP * 'X4' STO 'X4*(5005-X4*(5082-X4*969))/(15015-X4*(24255-X4*(11025-X4*1225)))' ->NUM 'X4' STO 'X2*(X4+X4-x)' ->NUM
IFERR 'max' RCL
THEN DROP
ELSE DROP 2 * x SQ -
END
END
»
»

Learning purpose
My solution with the 'max' variable seems to me very cumbersome.
As I lack of practice with recursive programming, if somebody comes with a simpler/better approach for that recursive program playing with the algebraic mode/expression containing the effective x[i] called back variables (such as 'f(x)', and not fully with the stack), it would be of course most appreciated.

Code:
\<< 0 0 \-> x X2 X4
  \<<
    IF x ABS .4 \>=
    THEN
      IFERR 'max' RCL
      THEN x SWAP STO
      ELSE DROP
      END 'x/(\v/(1+x)+1)' \->NUM FNL5
      IF x max \=/
      THEN 2 * x SQ -
      ELSE 'max' PURGE
      END
    ELSE 'x/(x+2)' \->NUM 'X2' STO X2 DUP * 'X4' STO 'X4*(5005-X4*(5082-X4*969))/(15015-X4*(24255-X4*(11025-X4*1225)))' \->NUM 'X4' STO 'X2*(X4+X4-x)' \->NUM
      IFERR 'max' RCL
      THEN DROP
      ELSE DROP 2 * x SQ -
      END
    END
  \>>
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
HP49-50G: help for recursive - Gil - 02-02-2024, 12:46 PM
RE: HP49-50G: help for recursive - Gil - 02-02-2024, 01:52 PM
RE: HP49-50G: help for recursive - Gil - 02-02-2024, 06:13 PM
RE: HP49-50G: help for recursive - Gil - 02-03-2024, 10:19 AM
RE: HP49-50G: help for recursive - Gil - 02-04-2024 12:06 PM
RE: HP49-50G: help for recursive - Gil - 02-06-2024, 11:57 AM
RE: HP49-50G: help for recursive - DavidM - 02-06-2024, 01:14 PM
RE: HP49-50G: help for recursive - Gil - 02-06-2024, 01:29 PM
RE: HP49-50G: help for recursive - DavidM - 02-06-2024, 03:51 PM
RE: HP49-50G: help for recursive - Gil - 02-06-2024, 05:48 PM
RE: HP49-50G: help for recursive - DavidM - 02-06-2024, 08:51 PM
RE: HP49-50G: help for recursive - Gil - 02-06-2024, 08:53 PM



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