(12C) Secant Method solution of f(x)=0
03-31-2018, 12:43 PM (This post was last modified: 04-02-2018 01:49 PM by Gamo.)
Post: #1
 Gamo Senior Member Posts: 497 Joined: Dec 2016
(12C) Secant Method solution of f(x)=0
This Secant Method used only 29 line in the program but come with a cost of more steps to setup.

Setup Procedure:
1. Store Two Guesses at R0 and R2
2. Store f(x) end point at R1
3. Store Tolerant at R4
4. Store Interval at R3
5. GTO 03
6. R/S

Example: X^X=Y
Problem: X^X=1000
f(x) Program:
GTO 28
P/R
LN
LSTx
x
EEX
3
LN
-
STO1
GTO 03
P/R

Setup:
1. 3 STO 0, 5 STO 2
2. 1 STO 1
3. EEX CHS 6 STO 4
4. 1 CHS STO 3
5. GTO 03
6. R/S

Program:
Code:
 01 RCL 0    // Result 02 R/S       // ........and stop 03 RCL 2    // Secant Method start here 03 to 18 04 RCL 0 05 - 06 LSTx 07 STO 2 08 CLx 09 RCL 3 10 RCL 1 11 - 12 LSTx 13 STO 3 14 Rv 15 / 16 RCL 1 17 x 18 STO-0 19 RCL 0       //Stop Condition: relative error of the root 20 / 21 2             //ABS function 22 y^x 23 SQRT 24 RCL 4 25 X<>Y 26 X<=Y 27 GTO 01    //Root is found 28 RCL 0 29 .............Start f(x) here . . . STO 1 GTO 03........Iteration Loop and End f(x)

Gamo
04-02-2018, 08:01 AM (This post was last modified: 04-02-2018 12:55 PM by Dieter.)
Post: #2
 Dieter Senior Member Posts: 2,398 Joined: Dec 2013
RE: (12C) Secant Method solution of f(x)=0
(03-31-2018 12:43 PM)Gamo Wrote:  Setup Procedure:
1. Store Two Guesses at R0 and R2
2. Store f(x) end point at R1
3. Store Tolerant at R4
4. Store Interval at R3
5. GTO 03
6. R/S

What is "f(x) end point" in step 2 ? Why is it 1 in the example?
And what is the "interval" in step 4 ? Why is it –1 in the example?
I get an Error 0 if I run the program with the example data.

Looking at the program, I think the values in R1 (step 2) and R3 (step 4) should be f(x1) and f(x2).

(03-31-2018 12:43 PM)Gamo Wrote:  Example: X^X=Y
Problem: X^X=1000
f(x) Program:
GTO 28
P/R
LN
LSTx
x
EEX
3
-
STO1
GTO 03
P/R

Here f(x) is rewritten as x*ln(x) – ln 1000 = 0
So there is a "LN" missing after the EEX 3.

And here's a tip: You can elegantly store x1 and f(x1) during the calculation.

Code:
RCL 2 RCL 0 STO 2 - RCL 3 RCL 1 STO 3 - /

And another tip: if you want to check if |x| is less than R4 (error tolerance) you can also divide x by R4 and check if the integer part is zero. This is both shorter and faster, three steps instead of six:

Code:
... RCL 4 / INT X=0? GTO 01

BTW checking the relative error (division by R0) may cause problems since R0 can be zero. You can avoid this if you replace this value with 1:

Code:
... STO-0 RCL 0 x=0? e^x /

Maybe you can try this version:

Code:
01 STO 4 02 Rv 03 STO 3 04 Rv 05 STO 2 06 Rv 07 STO 1 08 RCL 1 09 RCL 2 10 STO 1 11 - 12 RCL 3 13 RCL 4 14 STO 3 15 - 16 / 17 RCL 4 18 x 19 STO-2 20 RCL 2 21 X=0? 22 e^x 23 / 24 EEX 25 8 26 x 27 INTG 28 X=0? 29 GTO 31 30 GTO 33 31 RCL 2 32 GTO 00 33 RCL 2 34 ENTER    // start f(x) 35 LN 36 x 37 EEX 38 3 39 LN 40 -   . 41 STO 4    // end f(x) with 42 GTO 08   // these two lines

You have to enter the two guesses x1 and x2 as well as their respective function values f(x1) and f(x2). The program expects them on the stack. You can also store them manually in R1...R4 and remove the first seven lines, which makes the program much shorter. In this case, here is the register mapping:

R1:  x1
R2:  x2
R3:  f(x1)
R4:  f(x2)

If in the example the guesses are 3 and 5 their function values are f(3)=–3.611918412 and f(5)=1.139434281. But usually rounded values will do:

3 [ENTER] 5  [ENTER]  –3,6 [ENTER] 1,1  [R/S]
=> 4,555535706

You can even round f(x1) to –4 and f(x2) to 1 and it still works. If you're lazy you may even try just the signs of f(x), here –1 and +1 (which indeed is enough to return a correct result).

An input for the relative error threshold is not required, the program quits if it drops below 1E–8 which usually means that the result is as good as it gets.

However, I think the already posted modified Regula Falsi / Illinois algorithm works better than the Secant Rule, so it should be preferred.

Dieter
 « Next Oldest | Next Newest »

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