You are on page 1of 2

Problem 6

September 9, 2017

1 Problem 6 :
In [1]: import matplotlib.pyplot as plt
import numpy as np
import math

In [2]: def f(x):


return (3*x)*(1-math.cos(math.pi * x))

In [3]: def df(x):


return ((3*(1-math.cos(math.pi * x)))+(3*x*(math.pi)*(math.sin(math.pi * x))))

In [4]: p = np.zeros(11)
e = np.zeros(10)
p[0] = 0.5

In [5]: for i in range(0,10):


p[i+1] = p[i] - (f(p[i])/df(p[i]))
e[i] = np.abs(p[i+1] - p[i])
print("p : ",p)
print("e : ",e)

p : [ 0.5 0.30550774 0.19808816 0.13059723 0.0866527 0.05764891


0.03839751 0.02558798 0.01705559 0.01136949 0.00757939]
e : [ 0.19449226 0.10741958 0.06749092 0.04394453 0.02900379 0.0192514
0.01280953 0.00853239 0.0056861 0.0037901 ]

In [6]: plt.plot(e[1:10],e[0:9],"-x")
plt.show()
plt.grid(True)

1
In [7]: ratio = np.zeros(10)
for i in range(0,9):
ratio[i] = e[i+1]/(e[i])
print("Ratio : ",ratio)

Ratio : [ 0.55230772 0.62829255 0.65111763 0.66000921 0.66375466 0.66538145


0.66609721 0.66641392 0.6665544 0. ]

Order of convergence = 1. Hence, linear convergence

In [ ]:

You might also like