Exploring the Development of Concepts

We want to create a function, and find its roots using Newton-Raphson

Develop and Create a program to take in a function and then find a root of that function

In [1]:
import matplotlib.pyplot as plt
import numpy as np
%config InlineBackend.figure_format='retina'
In [2]:
def f(x):
    return x*(x**2-5)
In [3]:
f(2)
Out[3]:
-2
In [4]:
X=np.arange(-3,3,0.1)
In [7]:
plt.plot(X,f(X))
plt.grid()
In [8]:
def Df(x):
    h=0.00000000001
    return (f(x+h)-f(x))/h
In [9]:
Df(2)
Out[9]:
7.000000579182597
In [11]:
plt.plot(X,f(X))
plt.plot(X,Df(X),c='r')
plt.grid()
In [13]:
x0=2
xold=x0
for c in range(0,20):
    xnew = xold - f(xold)/Df(xold)
    xold=xnew
    print(xnew)
2.2857142620741815
2.2376402563442266
2.236069627476784
2.236067977507786
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
2.23606797749979
In [14]:
5**0.5
Out[14]:
2.23606797749979
In [26]:
x0=10000
xold=x0
xchecker=x0+1
xnew=x0-1
while ((xchecker-xnew)**2>0.0000000001) and ((xnew-xold)**2<10000):
    xchecker=xold
    xnew = xold - f(xold)/Df(xold)
    xold=xnew
    print(xnew)
6276.363822545454
4199.014158411462
2812.725148945905
1871.8546599147326
1240.9130488461228
827.788674837358
552.7466418551346
368.16744707023946
245.27264385536034
163.6126429956803
109.06674967851741
72.7328313496924
48.52497722306316
32.36649729656317
21.608259595281236
14.45876946210817
9.716429190192725
6.59363219121249
4.57098327614402
3.311476428708434
2.6033223721943504
2.301544960443691
2.238759897611542
2.2360728041070836
2.2360679775266097
In [36]:
def Newton(x0):
    xold=x0
    xchecker=x0+1
    xnew=x0-1
    count=1
    while ((xchecker-xnew)**2>0.0000000001) and (count<=1000):
        count+=1
        xchecker=xold
        xnew = xold - f(xold)/Df(xold)
        xold=xnew
    return xnew
In [38]:
Newton(-1)
Out[38]:
-0.9999998345192442
In [30]:
plt.plot(X,f(X))
Out[30]:
[<matplotlib.lines.Line2D at 0x7faafad20fd0>]
In [44]:
for c in range(-10,10):
    print(Newton(c))
-2.2360679775588634
-2.2360679775034065
-2.2360679774999053
-2.2360679774997876
-2.23606797750181
-2.236067977499795
-2.2360679775002037
-2.236067977500355
-2.2360679775077865
-0.9999998345192442
0.0
0.9999998345192442
2.236067977507786
2.236067977500396
2.2360679775002046
2.2360679774997916
2.2360679775018073
2.236067977499787
2.236067977499394
2.2360679775034105

silly word

In [45]:
import sympy as sym
In [73]:
x = sym.symbols('x')
In [74]:
x
Out[74]:
$\displaystyle x$
In [75]:
f = x*(x**2-5)
In [76]:
f
Out[76]:
$\displaystyle x \left(x^{2} - 5\right)$
In [65]:
Df = sym.diff(f,x)
In [66]:
Df
Out[66]:
$\displaystyle 3 x^{2} - 5$
In [67]:
f = sym.lambdify(x,f)
In [68]:
Df = sym.lambdify(x,Df)
In [69]:
X = np.arange(-2,1,0.01)
plt.plot(X,f(X))
plt.plot(X,Df(X))
Out[69]:
[<matplotlib.lines.Line2D at 0x7faafc3edd50>]
In [56]:
from scipy.optimize import newton
In [57]:
def g(x):
    return x*(x**2-5)
In [60]:
newton(g,2)
Out[60]:
2.23606797749979
In [77]:
sym.roots(f,x)
Out[77]:
{-sqrt(5): 1, sqrt(5): 1, 0: 1}
In [78]:
g(x)
Out[78]:
$\displaystyle x \left(x^{2} - 5\right)$
In [ ]: