Post Reply 
(12C Platinum) Zero of Function
02-03-2019, 10:34 AM (This post was last modified: 02-04-2019 08:59 AM by Gamo.)
Post: #1
(12C Platinum) Zero of Function
ALG mode program for Secant Method Solution to f(x)=0

This program is very small and easy to use.
User need to give a very close pair of educated guess nearest to the root.

Remark:
This program don't have the tolerance setup but used the
iteration counter instead. The default counter is 65
Counter can be change on line 002 using two steps 50, 75, 90, etc.
-------------------------------------------------------
Procedure:

Start f(x) at line 022 and end GTO 008

1. First Guess [R/S] ... display # of iteration counter
2. Second Guess ... display Answer
---------------------------------------------------------
Example: FIX 4

x^3 + x^2 + x - 2 = 0

Insert Equation at line 022

[RCL] 1 [Y^X] 3 [+] [RCL] 1 [X^2] [+] [RCL] 1 [-] 2 [=] [GTO] 008

First guess 3 [R/S] display 65
Second guess 1 [R/S] display...........0.8105

Root = 0.8105
------------------------------------------------------
LN(x) + 3(x) - 10.8074

Insert Equation at line 022

[RCL] 1 [LN] + ([RCL]1 [x] 3) [-] 10.8074 [=] [GTO] 008

First guess 4 [R/S] display 65
Second guess 2 [R/S] display...........3.2134
-------------------------------------------------------
Program: ALG Mode
Quote:[STO] 0 65 [STO] 2 [R/S] [STO] 1
[GTO] 023
[÷][ RCL] 0 [CHS] [+] [RCL] 1 [=] [STO] 1
1 [STO]-2 [RCL] 2
[X=0]
[GTO] 021
[GTO] 023
[RCL] 1
[GTO] 000
. // Start Equation at line 022
.
.
. // Use [RCL] 1 for the unknown Xs
.
[GTO] 008

Remark:
This is not a perfect program, use at your own risk.
Any improvement is welcome for ALG programming mode only.

Gamo
Find all posts by this user
Quote this message in a reply
02-03-2019, 01:31 PM
Post: #2
RE: (12C Platinum) Secant Method
Hi, Gamo

I thought Secant's method required 2 points, thus 4 variables: (x0, f0), (x1,f1)
Interpolate the line to (x2, 0):

Slope = (f1-f0)/(x1-x0) = (f1-0)/(x1-x2)

--> x2 = x1 - f1/(f1-f0) * (x1-x0)

The method only use the most recent 2 points.
There should be some updates: x0,f0,x1,f1 = x1,f1,x2,f(x2)

For your code, I did not see f0 anywhere.

Also, your code seems to start the equation at line 023.
Find all posts by this user
Quote this message in a reply
02-03-2019, 05:57 PM (This post was last modified: 02-03-2019 06:31 PM by Dieter.)
Post: #3
RE: (12C Platinum) Secant Method
(02-03-2019 01:31 PM)Albert Chan Wrote:  I thought Secant's method required 2 points, thus 4 variables: (x0, f0), (x1,f1)
Interpolate the line to (x2, 0):

Take a look at the code and see what it does:
  1. This is not some kind of Secant method
  2. For inputs a and b the suggested method calculates b := b – f(b)/a
  3. This looks similar to Newton's method where f'(b) is replaced by a constant.
  4. Only b is an estimate for the root, while a is not.

The choice of a is crucial as it decides whether the iteration will converge at all. It doesn't matter if a is greater or less than b (which can be shown easily). The iteration may converge, diverge or oscillate between different values.

As a rough rule the value of a should be close to the slope (!) of the function near the root, and b should be close to the root itself. Again: only b is a guess for the root, but a isn't.

Consider the first example x³+x²+x–2 = 0. There is a root at ≈0,81. At this root the slope (derivative) is ≈4,6. At the initial guess b=1 the derivative is 6. So values for a in this range, say 4...6, will make the iteration converge well, similar to Newton's method.

Now try a=2 and b=1. The iteration does not converge and will finally oscillate between approx. 0,3 and 1,09.

Then try a=–1 and b=1. The iteration will diverge towards infinity (eventually causing an overflow error).

So this method only works under some exactly defined conditions.

Or, as Gamo put it:
(02-03-2019 10:34 AM)Gamo Wrote:  This is not a perfect program, use at your own risk.

;-)

Edit: here is another version of Gamo's program that might be more clear:

Code:
001 STO 0
002 R/S
003 STO 1
004 6
005 5
006 STO 2
007 GTO 020
008 ÷
009 RCL 0
010 =
011 STO-1
012 1
013 STO-2
014 RCL 2
015 1
016 x≤y
017 GTO 020
018 RCL 1
019 GTO 000
020 (place your f(x) here, where x = RCL 1)
... GTO 008

Dieter
Find all posts by this user
Quote this message in a reply
02-04-2019, 01:00 AM (This post was last modified: 02-04-2019 01:38 AM by Gamo.)
Post: #4
RE: (12C Platinum) Secant Method
Thanks to Albert Chan and Dieter for the review.

Dieter program suggestion is more refine and more clear.

--------------------------------------------------------------

I have try my original program for

X^X = Y equation for X^X = 1000

Using this formula: LN(x) * (x) - LN(y)

f(x) equation start line 022 and store 1000 in R5

[RCL] 1 [LN] [x] [RCL] 1 [-] ([RCL] 5 [LN]) [=] [GTO] 008

1000 [STO] 5
First Guess: 6 [R/S] display 65
Second Guess: 4 [R/S] display 4.5555

Fix 9 display 4.555535705

Not bad for when using a good guess.


Gamo
Find all posts by this user
Quote this message in a reply
02-04-2019, 05:19 PM (This post was last modified: 02-04-2019 05:24 PM by Dieter.)
Post: #5
RE: (12C Platinum) Zero of Function
(02-04-2019 01:00 AM)Gamo Wrote:  Dieter program suggestion is more refine and more clear.

The clarity essentially is in the formatting.
Use line numbers, one command per line, and you will also detect possible errors.
For instance that f(x) does not start on line 022 (which is GTO 000) but line 023.

(02-04-2019 01:00 AM)Gamo Wrote:  First Guess: 6 [R/S] display 65
Second Guess: 4 [R/S] display 4.5555

As already explained, the two inputs are not two guesses for the root. Only the second value (I called it "b") is an estimate, while the first one (I called it "a") is not.

Still not convinced? Then try this:
f(x) = 1000·x – 2000 = 0

There obviously is a root at x=2. If you think that the two input values are two guesses, try 3 and 1. Result: this will not converge, the iteration finally oscillates between plus and minus infinity.

Then try 3000 (!) and 1. This will eventually converge to the correct result x=2.
Try 1000 and 1, 100, 4711 or whatever, and the root is instantly found in the first iteration.

The first input "a" is not a guess for the root. It is a slope (!) that determines how fast the iteration converges – if at all. In the above example f'(x) = 1000 (constant), so the iteration directly returns the root.

To be honest, I don't think that this method is a good one for solving functions. Its essential advantage is its simplicity that requires just one function call – which is nice for calculators without subroutines.

If one wants to try this approach it is recommended to choose the first input close to the function's derivative at the root, and the second input (the guess) should be close to root itself.

Consider your last example: f(x) = x · ln(x) – ln(1000)

Let's start with a quite rough guess: the solution is somewhere between 1 and 10. The derivative for these values falls between 1 and ~3,3. So let's choose 2 as the first input. The second input, i.e. the initial guess, may be any positive value up to about 12, and the iteration will converge to the true root. It doesn't matter whether you enter 0,001 or 2 or 4,5 or 8 or 12 here. All these initial guesses will equally converge within roughly twenty iterations.

(02-04-2019 01:00 AM)Gamo Wrote:  Not bad for when using a good guess.

As shown, the initial guess is not that crucial; it's the first input, the slope, that decides whether the whole thing will work or not. If you choose 2,5 here (which is f'(4,55) it only takes a few iterations until the root is found – for any positive guess up to about 17.

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 




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