Saturday, December 5, 2026

Python: Secant Method (Casio fx-CG 100)

Python: Secant Method (Casio fx-CG 100)



Solving by the Secant Method



We have several ways of numerically finding roots to the equation f(x) = 0. My favorite is the good old Newton’s Method:


x_n = x_n-1 + f(x_n-1) ÷ f’(x_n-1)


Another way is the Secant Method:


x_n = x_n-1 – f(x_n-1) * ((x_n-1 – x_n-2) ÷ (f(x_n-1) – f(x_n-2))


The Secant Method differs from Newton’s Method in the following ways:


* The Secant method does not require the derivative of f(x). All we need is the function. This makes programming easier when a derivative command isn’t easily available or calculating the derivative numerically is not feasible.

* The Secant method requires two initial points. Ideally these two points surround the root. Say the initial points are x = a and x = b. We want either f(a) < 0 and f(b) > 0, or f(a) >0 and f(b) < 0.


The script was made with a Casio fx-CG 100. The script imports the math module and should be able to applied to any calculate or software that runs Python.



Casio fx-CG 100 Python: Secant Method



The script shows the results rounded to 10 decimal places (via the format command).


'''

Secant Method

EWS 2026-08-21

'''


from math import *

print("math module imported")

f=input("enter f(x): ")

a=eval(input("x0? "))

b=eval(input("x1? "))


# setup

s=1 # control variable

i=0 # iterations


# loop

while s>1*10**-12 and i<500:

  x=a

  fa=eval(f)

  x=b

  fb=eval(f)

  c=b-fb*((b-a)/(fb-fa))

  s=abs(c-b)

  a=b

  b=c

  i+=1

# while end


x=c

fc=eval(f)

print("to 10 places")

print("root: {0:.10f}".format(c))

print("value: {0:.10f}".format(fc))

print("iter: ",str(i))


Examples


Example 1: f(x) = 2 * x^3 – x – 3, x_0 = 1, x_1 = 2


Results:

Root ≈ 1.2896239015

f(root) = 0


Example 2: f(x) = -x^2 + 7 * x – 12, x_0 = 4, x_1 = 5


Results:

Root ≈ 4.6180339887

f(root) = 0


Example 3: f(x) = exp(-x) – 5, x_0 = -2, x_1 = -1 (exp(-x) = e^(-x))


Results:

Root ≈ -1.36094379124

f(root) = -0 (it’s really 0, Python deals with floating decimal)


Example 4: f(x) = (x – 2) * (x + 6), x_0 = 5, x_1 = 0


Results:

Root = 2

f(root) = 0



Source


“Secant Method” Wikipedia. Last Edited July 4, 2026. Accessed August 20, 2026.

https://en.wikipedia.org/wiki/Secant_method




Eddie


All original content copyright, © 2011-2026. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.