CoCalc Public FilesNewtons Method for Cube Root.ipynb
Author: Julia Burnside
Views : 218
Compute Environment: Ubuntu 20.04 (Default)

Newton's Method to approximate cube roots

Approximating $\sqrt[3]{4}$

In [2]:
def newton(c, dc, x, n, err):
i=0
while abs(c(x)) > err and i <=n:
x=x-c(x)/dc(x)
i+=1
if i > n:
return False
else:
return x

def cubic(x):
return x**3-4

def cubicDerivative(x):
return 3*x**2

print("cube root of 4 = 4^(1/3) =", 4**(1/3), '\n')
print("Using Newton's Method:", '\n')
print("1st guess = 1:", '\n', newton(cubic, cubicDerivative, 1, 4, 0.0001), '\n')
print("2nd guess = 1.5:", '\n', newton(cubic, cubicDerivative, 1.4, 5, 0.0001),'\n')
print("3rd guess = 1.5 (decreasing err to 0.0000000001):", '\n', newton(cubic, cubicDerivative, 1.5, 4, 0.0000000001),"<-- this is the most accurate")


cube root of 4 = 4^(1/3) = 1.5874010519681994 Using Newton's Method: 1st guess = 1: 1.5874096961416333 2nd guess = 1.5: 1.587401164777749 3rd guess = 1.5 (decreasing err to 0.0000000001): 1.5874010519681996 <-- this is the most accurate
In [ ]: