Q. The objective of this exercise is to implement the secant method (a) Write a simple program to implement the secant method to locate the root of the equation g(x)=00. For the stoppoing criterion, we condition $|x^{(k+1)}-x^{(k)}| <|{x^{(k)}}|\epsilon$
In [134]:
from math import *
import numpy as np
# Function definition
def g(x):
#return (1/2)*(x**2)- sin(x)
return x**3 - 12.2*x**2 + 7.45*x + 42
In [156]:
# Implementation of Secant method
def secant(x0,x1,eps):
i=0
x2 = ((g(x1)*x0) - (g(x0)*x1))/(g(x1)-g(x0))
x2 = np.float("{:.2f}".format(x2))
arr = np.empty((5), dtype=object)
arr1 = np.empty((5), dtype=object)
arr[0] = i; arr[1] = x0; arr[2] = x1; arr[3] = x2; arr[4] = g(x2)
while abs(x2-x1) > abs(x1)*eps:
i = i+1
x0 = x1
x1 = x2
x2 = ((g(x1)*x0) - (g(x0)*x1))/(g(x1)-g(x0))
x2 = np.float("{:.2f}".format(x2))
arr1[0] = i;arr1[1] = x0; arr1[2] = x1; arr1[3] = x2; arr1[4] = g(x2)
arr = np.vstack((arr,arr1))
arr1 = np.array(["Iter-k","x0","x1","x2","g(x)"])
arr = np.vstack((arr1,arr))
for i in arr:
print(i)
In [157]:
x0 = 13
x1 = 12
eps = 0.00001
secant(x0,x1,eps)
['Iter-k' 'x0' 'x1' 'x2' 'g(x)'] [0 13 12 11.4 22.96200000000016] [1 12 11.4 11.23 3.3339870000003202] [2 11.4 11.23 11.2 -5.684341886080802e-14] [3 11.23 11.2 11.2 -5.684341886080802e-14]
(b) Let $g(x) = (2x-1)^2 + 4(4-1024x)^4$. Find the root of $g(x)=0$ using the secant method with $x^{(-1)}=0, x^{(0)}=1, \epsilon = 10^{-5}$.Also determine the value of g at the solution obtained.
In [168]:
# Function definition
def g1(x):
return ((2*x-1)**2) + (4*(4-1024*x)**4)
# Implementation of Secant method
def secant(x0,x1,eps):
i=0
x2 = ((g1(x1)*x0) - (g1(x0)*x1))/(g1(x1)-g1(x0))
arr = np.empty((5), dtype=object)
arr1 = np.empty((5), dtype=object)
arr[0] = i; arr[1] = x0; arr[2] = x1; arr[3] = x2; arr[4] = g1(x2)
while abs(x2-x1) > abs(x1)*eps:
i = i+1
x0 = x1
x1 = x2
x2 = ((g1(x1)*x0) - (g1(x0)*x1))/(g1(x1)-g1(x0))
arr1[0] = i;arr1[1] = x0; arr1[2] = x1; arr1[3] = x2; arr1[4] = g1(x2)
arr = np.vstack((arr,arr1))
arr1 = np.array(["Iter-k","x0","x1","x2","g(x)"])
arr = np.vstack((arr1,arr))
for i in arr:
print(*i)
In [170]:
x0 = 0
x1 = 1
eps = 0.00001
secant(x0,x1,eps)
Iter-k x0 x1 x2 g(x) 0 0 1 -2.3673539047528324e-10 1025.0002482360187 1 1 -2.3673539047528324e-10 -4.734708383395378e-10 1025.0004964721415 2 -2.3673539047528324e-10 -4.734708383395378e-10 0.0009775121818762856 324.57618912953166 3 -4.734708383395378e-10 0.0009775121818762856 0.0014304909394271194 166.22596917864954 4 0.0009775121818762856 0.0014304909394271194 0.0019059991705781961 71.39644245661505 5 0.0014304909394271194 0.0019059991705781961 0.00226400576658851 32.980711093943384 6 0.0019059991705781961 0.00226400576658851 0.0025713619379775926 14.954685514919948 7 0.00226400576658851 0.0025713619379775926 0.0028263496425075542 6.9700123975749015 8 0.0025713619379775926 0.0028263496425075542 0.0030489345167639906 3.3637104229028876 9 0.0028263496425075542 0.0030489345167639906 0.0032565463769951347 1.7706641313485134 10 0.0030489345167639906 0.0032565463769951347 0.0034873060706102775 1.1215819905072293 11 0.0032565463769951347 0.0034873060706102775 0.003886047462814129 0.9845169482362165 12 0.0034873060706102775 0.003886047462814129 0.006750145062636765 288.65610743221816 13 0.003886047462814129 0.006750145062636765 0.00387624547776693 0.9845586837711187 14 0.006750145062636765 0.00387624547776693 0.0038664095283834055 0.9846052388347548 15 0.00387624547776693 0.0038664095283834055 0.21188948017907833 8229493255.343119 16 0.0038664095283834055 0.21188948017907833 0.0038664095034948004 0.9846052389612274 17 0.21188948017907833 0.0038664095034948004 0.0038664094786061954 0.9846052390877005