Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168731
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 halley(p_0): f_1 = derivative(f,x) f_2 = derivative(f_1,x) ans = p_0 - ((f(p_0)*f_1(p_0))/(f_1(p_0)^2 - (1/2)*f(p_0)*f_2(p_0))) return ans
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 = [] i=1 while i<=n_0: p = halley(p_0) 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 = [] 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 = halley(p_0) #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 & 0.783018867924528 & -0.624085646679841 & 0.216981132075472 & 0.216981132075472 \\ 2 & 0.983311377154950 & -0.0651019448197584 & 0.0166886228450503 & 0.0166886228450503 \\ 3 & 0.999994044202479 & -0.0000238229772541176 & 5.95579752060704 \times 10^{-6} & 5.95579752060704 \times 10^{-6} \\ 4 & 1.00000000000000 & -8.88178419700125 \times 10^{-16} & 2.22044604925031 \times 10^{-16} & 2.22044604925031 \times 10^{-16} \\ 5 & 1.00000000000000 & 0 & 0.000000000000000 & 0 \end{array}\right)
latex(mat_c)
\left(\begin{array}{rrrrr} 0.000000000000000 & -0.500000000000000 & -0.937500000000000 & 0.500000000000000 & 0.500000000000000 \\ 1 & -0.783018867924528 & -0.624085646679841 & 0.216981132075472 & 0.216981132075472 \\ 2 & -0.983311377154950 & -0.0651019448197584 & 0.0166886228450503 & 0.0166886228450503 \\ 3 & -0.999994044202479 & -0.0000238229772541176 & 5.95579752060704 \times 10^{-6} & 5.95579752060704 \times 10^{-6} \\ 4 & -1.00000000000000 & -8.88178419700125 \times 10^{-16} & 2.22044604925031 \times 10^{-16} & 2.22044604925031 \times 10^{-16} \\ 5 & -1.00000000000000 & 0.000000000000000 & 0.000000000000000 & 0 \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.0988743221909534 + 0.856544718237353i & -0.504669098997601 + 0.245226705007760i & 0.174228440428998 & 0.174228440428998 \\ 2 & -0.00763862425050195 + 1.00283282208455i & 0.0110274542393283 + 0.0308131117849640i & 0.00814699094163501 & 0.00814699094163501 \\ 3 & 3.19092067562268 \times 10^{-7} + 0.999999407351582i & -2.37059217633728 \times 10^{-6} - 1.27636600093738 \times 10^{-6}i & 6.73091297827470 \times 10^{-7} & 6.73091297827470 \times 10^{-7} \\ 4 & 3.79629579047537 \times 10^{-19} + 1.00000000000000i & -1.51851831619015 \times 10^{-18}i & 3.79629579047537 \times 10^{-19} & 3.79629579047537 \times 10^{-19} \\ 5 & 1.00000000000000i & 0 & 0.000000000000000 & 0 \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.0988743221909534 - 0.856544718237353i & -0.504669098997601 - 0.245226705007760i & 0.174228440428998 & 0.174228440428998 \\ 2 & -0.00763862425050195 - 1.00283282208455i & 0.0110274542393283 - 0.0308131117849640i & 0.00814699094163501 & 0.00814699094163501 \\ 3 & 3.19092067562268 \times 10^{-7} - 0.999999407351582i & -2.37059217633728 \times 10^{-6} + 1.27636600093738 \times 10^{-6}i & 6.73091297827470 \times 10^{-7} & 6.73091297827470 \times 10^{-7} \\ 4 & 3.79629579047537 \times 10^{-19} - 1.00000000000000i & 1.51851831619015 \times 10^{-18}i & 3.79629579047537 \times 10^{-19} & 3.79629579047537 \times 10^{-19} \\ 5 & -1.00000000000000i & 0 & 0.000000000000000 & 0 \end{array}\right)