HI
Ubuntu 18.04,Jupyter notebook 9.0
from copy import deepcopy
''' Newton’s method. '''
show(LatexExpr(r"f'(x_n) \, (x-x_n) + f(x_n)"))
show(LatexExpr(r"x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}"))
print("source :","https://en.wikipedia.org/wiki/Newton%27s_method")
rd=5 # rounding display
def N_f(x_0=0.5,f=x^2,delta=10^(-6),n=0,xL=[]) :
global rRg,rd
"""real root approximation"""
fd=f.derivative(x)
epsilon=abs(f(x_0))
xnPlus1=0
xL.append((n,round(x_0,rd)))
rRg=[]
if epsilon > delta and n<100 and fd(x_0)!=0:
n+=1
xnPlus1=x_0-f(x_0)/fd(x_0)
print('x_'+str(n)+' : ',round(x_0,rd),'\t x_'+str(n+1),' : ',round(xnPlus1,rd),
' epsilon : ',round(epsilon,rd),' delta : ',round(delta,rd))
#xL.append((n,xnPlus1))
return N_f(xnPlus1,f,delta,n,xL)
else:
return x_0,n+1,delta,xL
'''test of function N_f '''
#R.<x> = PolynomialRing(RR)
f=x^3 - 2*x^2 + x - 1
for r in f.roots():
#show(r)
if r[0].imag_part()==0 :
realRoot=r[0]
show("real root: ",r[0])
show("real root numerical value:\t",r[0].n())
G = plot(f, (x, -1/2, 2), thickness=2, color='green', title="f(x)")
Gp0=point((realRoot,0),color='red',size=40)
Gt0 = text(r"$%s $" %(latex(f)),(0.75,1),color='green')
Gt1 = text(r"$%s $" %(latex(round(realRoot,rd))),(realRoot*(1+1/8),0.1),color='red')
Gl0=line([(0,realRoot), (2*realRoot,realRoot)],color='blue')
f_Coeffs=f.coefficients(sparse=False)
try: deltag
except NameError: deltag = None
if deltag ==None :
deltag= 5*10^(-2)
try: x_0g
except NameError: x_0g = None
if x_0g ==None :
x_0g=(realRoot-1/2).n()
#print(' \t \t delta : ',round(delta,rd))
GG=G+Gp0+Gl0+Gt0+Gt1
nbColorMaxg=16
rainbowG=rainbow(nbColorMaxg, 'rgbtuple')
try: colorNg
except NameError: colorNg = None
if colorNg ==None :
colorNg= 0
try: newtonG
except NameError: newtonG = None
if newtonG ==None :
newtonG= Graphics()
try: rRg
except NameError: rRg = None
if rRg ==None :
rRg=[]
@interact
#def _(x_0=input_box(round(x_0g,rd), width=20, label="x_0=")):
def _( deltal=slider(0.0001,0.5,0.0001,0.3)):
global GG,newtonG,delta,x_0g,colorNg,f,nbColorMaxg,realRoot,rRg
deltag=deltal;
if rRg != [] :
rRg=[]
rRg=N_f(x_0=x_0g,f=f,delta=deltag)
newtonG= GG
if rRg[3] !=[] :
newtonG=list_plot(rRg[3],color=rainbowG[colorNg % (nbColorMaxg-1) ],size=40)
colorNg+=1
(GG+newtonG).show()
#show(rRg[3])
Q1) How to supress warning messages: SageMath/local/lib/python3.7/site-packages/sage/repl/ipython_kernel/__main__.py:85: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR (x = ..., y = …) See http://trac.sagemath.org/5930 for details.
Q2) why do the values of the convergence curves accumulate in rRg [3]?. At the initialization we read in rRg:
[1.81956500676553,
4,
0.300000000000000,
[(0, 1.25488), (1, 2.55835), (2, 2.05721), (3, 1.81957)]]
Then after having positioned the cursor on the far left we read in rRg:
[1.75489290022857,
6,
0.000100000000000000,
[(0, 1.25488),
(1, 2.55835),
(2, 2.05721),
(3, 1.81957),
(0, 1.25488),
(1, 2.55835),
(2, 2.05721),
(3, 1.81957),
(0, 1.25488),
(1, 2.55835),
(2, 2.05721),
(3, 1.81957),
(4, 1.75876),
(0, 1.25488),
(1, 2.55835),
(2, 2.05721),
(3, 1.81957),
(4, 1.75876),
(5, 1.75489)]]
despite I clear rRg:
if rRg != [] :
rRg=[]
do a list(rRg) in the next cell and run it