You are on page 1of 3

22/10/2019 Problem5NewtonRaphsonMethod

Problem 5: Newton-Raphson Method

localhost:8891/nbconvert/html/Problem5NewtonRaphsonMethod.ipynb?download=false 1/3
22/10/2019 Problem5NewtonRaphsonMethod

In [23]: import matplotlib.pyplot as plt


import numpy as np

if __name__ == "__main__":
def function(x):
return (9.8*x/14)*(1 - np.exp(-1*14*10.22/x)) - 48.7564 # The main fun
ction
def derivative(x):
return (9.8/14)*(1 - (np.exp(-1*14*10.22/x)*(1 + (14*10.22/x)))) # The
derivative

def newton(function, derivative, x0, tolerance, number_of_max_iterations=100):


x1 = 0

if abs(x0-x1)<= tolerance and abs((x0-x1)/x0)<= tolerance:


return x0

print("k\t x0\t\t function(x0)")


k = 1

while k <= number_of_max_iterations:


x1 = x0 - (function(x0)/derivative(x0))
print("x%d\t%e\t%e"%(k,x1,function(x1)))

if abs(x0-x1)<= tolerance and abs((x0-x1)/x0)<= tolerance:


plt.plot(x0, function(x0), 'or')
return x1

x0 = x1
k = k + 1
plt.plot(x0, function(x0), 'or')

# Stops the method if it hits the number of maximum iterations


if k > number_of_max_iterations:
print("ERROR: Exceeded max number of iterations")

return x1 # Returns the value

F = newton(function, derivative, 1.7, 0.0000001)


print("The approximate value of x is: "+str(F))

# Plotting configuration
u = np.arange(1.0, 2.0, 0.0001) # Setting up values for x in the plot
w = (9.8*x/14)*(1 - np.exp(-1*14*10.22/x)) - 48.7564 # Define the main functio
n again

plt.plot(u, w)
plt.axhline(y=0.0, color='black', linestyle='-')
plt.title('Newton-Raphson Graphics for' + ' y = (9.8*x/14)*(1 - np.exp(-1*14*1
0.22/x)) - 48.7564')
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)
plt.legend(['Xn'], loc='upper left')
plt.show()

localhost:8891/nbconvert/html/Problem5NewtonRaphsonMethod.ipynb?download=false 2/3
22/10/2019 Problem5NewtonRaphsonMethod

k x0 function(x0)
x1 6.965200e+01 -6.250266e+00
x2 8.432646e+01 -5.466393e-01
x3 8.587055e+01 -5.180202e-03
x4 8.588547e+01 -4.755761e-07
x5 8.588547e+01 0.000000e+00
x6 8.588547e+01 0.000000e+00
The approximate value of x is: 85.88546897608387

Since m = 85.88 from above, the actual mass now is


mparatroopper = mderived - 5kg mparatroopper = 85.88 - 5 mparatroopper = 80.88 kg

localhost:8891/nbconvert/html/Problem5NewtonRaphsonMethod.ipynb?download=false 3/3

You might also like