#Solution of non-linear equations using Secant Method
Algorithm
1. Start
2. Define function f(x)
3. Define secant(x₀, x₁, e)
4. Initialize count = 1
5. Compute f(x₀) and f(x₁)
6. loop:
• Compute x₂ = x₁ - f(x₁) * (x₁ - x₀) / (f(x₁) - f(x₀))
• Compute f(x₂)
• Display iteration number, x₀, x₁, and f(x₂)
• If |f(x₂)| < e, break
• Update values: x₀ = x₁, f(x₀) = f(x₁) x₁ = x₂, f(x₁) = f(x₂)
• Increment count
1. Call function secant()
2. Stop
Code
def fun(x):
return x**3 - 4*x - 9
def secant(x0, x1, e):
count = 1
fx0 = fun(x0)
fx1 = fun(x1)
print(f"{'Iter'}\t{'x0'}\t\t{'x1'}\t\t{'f(x2)'}")
while (1):
x2 = x1 - (fx1 * (x1 - x0)) / (fx1 - fx0)
fx2 = fun(x2)
print(f"{count}\t{x0:.8f}\t{x1:.8f}\t{fx2:.8f}")
if abs(fx2) < e:
break
x0 = x1
fx0 = fx1
x1 = x2
fx1 = fx2
count += 1
x0 = 2
x1 = 3
e = 0.00001
secant(x0, x1, e)
Iter x0 x1 f(x2)
1 2.00000000 3.00000000 -1.82400000
2 3.00000000 2.60000000 -0.23722651
3 2.60000000 2.69325153 0.01195595
4 2.69325153 2.70719287 -0.00007197
5 2.70719287 2.70652395 -0.00000002
Error vs iteration curve of Secant method
Algorithm
1. Start
2. Define function f(x)
3. Define secant(x₀, x₁, e)
4. Initialize:
• count = 1
• fx₀ = f(x₀)
• fx₁ = f(x₁)
• Create empty lists errors, iterations
1. Repeat
• Calculate new approximation: x₂ = x₁ - f(x₁) * (x₁ - x₀) / (f(x₁) - f(x₀))
• Evaluate f(x₂)
• Append |x₁ - x₀| to errors, count to iterations
• Display iteration data
• If |f(x₂)| < e, exit loop
• Update values: x₀ = x₁, fx₀ = fx₁ x₁ = x₂, fx₁ = fx₂
• Increment count
1. Return iterations, errors
2. Plot graph of errors vs iterations
3. Stop
import matplotlib.pyplot as plt
def fun(x):
return x**3 - 4*x - 9
def secant(x0, x1, e):
errors = []
iterations = []
count = 1
fx0 = fun(x0)
fx1 = fun(x1)
print(f"{'Iter'}\t{'x0'}\t\t{'x1'}\t\t{'f(x2)'}")
while (1):
x2 = x1 - (fx1 * (x1 - x0)) / (fx1 - fx0)
fx2 = fun(x2)
print(f"{count}\t{x0:.6f}\t{x1:.6f}\t{fx2:.6f}")
error = x1 - x0
errors.append(abs(error))
iterations.append(count)
if abs(fx2) < e:
break
x0 = x1
fx0 = fx1
x1 = x2
fx1 = fx2
count += 1
return iterations, errors
x0 = 2
x1 = 3
e = 0.00001
iterations, errors = secant(x0, x1, e)
plt.plot(iterations, errors, label='Error', color='black', marker='*')
plt.title("Error vs Iteration curve")
plt.xlabel("No. of Iteration ----->")
plt.ylabel("Error ----->")
plt.legend()
plt.grid(True)
plt.show()
Iter x0 x1 f(x2)
1 2.000000 3.000000 -1.824000
2 3.000000 2.600000 -0.237227
3 2.600000 2.693252 0.011956
4 2.693252 2.707193 -0.000072
5 2.707193 2.706524 -0.000000
#Solution of non-linear equations using Newton raphson method Algorithm
1. Start
2. Define function f(x)
3. Define derivative function f′(x)
4. Define NewtonRaphson(x₀, e)
5. Initialize count = 1
6. loop
• Compute:
• use initial guess x₀ * x₁ = x₀ − f(x₀) / f′(x₀)
• Evaluate f(x₁)
• Display iteration data
• If |f(x₁)| < e, exit loop
• Update x₀ = x₁
• Increment count
1. Stop
import math
def fun(x):
return x**3 - 4*x + 1
def derivative(x):
return 3*x**2 - 4
def NewtonRaphson(x0, e):
count = 1
print(f"{'Iter'}\t{'x0'}\t\t{'x1'}\t\t{'f(x2)'}")
while (1):
x1 = x0 - fun(x0) / derivative(x0)
fx1 = fun(x1)
print(f"{count}\t{x0:.6f}\t{x1:.6f}\t{fx1:.6f}")
if abs(fx1) < e:
break
x0 = x1
count += 1
x0 = 1
e = 0.0001
NewtonRaphson(x0, e)
Iter x0 x1 f(x2)
1 1.000000 -1.000000 4.000000
2 -1.000000 3.000000 16.000000
3 3.000000 2.304348 4.018739
4 2.304348 1.967489 0.746223
5 1.967489 1.869470 0.055768
6 1.869470 1.860871 0.000414
7 1.860871 1.860806 0.000000
Error vs Iteration curve of Newton Raphson method
Algorithm
1. Start
2. Define function f(x)
3. Define derivative f′(x)
4. Define NewtonRaphson(x₀, e)
5. Initialize count = 1
6. Create empty lists errors, iterations
7. loop
• Compute new approximation: x₁ = x₀ - f(x₀) / f′(x₀)
• Evaluate f(x₁)
• Append |x₁ - x₀| to errors, count to iterations
• Display iteration data
• If |f(x₁)| < e, exit loop
• Update: x₀ = x₁
• Increment count
• Return iterations, errors
1. Plot graph of errors vs iterations
2. Stop
import math
import matplotlib.pyplot as plt
def fun(x):
return x**3 - 4*x + 1
def derivative(x):
return 3*x**2 - 4
def NewtonRaphson(x0, e):
errors = []
iterations = []
count = 1
print(f"{'Iter'}\t{'x0'}\t\t{'x1'}\t\t{'f(x2)'}")
while (1):
x1 = x0 - fun(x0) / derivative(x0)
fx1 = fun(x1)
print(f"{count}\t{x0:.6f}\t{x1:.6f}\t{fx1:.6f}")
error = (x1 - x0)
errors.append(abs(error))
iterations.append(count)
if abs(fx1) < e:
break
x0 = x1
count = count + 1
return iterations, errors
x0 = 1
e = 0.00001
iterations, errors = NewtonRaphson(x0, e)
plt.plot(iterations, errors, label='Error', color='black', marker='*')
plt.title("Error vs Iteration curve")
plt.xlabel("No of Iteration ----->")
plt.ylabel("Error ----->")
plt.legend()
plt.grid(True)
plt.show()
Iter x0 x1 f(x2)
1 1.000000 -1.000000 4.000000
2 -1.000000 3.000000 16.000000
3 3.000000 2.304348 4.018739
4 2.304348 1.967489 0.746223
5 1.967489 1.869470 0.055768
6 1.869470 1.860871 0.000414
7 1.860871 1.860806 0.000000