Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168730
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_mod(p_0): mue(x) = f(x)/derivative(f,x) mue_1 = derivative(mue,x) return n(p_0 - (mue(p_0)/mue_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 = [] i=1 while i<=n_0: p = newton_mod(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 = newton_mod(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.653061224489796 & -0.818107164497092 & 0.346938775510204 & 0.346938775510204 \\ 2 & 0.820971991517844 & -0.545730725340175 & 0.179028008482156 & 0.179028008482156 \\ 3 & 0.950675151518051 & -0.183175848382037 & 0.0493248484819490 & 0.0493248484819490 \\ 4 & 0.996299660402282 & -0.0147194057923515 & 0.00370033959771821 & 0.00370033959771821 \\ 5 & 0.999979436178562 & -0.0000822527485641045 & 0.0000205638214384463 & 0.0000205638214384463 \\ 6 & 0.999999999365690 & -2.53724197207816 \times 10^{-9} & 6.34310493019541 \times 10^{-10} & 6.34310493019541 \times 10^{-10} \\ 7 & 1.00000000000000 & 0 & 0.000000000000000 & 0.000000000000000 \\ 8 & 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 & -0.653061224489796 & -0.818107164497092 & 0.346938775510204 & 0.346938775510204 \\ 2 & -0.820971991517844 & -0.545730725340175 & 0.179028008482156 & 0.179028008482156 \\ 3 & -0.950675151518051 & -0.183175848382037 & 0.0493248484819490 & 0.0493248484819490 \\ 4 & -0.996299660402282 & -0.0147194057923515 & 0.00370033959771821 & 0.00370033959771821 \\ 5 & -0.999979436178562 & -0.0000822527485641045 & 0.0000205638214384463 & 0.0000205638214384463 \\ 6 & -0.999999999365690 & -2.53724197207816 \times 10^{-9} & 6.34310493019541 \times 10^{-10} & 6.34310493019541 \times 10^{-10} \\ 7 & -1.00000000000000 & 0.000000000000000 & 0.000000000000000 & 0.000000000000000 \\ 8 & -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.0992467877713774 + 1.50287992910944i & 3.96810309264272 + 1.34168699959197i & 0.512579894244839 & 0.512579894244839 \\ 2 & 0.0750847590826504 + 0.741805094962531i & -0.715779636674473 - 0.121341343393605i & 0.268890925904552 & 0.268890925904552 \\ 3 & 0.0579893618541384 + 0.905620378015188i & -0.343893309254788 - 0.171578366222158i & 0.110771292013076 & 0.110771292013076 \\ 4 & 0.0169089524430208 + 0.991596534005004i & -0.0348792068457062 - 0.0659257974882592i & 0.0188820262008451 & 0.0188820262008451 \\ 5 & 0.000425840086939418 + 1.00032619856507i & 0.00130434408355606 - 0.00170502748367350i & 0.000536418944014846 & 0.000536418944014846 \\ 6 & -4.16695981627193 \times 10^{-7} + 1.00000011232993i & 4.49318761086559 \times 10^{-7} + 1.66678448819772 \times 10^{-6}i & 4.31571030865824 \times 10^{-7} & 4.31571030865824 \times 10^{-7} \\ 7 & 1.40422321712109 \times 10^{-13} + 1.00000000000024i & 9.66338120633736 \times 10^{-13} - 5.61689286848841 \times 10^{-13}i & 2.79430695606067 \times 10^{-13} & 2.79430695606067 \times 10^{-13} \\ 8 & -1.01781989435236 \times 10^{-25} + 1.00000000000000i & 4.07127957740943 \times 10^{-25}i & 1.01781989435236 \times 10^{-25} & 1.01781989435236 \times 10^{-25} \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.0992467877713774 - 1.50287992910944i & 3.96810309264272 - 1.34168699959197i & 0.512579894244839 & 0.512579894244839 \\ 2 & 0.0750847590826504 - 0.741805094962531i & -0.715779636674473 + 0.121341343393605i & 0.268890925904552 & 0.268890925904552 \\ 3 & 0.0579893618541384 - 0.905620378015188i & -0.343893309254788 + 0.171578366222158i & 0.110771292013076 & 0.110771292013076 \\ 4 & 0.0169089524430208 - 0.991596534005004i & -0.0348792068457062 + 0.0659257974882592i & 0.0188820262008451 & 0.0188820262008451 \\ 5 & 0.000425840086939418 - 1.00032619856507i & 0.00130434408355606 + 0.00170502748367350i & 0.000536418944014846 & 0.000536418944014846 \\ 6 & -4.16695981627193 \times 10^{-7} - 1.00000011232993i & 4.49318761086559 \times 10^{-7} - 1.66678448819772 \times 10^{-6}i & 4.31571030865824 \times 10^{-7} & 4.31571030865824 \times 10^{-7} \\ 7 & 1.40422321712109 \times 10^{-13} - 1.00000000000024i & 9.66338120633736 \times 10^{-13} + 5.61689286848841 \times 10^{-13}i & 2.79430695606067 \times 10^{-13} & 2.79430695606067 \times 10^{-13} \\ 8 & -1.01781989435236 \times 10^{-25} - 1.00000000000000i & -4.07127957740943 \times 10^{-25}i & 1.01781989435236 \times 10^{-25} & 1.01781989435236 \times 10^{-25} \end{array}\right)