Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168729
Image: ubuntu2004
a = ComplexField()#e.g.: a(4,2) = 4+2i b = .5 c = -.5 d = a(.5,1) #.5+i e = a(.5,-1)#-.5+i TOL = 10^(-10)
def newton_con(p_0, constant): f_1 = derivative(f,x) return n(p_0 - (f(p_0)/f_1(p_0)))
f(x) = x^4 - 1 #the purpose method is to make the plot of the newtons method def plot_evaluation(p_0, tol, n_0): var('i') var('p') data = [] temp = [] con = p_0# to pass constant i=1 while i<=n_0: p = newton_con(p_0,con) data.append((p.real(),p.imag())) if abs(p - p_0) < tol: break p_0 = p i = i + 1 return data
#getting the data of roots pts_b = plot_evaluation(b,TOL,500) pts_c = plot_evaluation(c,TOL,500) pts_d = plot_evaluation(d,TOL,500) pts_e = plot_evaluation(e,TOL,500)
#plot the points for the roots in red,green,purple, blue plot_b = point(pts_b,rgbcolor=(1,0,0)) plot_c = point(pts_c,rgbcolor=(0,1,0)) plot_d = point(pts_d,rgbcolor=(.5,0,.5)) plot_e = point(pts_e,rgbcolor=(0,0,1)) graph = plot_b+plot_c+plot_d+plot_e graph.show(xmin = -2, xmax=2,ymin=-2,ymax=2)
def evaluation(p_0, tol, n_0,root): #the extra parameter in the definition, root, makes things awesome later on...you'll see why var('i') var('p') data = [] temp = [] con = p_0 i=1 #I need to to an if-else statement in order to process the original p_0's iteration inside the while loop while i<=n_0: if i == 1: temp = (Integer((i-1)),p_0, f(p_0), abs(p_0-root), abs(p_0-root)/abs(root)) data.append(temp) i = i +1 else: p = newton_con(p_0,con) #store the other data temp = ((i - 1),p,f(p),abs(p-root), abs(p-root)/abs(root)) data.append(temp) if abs(p - p_0) < tol: break p_0 = p i = i + 1 return data
#vvvvvv awesome-ness below vvvvvvv data_b = evaluation(b,TOL,500,1) data_c = evaluation(c,TOL,500,-1) data_d = evaluation(d,TOL,500,a(0,1)) data_e = evaluation(e,TOL,500,a(0,-1)) #^^^^^^ awesome-ness above ^^^^^^^
# put data into a matrix mat_b = matrix(data_b) mat_c = matrix(data_c) mat_d = matrix(data_d) mat_e = matrix(data_e)
#now to make a Latex array, If you want a table, #i just modified \left(\begin{array}{rrrrr} #to #\begin{tabular}{|r|r|r|r|r|} #and #\end{array}\right) #to #\end{tabular} #in my latex editor latex(mat_b)
\left(\begin{array}{rrrrr} 0.000000000000000 & 0.500000000000000 & -0.937500000000000 & 0.500000000000000 & 0.500000000000000 \\ 1 & 2.37500000000000 & 30.8166503906250 & 1.37500000000000 & 1.37500000000000 \\ 2 & 1.79991161247995 & 9.49553824779953 & 0.799911612479953 & 0.799911612479953 \\ 3 & 1.39280696583296 & 2.76325563488009 & 0.392806965832955 & 0.392806965832955 \\ 4 & 1.13713195528050 & 0.672027655272641 & 0.137131955280504 & 0.137131955280504 \\ 5 & 1.02287186525853 & 0.0946743271371449 & 0.0228718652585267 & 0.0228718652585267 \\ 6 & 1.00075576570494 & 0.00302649163762347 & 0.000755765704943512 & 0.000755765704943512 \\ 7 & 1.00000085569472 & 3.42278329013013 \times 10^{-6} & 8.55694724188893 \times 10^{-7} & 8.55694724188893 \times 10^{-7} \\ 8 & 1.00000000000110 & 4.39293046383682 \times 10^{-12} & 1.09823261595920 \times 10^{-12} & 1.09823261595920 \times 10^{-12} \\ 9 & 1.00000000000000 & 0 & 0.000000000000000 & 0.000000000000000 \end{array}\right)
latex(mat_c)
\left(\begin{array}{rrrrr} 0.000000000000000 & -0.500000000000000 & -0.937500000000000 & 0.500000000000000 & 0.500000000000000 \\ 1 & -2.37500000000000 & 30.8166503906250 & 1.37500000000000 & 1.37500000000000 \\ 2 & -1.79991161247995 & 9.49553824779953 & 0.799911612479953 & 0.799911612479953 \\ 3 & -1.39280696583296 & 2.76325563488009 & 0.392806965832955 & 0.392806965832955 \\ 4 & -1.13713195528050 & 0.672027655272641 & 0.137131955280504 & 0.137131955280504 \\ 5 & -1.02287186525853 & 0.0946743271371449 & 0.0228718652585267 & 0.0228718652585267 \\ 6 & -1.00075576570494 & 0.00302649163762347 & 0.000755765704943512 & 0.000755765704943512 \\ 7 & -1.00000085569472 & 3.42278329013013 \times 10^{-6} & 8.55694724188893 \times 10^{-7} & 8.55694724188893 \times 10^{-7} \\ 8 & -1.00000000000110 & 4.39293046383682 \times 10^{-12} & 1.09823261595920 \times 10^{-12} & 1.09823261595920 \times 10^{-12} \\ 9 & -1.00000000000000 & 0.000000000000000 & 0.000000000000000 & 0.000000000000000 \end{array}\right)
latex(mat_d)
\left(\begin{array}{rrrrr} 0 & 0.500000000000000 + 1.00000000000000i & -1.43750000000000 - 1.50000000000000i & 0.500000000000000 & 0.500000000000000 \\ 1 & 0.199000000000000 + 0.782000000000000i & -0.769771929767000 - 0.356006053656000i & 0.295169442862909 & 0.295169442862909 \\ 2 & -0.174237430404181 + 0.935443186698232i & -0.392751834733713 + 0.550704885322329i & 0.185812443871586 & 0.185812443871586 \\ 3 & 0.0216031068276627 + 0.948596404538570i & -0.192816192524941 - 0.0737216971357816i & 0.0557586213151812 & 0.0557586213151812 \\ 4 & -0.00377332053071798 + 1.00342102746120i & 0.0136684777211655 + 0.0152485006219681i & 0.00509326778383489 & 0.00509326778383489 \\ 5 & -0.0000385285289577100 + 0.999996460974189i & -0.0000141649347179529 + 0.000154112479366356i & 0.0000386907255984654 & 0.0000386907255984654 \\ 6 & 4.08921007549279 \times 10^{-10} + 0.999999997792076i & -8.83169404275463 \times 10^{-9} - 1.63568401936272 \times 10^{-9}i & 2.24547158064110 \times 10^{-9} & 2.24547158064110 \times 10^{-9} \\ 7 & -2.70859898669698 \times 10^{-18} + 1.00000000000000i & 1.08343959467879 \times 10^{-17}i & 2.70859898669698 \times 10^{-18} & 2.70859898669698 \times 10^{-18} \\ 8 & 1.00000000000000i & 0 & 0.000000000000000 & 0.000000000000000 \end{array}\right)
latex(mat_e)
\left(\begin{array}{rrrrr} 0 & 0.500000000000000 - 1.00000000000000i & -1.43750000000000 + 1.50000000000000i & 0.500000000000000 & 0.500000000000000 \\ 1 & 0.199000000000000 - 0.782000000000000i & -0.769771929767000 + 0.356006053656000i & 0.295169442862909 & 0.295169442862909 \\ 2 & -0.174237430404181 - 0.935443186698232i & -0.392751834733713 - 0.550704885322329i & 0.185812443871586 & 0.185812443871586 \\ 3 & 0.0216031068276627 - 0.948596404538570i & -0.192816192524941 + 0.0737216971357816i & 0.0557586213151812 & 0.0557586213151812 \\ 4 & -0.00377332053071798 - 1.00342102746120i & 0.0136684777211655 - 0.0152485006219681i & 0.00509326778383489 & 0.00509326778383489 \\ 5 & -0.0000385285289577100 - 0.999996460974189i & -0.0000141649347179529 - 0.000154112479366356i & 0.0000386907255984654 & 0.0000386907255984654 \\ 6 & 4.08921007549279 \times 10^{-10} - 0.999999997792076i & -8.83169404275463 \times 10^{-9} + 1.63568401936272 \times 10^{-9}i & 2.24547158064110 \times 10^{-9} & 2.24547158064110 \times 10^{-9} \\ 7 & -2.70859898669698 \times 10^{-18} - 1.00000000000000i & -1.08343959467879 \times 10^{-17}i & 2.70859898669698 \times 10^{-18} & 2.70859898669698 \times 10^{-18} \\ 8 & -1.00000000000000i & 0 & 0.000000000000000 & 0.000000000000000 \end{array}\right)