Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
58 views
def Newton(f,p,pow=1,max=10): f1=diff(f(x),x) n=0 error1=abs(f(p)) while(n<max): p2=p-(f(p)/f1(p)) error2=abs(p-p2) p=p2 n=n+1 print "%d steps error:%.15f ratio:%.15f"%(n,error2,error2/error1^pow) error1=error2 if error2>10^3 or abs(p2)>10^3 or n>10^3: print "DIVERGE: root not found" return print "after %d steps" %n print "Root: %.15f"%p print "f(x): %.15f"%f(p)
def Secant(f,p1,p2,max=10,pow=1): n=0 error1=abs(p1-p2) f1=f(p1) f2=f(p2) while(n<max): if f2==f1: print "DIVERGE: root not found" print p1 print p2 print n return p3=(p1*f2-p2*f1)/(f2-f1) p1=p2 p2=p3 f1=f2 f2=f(p2) error2=abs(p1-p2) n=n+1 print "%d steps error:%.15f ratio:%.15f"%(n,error2,error2/error1^pow) error1=error2 #print p3 if error2>10^3 or abs(f2)>10^3 or n>10^3: print "DIVERGE: root not found" return print "after %d steps" %n print "Root: %.15f"%p2 print "f(x): %.15f"%f2 def FalsePos(f,p1,p2,pow=1,max=10): n=0 error1=abs(p1-p2) f1=f(p1) f2=f(p2) #for better flase pos "Illonios algorithm" uncomment the commented lines below #flag=True while(n<max): if(f1*f2>=0): print "Unbracketed root!" return p3=(p1*f2-p2*f1)/(f2-f1) f3=f(p3) if(f1*f3>0): # if flag==True: # f2=f2/2 # flag=True error2=abs(p3-p1) p1=p3 f1=f3 else: # if flag==False: # f1=f1/2 # flag=False error2=abs(p3-p2) p2=p3 f2=f3 n=n+1 print "%d steps error:%.15f ratio:%.15f"%(n,error2,error2/error1^pow) error1=error2 #print p3 if error2>10^3 or abs(f3)>10^3 or n>10^3: print "DIVERGE: root not found" print error1 print n print p3 return print "after %d steps" %n print "Root: %.15f"%p3 print "f(x): %.15f"%f3
Newton(x^2-2,1)
1 steps error:0.500000000000000 ratio:0.500000000000000 2 steps error:0.083333333333333 ratio:0.166666666666667 3 steps error:0.002450980392157 ratio:0.029411764705882 4 steps error:0.000002123899820 ratio:0.000866551126516 5 steps error:0.000000000001595 ratio:0.000000750911983 6 steps error:0.000000000000000 ratio:0.000000000000564 7 steps error:0.000000000000000 ratio:0.000000000000000 8 steps error:0.000000000000000 ratio:0.000000000000000 9 steps error:0.000000000000000 ratio:0.000000000000000 10 steps error:0.000000000000000 ratio:0.000000000000000 after 10 steps Root: 1.414213562373095 f(x): 0.000000000000000
Newton(x^2-2,1,pow=2) #quadratic convergence
1 steps error:0.500000000000000 ratio:0.500000000000000 2 steps error:0.083333333333333 ratio:0.333333333333333 3 steps error:0.002450980392157 ratio:0.352941176470588 4 steps error:0.000002123899820 ratio:0.353552859618718 5 steps error:0.000000000001595 ratio:0.353553390592875 6 steps error:0.000000000000000 ratio:0.353553390593274 7 steps error:0.000000000000000 ratio:0.353553390593274 8 steps error:0.000000000000000 ratio:0.353553390593274 9 steps error:0.000000000000000 ratio:0.353553390593274 10 steps error:0.000000000000000 ratio:0.353553390593274 after 10 steps Root: 1.414213562373095 f(x): 0.000000000000000
Secant(x^2-2,0,2)
1 steps error:1.000000000000000 ratio:0.500000000000000 2 steps error:0.333333333333333 ratio:0.333333333333333 3 steps error:0.095238095238095 ratio:0.285714285714286 4 steps error:0.014778325123153 ratio:0.155172413793103 5 steps error:0.000418335026594 ratio:0.028307336799538 6 steps error:0.000002124214000 ratio:0.005077781836515 7 steps error:0.000000000315775 ratio:0.000148654901728 8 steps error:0.000000000000000 ratio:0.000000750911419 9 steps error:0.000000000000000 ratio:0.000000000111643 10 steps error:0.000000000000000 ratio:0.000000000000000 after 10 steps Root: 1.414213562373095 f(x): -0.000000000000000
Secant(x^2-2,0,2,pow=2)
1 steps error:1.000000000000000 ratio:0.250000000000000 2 steps error:0.333333333333333 ratio:0.333333333333333 3 steps error:0.095238095238095 ratio:0.857142857142857 4 steps error:0.014778325123153 ratio:1.629310344827586 5 steps error:0.000418335026594 ratio:1.915463123435394 6 steps error:0.000002124214000 ratio:12.138074781485892 7 steps error:0.000000000315775 ratio:69.981132670729551 8 steps error:0.000000000000000 ratio:2377.996480435273043 9 steps error:0.000000000000000 ratio:470832.000105395971332 10 steps error:0.000000000000000 ratio:3166815961.999999523162842 after 10 steps Root: 1.414213562373095 f(x): -0.000000000000000
Secant(x^2-2,0,2,pow=(1+5^(.5))/2)#superlinear but subquadratic convergence
1 steps error:1.000000000000000 ratio:0.325779112153147 2 steps error:0.333333333333333 ratio:0.333333333333333 3 steps error:0.095238095238095 ratio:0.563389965878845 4 steps error:0.014778325123153 ratio:0.663657813152499 5 steps error:0.000418335026594 ratio:0.382941858179991 6 steps error:0.000002124214000 ratio:0.621851798099311 7 steps error:0.000000000315775 ratio:0.476610402197814 8 steps error:0.000000000000000 ratio:0.558851379262133 9 steps error:0.000000000000000 ratio:0.506563786287912 10 steps error:0.000000000000000 ratio:0.538271204837748 after 10 steps Root: 1.414213562373095 f(x): -0.000000000000000
FalsePos(x^2-2,0,2) #linear convergenge
1 steps error:1.000000000000000 ratio:0.500000000000000 2 steps error:0.333333333333333 ratio:0.333333333333333 3 steps error:0.066666666666667 ratio:0.200000000000000 4 steps error:0.011764705882353 ratio:0.176470588235294 5 steps error:0.002028397565923 ratio:0.172413793103448 6 steps error:0.000348310693138 ratio:0.171717171717172 7 steps error:0.000059769290539 ratio:0.171597633136095 8 steps error:0.000010255042917 ratio:0.171577123050260 9 steps error:0.000001759494673 ratio:0.171573604060914 10 steps error:0.000000301881780 ratio:0.171573000297354 after 10 steps Root: 1.414213499851323 f(x): -0.000000176838271
def fixedpoint(f,p,max=10,n=1): new=f(p) if abs(new)>10^3: print "DIVERGENCE!! (probably)" return diff=abs(new-p) print "after ",str(n),"iterations the guess %.9f changed by %.9f"%(new,diff) if n<max: fixedpoint(f,new,max,n+1) fixedpoint((x+2)/(x+1),1)
after 1 iterations the guess 1.500000000 changed by 0.500000000 after 2 iterations the guess 1.400000000 changed by 0.100000000 after 3 iterations the guess 1.416666667 changed by 0.016666667 after 4 iterations the guess 1.413793103 changed by 0.002873563 after 5 iterations the guess 1.414285714 changed by 0.000492611 after 6 iterations the guess 1.414201183 changed by 0.000084531 after 7 iterations the guess 1.414215686 changed by 0.000014503 after 8 iterations the guess 1.414213198 changed by 0.000002488 after 9 iterations the guess 1.414213625 changed by 0.000000427 after 10 iterations the guess 1.414213552 changed by 0.000000073
Newton(tan(3.14159265359*x)-6,0.0) Secant(tan(3.14159265359*x)-6,0.0,.48) FalsePos(tan(3.14159265359*x)-6,0.0,.48)
1 steps error:1.909859317102618 ratio:0.318309886183770 2 steps error:1.846149063459904 ratio:0.966641389199616 3 steps error:1.150012922483032 ratio:0.622925280111331 4 steps error:1.836773300774937 ratio:1.597176227210645 5 steps error:1.070703914936185 ratio:0.582926545417692 6 steps error:1.472548242352314 ratio:1.375308544043270 7 steps error:0.585403171739047 ratio:0.397544307821045 8 steps error:1.729960123893962 ratio:2.955160148440601 9 steps error:0.282064456500546 ratio:0.163046796631155 10 steps error:1.771537705084778 ratio:6.280613045200716 after 10 steps Root: 13.655012218327325 f(x): -7.888496046948566 1 steps error:0.298805758309776 ratio:0.622511996478700 2 steps error:0.104992924131721 ratio:0.351375170028931 3 steps error:0.805798940682327 ratio:7.674792823861114 4 steps error:4.784282771950692 ratio:5.937315787359438 5 steps error:18.908353198739704 ratio:3.952181361351728 6 steps error:34.622192846046445 ratio:1.831052788264719 7 steps error:60.761244704724128 ratio:1.754979673728625 8 steps error:116.897167344899543 ratio:1.923877101480953 9 steps error:154.167657179923651 ratio:1.318831419798731 10 steps error:65.705785317255447 ratio:0.426196950249899 after 10 steps Root: -201.820637213076168 f(x): -5.368185241216611 1 steps error:0.181194241690224 ratio:0.377488003521300 2 steps error:0.104992924131721 ratio:0.579449562813484 3 steps error:0.062794061601643 ratio:0.598078985997794 4 steps error:0.038071393760618 ratio:0.606289715771810 5 steps error:0.023252098699340 ratio:0.610749867618266 6 steps error:0.014261763042296 ratio:0.613353797724107 7 steps error:0.008769830090899 ratio:0.614919071708764 8 steps error:0.005401095597553 ratio:0.615872319254851 9 steps error:0.003329540467331 ratio:0.616456496130004 10 steps error:0.002053712676501 ratio:0.616815652686109 after 10 steps Root: 0.444120661758126 f(x): -0.362258332550890