Newton'sIdea

403 days ago by tazzalenghe

f = factorial f(3) 
       
6
6
f(f(f(3))) 
       
f(f(f(f(3)))) 
       
icosahedron() 
       
# A version of Newton's method that lets you play with the precision of the real numbers used, # shows a few other new SAGE tricks, and has a a bit (not not really enough) logic to catch # problems before they cause errors. def newton(f, z, z_0, maxiterations, errorbits): """ INPUT: -"f"-function -"z"-variable defining g (usually 'x' in the examples) - this is now key because this version of Newton computes the derivative of f for you and we need to tell SAGE what to differentiate with respect to -"z_0"-initial value of z -"errorbits" - number of bits of accuracy you want in the approximate root - for example, if you sat this to 20 then you want the root to 2^(-20) which is a bit less than .000001 by the rule of thumb "10 bits roughly equals 3 decimals" - calculation inside the function uses twice as many bits of precision to make sure round-off does not hurt - usually answers are correct to this higher accuracy since we stop when the change to the root is 0 to errorbits and incorporating this root gives an answer with about 2 * errorbits of accuracy (quadratic convergence!) OUTPUT: - series of approximations and a measure of the RATIO of the change (dt) incorporated in the current and previous approximations RETURN: - "t" - an approximate root accurate to within errorbits (if nothing went wrong) """ # Create real numbers with 2 * errorbits bits of precision and set up initial guess and desired accuracy # as numbers of this type R = RealField(2*errorbits) epsilon = R(2^(-errorbits)) t = R(z_0) # Set up a counter for iterations which serves 2 purposes: # tells us when we have setup dt and lastdt # lets us stop if the answers do not appear to be converging n = 0; while (n < maxiterations): # save dt if it exists if (n > 0): lastdt = dt # find next dt: all the action is here # note that diff(f,z) is the (symbolic) derivative function df/dz # [that is, if we set up f= x^2-x and z=x then diff(f,z) is 2*z-1 (or 2*x-1)] # and that diff(f,z)(t) is the _value_ of this function at t [if t=2, this would be 3 in our example]. dt = -f(t)/diff(f,z)(t) # update approximate root t t = t+dt # print if we have both dt and lastdt set up if (n > 0): print '%5s%54.50s%10.6s'%(n,t,log(dt)/log(lastdt)) else: print '%5s%54.50s'%(n,t) n = n+1 # stop if our current dt is small enough if (abs(dt) < epsilon): print "" print "Reached desired accuracy after %s iterations"%(n) return t # return the last approximation print "" print "Failed to reach desired accuracy after %s iterations"%(maxiterations) return t 
       
s= newton(x^2-2, x, 1., 100, 1000); 
       
    0    1.500000000000000000000000000000000000000000000000
    1    1.416666666666666666666666666666666666666666666666    3.5849
    2    1.414215686274509803921568627450980392156862745098    1.5461
    3    1.414213562374689910626295578890134910116559622115    1.9213
    4    1.414213562373095048801689623502530243614981925776    2.0205
    5    1.414213562373095048801688724209698078569671875377    2.0245
    6    1.414213562373095048801688724209698078569671875376    2.0155
    7    1.414213562373095048801688724209698078569671875376    2.0085
    8    1.414213562373095048801688724209698078569671875376    2.0044
    9    1.414213562373095048801688724209698078569671875376    2.0022

Reached desired accuracy after 10 iterations
    0    1.500000000000000000000000000000000000000000000000
    1    1.416666666666666666666666666666666666666666666666    3.5849
    2    1.414215686274509803921568627450980392156862745098    1.5461
    3    1.414213562374689910626295578890134910116559622115    1.9213
    4    1.414213562373095048801689623502530243614981925776    2.0205
    5    1.414213562373095048801688724209698078569671875377    2.0245
    6    1.414213562373095048801688724209698078569671875376    2.0155
    7    1.414213562373095048801688724209698078569671875376    2.0085
    8    1.414213562373095048801688724209698078569671875376    2.0044
    9    1.414213562373095048801688724209698078569671875376    2.0022

Reached desired accuracy after 10 iterations
R = RealField(2000); R((2^(1/2))); ' '; s 
       
1.4142135623730950488016887242096980785696718753769480731766797379907324\
784621070388503875343276415727350138462309122970249248360558507372126441\
214970999358314132226659275055927557999505011527820605714701095599716059\
702745345968620147285174186408891986095523292304843087143214508397626036\
279952514079896872533965463318088296406206152583523950547457502877599617\
298355752203375318570113543746034084988471603868999706990048150305440277\
903164542478230684929369186215805784631115966687130130156185689872372352\
885092648612494977154218334204285686060146824720771435854874155657069677\
65372022648544701585880162
' '
1.4142135623730950488016887242096980785696718753769480731766797379907324\
784621070388503875343276415727350138462309122970249248360558507372126441\
214970999358314132226659275055927557999505011527820605714701095599716059\
702745345968620147285174186408891986095523292304843087143214508397626036\
279952514079896872533965463318088296406206152583523950547457502877599617\
298355752203375318570113543746034084988471603868999706990048150305440277\
903164542478230684929369186215805784631115966687130130156185689872372352\
885092648612494977154218334204285686060146824720771435854874155657069677\
65372022648544701585880162
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970274534596862014728517418640889198609552329230484308714321450839762603627995251407989687253396546331808829640620615258352395054745750287759961729835575220337531857011354374603408498847160386899970699004815030544027790316454247823068492936918621580578463111596668713013015618568987237235288509264861249497715421833420428568606014682472077143585487415565706967765372022648544701585880162
' '
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970274534596862014728517418640889198609552329230484308714321450839762603627995251407989687253396546331808829640620615258352395054745750287759961729835575220337531857011354374603408498847160386899970699004815030544027790316454247823068492936918621580578463111596668713013015618568987237235288509264861249497715421833420428568606014682472077143585487415565706967765372022648544701585880162
t= newton(tan(x/4)-1, x, 3., 100, 1000) 
       
    0    3.146484430127296958292932960585562772756879203282
    1    3.141598640833634999976845465252037951079234356943    2.7703
    2    3.141592653598755019610533725543969236237477150415    1.9342
    3    3.141592653589793238462663461659838584872006945503    2.0440
    4    3.141592653589793238462643383279502884197169399475    2.0386
    5    3.141592653589793238462643383279502884197169399375    2.0228
    6    3.141592653589793238462643383279502884197169399375    2.0121
    7    3.141592653589793238462643383279502884197169399375    2.0062
    8    3.141592653589793238462643383279502884197169399375    2.0031

Reached desired accuracy after 9 iterations
    0    3.146484430127296958292932960585562772756879203282
    1    3.141598640833634999976845465252037951079234356943    2.7703
    2    3.141592653598755019610533725543969236237477150415    1.9342
    3    3.141592653589793238462663461659838584872006945503    2.0440
    4    3.141592653589793238462643383279502884197169399475    2.0386
    5    3.141592653589793238462643383279502884197169399375    2.0228
    6    3.141592653589793238462643383279502884197169399375    2.0121
    7    3.141592653589793238462643383279502884197169399375    2.0062
    8    3.141592653589793238462643383279502884197169399375    2.0031

Reached desired accuracy after 9 iterations
R(pi); ' '; t 
       
3.1415926535897932384626433832795028841971693993751058209749445923078164\
062862089986280348253421170679821480865132823066470938446095505822317253\
594081284811174502841027019385211055596446229489549303819644288109756659\
334461284756482337867831652712019091456485669234603486104543266482133936\
072602491412737245870066063155881748815209209628292540917153643678925903\
600113305305488204665213841469519415116094330572703657595919530921861173\
819326117931051185480744623799627495673518857527248912279381830119491298\
336733624406566430860213949463952247371907021798609437027705392171762931\
76752384674818467669405132
' '
3.1415926535897932384626433832795028841971693993751058209749445923078164\
062862089986280348253421170679821480865132823066470938446095505822317253\
594081284811174502841027019385211055596446229489549303819644288109756659\
334461284756482337867831652712019091456485669234603486104543266482133936\
072602491412737245870066063155881748815209209628292540917153643678925903\
600113305305488204665213841469519415116094330572703657595919530921861173\
819326117931051185480744623799627495673518857527248912279381830119491298\
336733624406566430860213949463952247371907021798609437027705392171762931\
76752384674818467669405132
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132
' '
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132
# u = newton(tan(x/4)-1, x, 3., 100, 1000000) 
       
    0    3.146484430127296958292932960585562772756879203282
    1    3.141598640833634999976845465252037951079234356943    2.7703
    2    3.141592653598755019610533725543969236237477150415    1.9342
    3    3.141592653589793238462663461659838584872006945503    2.0440
    4    3.141592653589793238462643383279502884197169399475    2.0386
    5    3.141592653589793238462643383279502884197169399375    2.0228
    6    3.141592653589793238462643383279502884197169399375    2.0121
    7    3.141592653589793238462643383279502884197169399375    2.0062
    8    3.141592653589793238462643383279502884197169399375    2.0031
    9    3.141592653589793238462643383279502884197169399375    2.0016
   10    3.141592653589793238462643383279502884197169399375    2.0008
   11    3.141592653589793238462643383279502884197169399375    2.0004
   12    3.141592653589793238462643383279502884197169399375    2.0002
   13    3.141592653589793238462643383279502884197169399375    2.0001
   14    3.141592653589793238462643383279502884197169399375    2.0000
   15    3.141592653589793238462643383279502884197169399375    2.0000
   16    3.141592653589793238462643383279502884197169399375    2.0000
   17    3.141592653589793238462643383279502884197169399375    2.0000
   18    3.141592653589793238462643383279502884197169399375    2.0000

Reached desired accuracy after 19 iterations
    0    3.146484430127296958292932960585562772756879203282
    1    3.141598640833634999976845465252037951079234356943    2.7703
    2    3.141592653598755019610533725543969236237477150415    1.9342
    3    3.141592653589793238462663461659838584872006945503    2.0440
    4    3.141592653589793238462643383279502884197169399475    2.0386
    5    3.141592653589793238462643383279502884197169399375    2.0228
    6    3.141592653589793238462643383279502884197169399375    2.0121
    7    3.141592653589793238462643383279502884197169399375    2.0062
    8    3.141592653589793238462643383279502884197169399375    2.0031
    9    3.141592653589793238462643383279502884197169399375    2.0016
   10    3.141592653589793238462643383279502884197169399375    2.0008
   11    3.141592653589793238462643383279502884197169399375    2.0004
   12    3.141592653589793238462643383279502884197169399375    2.0002
   13    3.141592653589793238462643383279502884197169399375    2.0001
   14    3.141592653589793238462643383279502884197169399375    2.0000
   15    3.141592653589793238462643383279502884197169399375    2.0000
   16    3.141592653589793238462643383279502884197169399375    2.0000
   17    3.141592653589793238462643383279502884197169399375    2.0000
   18    3.141592653589793238462643383279502884197169399375    2.0000

Reached desired accuracy after 19 iterations
us= str(u) f=open('u.txt','w') j = 0; while (j < len(us)): uss= us[j:j+80]+"\n" j += 80 f.write(uss) f.close() 
       
v = newton(sin(x/2)-1, x, 3., 100, 1000) 
       
    0    3.070825911596367388554289564395476011563056867234
    1    3.106212974670069156119698463357558359202627319516    1.2620
    2    3.123903275451775195995730417915502395924008917970    1.2074
    3    3.132748022180150073113814822791002769180681873175    1.1718
    4    3.137170345092209144731084409216070576607233906051    1.1466
    5    3.139381500241900151581167911189442655888315914806    1.1278
    6    3.140487077028458886079922644646299178837445615916    1.1133
    7    3.141039865323202580561672392450848974170535654229    1.1018
    8    3.141316259458257474123710950218447425842136802019    1.0924
    9    3.141454456524245301864160500019928726587545766901    1.0845
   10    3.141523555057046763359604208329640875676285173801    1.0779
   11    3.141558104323423437560643746327768603851915001854    1.0723
   12    3.141575378956608767592833391968667150565429248736    1.0674
   13    3.141584016273201056725387110811889940385948399599    1.0632
   14    3.141588334931497154306221337281427527602168017933    1.0594
   15    3.141590494260645197223458121554845829686643637338    1.0561
   16    3.141591573925219217947928972576313004739440683816    1.0531
   17    3.141592113757506228218395955447795308875001895069    1.0504
   18    3.141592383673649733342158391553634861881672912828    1.0480
   19    3.141592518631721485902605727690317088857455506769    1.0458
   20    3.141592586110757362182650160519128513352999120108    1.0438
   21    3.141592619850275300322649972528593014623558604783    1.0419
   22    3.141592636720034269392647077982707613891275279949    1.0402
   23    3.141592645154913753927645280640937707104331623918    1.0387
   24    3.141592649372353496195144338211449352908264027632    1.0372
   25    3.141592651481073367328893861526879750709905898485    1.0359
   26    3.141592652535433302895768622500866771473186296911    1.0346
   27    3.141592653062613270679206002902394259587633929136    1.0335
   28    3.141592653326203254570924693092474750861458674379    1.0324
   29    3.141592653457998246516784038186179589900446163143    1.0314
   30    3.141592653523895742489713710732865083595199297042    1.0304
   31    3.141592653556844490476178547006186964714483287681    1.0295
   32    3.141592653573318864469410965142845297058113710962    1.0287
   33    3.141592653581556051466027174211174137202927476098    1.0279
   34    3.141592653585674644964335278745338516521959177852    1.0271
   35    3.141592653587733941713489331012420701087303131128    1.0264
   36    3.141592653588763590088066357145961792733203620566    1.0257
   37    3.141592653589278414275354870212732338476557429384    1.0251
   38    3.141592653589535826368999126746117611338284779306    1.0244
   39    3.141592653589664532415821255012810247767904759956    1.0239
   40    3.141592653589728885439232319146156565982559288492    1.0233
   41    3.141592653589761061950937851212829725089867120037    1.0228
   42    3.141592653589777150206790617246166304643518606719    1.0223
   43    3.141592653589785194334717000262834594420344046423    1.0218
   44    3.141592653589789216398680191771168739308756728321    1.0213
   45    3.141592653589791227430661787525335811752963064526    1.0209
   46    3.141592653589792232946652585402419347975066232035    1.0204
   47    3.141592653589792735704647984340961116086117815715    1.0200
   48    3.141592653589792987083645683810232000141643607546    1.0196
   49    3.141592653589793112773144533544867442169406503461    1.0192
   50    3.141592653589793175617893958412185163183287951418    1.0189
   51    3.141592653589793207040268670845844023690228675396    1.0185
   52    3.141592653589793222751456027062673453943699037385    1.0182
   53    3.141592653589793230607049705171088169070434218380    1.0179
   54    3.141592653589793234534846544225295526633801808877    1.0175
   55    3.141592653589793236498744963752399205415485604126    1.0172
   56    3.141592653589793237480694173515951044806327501750    1.0170
   57    3.141592653589793237971668778397726964501748450562    1.0167
   58    3.141592653589793238217156080838614924349458924969    1.0164
   59    3.141592653589793238339899732059058904273314162172    1.0161
   60    3.141592653589793238401271557669280894235241780773    1.0159
   61    3.141592653589793238431957470474391889216205590074    1.0156
   62    3.141592653589793238447300426876947386706687494724    1.0154
   63    3.141592653589793238454971905078225135451928447049    1.0151
   64    3.141592653589793238458807644178864009824548923212    1.0149
   65    3.141592653589793238460725513729183447010859161293    1.0147
   66    3.141592653589793238461684448504343165604014280334    1.0145
   67    3.141592653589793238462163915891923024900591839854    1.0143
   68    3.141592653589793238462403649585712954548880619614    1.0141
   69    3.141592653589793238462523516432607919373025009495    1.0139
   70    3.141592653589793238462583449856055401785097204435    1.0137
   71    3.141592653589793238462613416567779142991133301905    1.0135
   72    3.141592653589793238462628399923641013594151350640    1.0133
   73    3.141592653589793238462635891601571948895660375007    1.0131
   74    3.141592653589793238462639637440537416546414887191    1.0130
   75    3.141592653589793238462641510360020150371792143283    1.0128
   76    3.141592653589793238462642446819761517284480771329    1.0126
   77    3.141592653589793238462642915049632200740825085352    1.0125
   78    3.141592653589793238462643149164567542468997242363    1.0123
   79    3.141592653589793238462643266222035213333083320869    1.0122
   80    3.141592653589793238462643324750769048765126360122    1.0120
   81    3.141592653589793238462643354015135966481147879748    1.0119
   82    3.141592653589793238462643368647319425339158639561    1.0117
   83    3.141592653589793238462643375963411154768164019468    1.0116
   84    3.141592653589793238462643379621457019482666709421    1.0115
   85    3.141592653589793238462643381450479951839918054398    1.0113
   86    3.141592653589793238462643382364991418018543726886    1.0112
   87    3.141592653589793238462643382822247151107856563130    1.0111
   88    3.141592653589793238462643383050875017652512981253    1.0110
   89    3.141592653589793238462643383165188950924841190314    1.0108
   90    3.141592653589793238462643383222345917561005294844    1.0107
   91    3.141592653589793238462643383250924400879087347109    1.0106
   92    3.141592653589793238462643383265213642538128373242    1.0105
   93    3.141592653589793238462643383272358263367648886308    1.0104
   94    3.141592653589793238462643383275930573782409142841    1.0103
   95    3.141592653589793238462643383277716728989789271108    1.0102
   96    3.141592653589793238462643383278609806593479335241    1.0101
   97    3.141592653589793238462643383279056345395324367308    1.0100
   98    3.141592653589793238462643383279279614796246883341    1.0099
   99    3.141592653589793238462643383279391249496708141358    1.0098

Failed to reach desired accuracy after 100 iterations
    0    3.070825911596367388554289564395476011563056867234
    1    3.106212974670069156119698463357558359202627319516    1.2620
    2    3.123903275451775195995730417915502395924008917970    1.2074
    3    3.132748022180150073113814822791002769180681873175    1.1718
    4    3.137170345092209144731084409216070576607233906051    1.1466
    5    3.139381500241900151581167911189442655888315914806    1.1278
    6    3.140487077028458886079922644646299178837445615916    1.1133
    7    3.141039865323202580561672392450848974170535654229    1.1018
    8    3.141316259458257474123710950218447425842136802019    1.0924
    9    3.141454456524245301864160500019928726587545766901    1.0845
   10    3.141523555057046763359604208329640875676285173801    1.0779
   11    3.141558104323423437560643746327768603851915001854    1.0723
   12    3.141575378956608767592833391968667150565429248736    1.0674
   13    3.141584016273201056725387110811889940385948399599    1.0632
   14    3.141588334931497154306221337281427527602168017933    1.0594
   15    3.141590494260645197223458121554845829686643637338    1.0561
   16    3.141591573925219217947928972576313004739440683816    1.0531
   17    3.141592113757506228218395955447795308875001895069    1.0504
   18    3.141592383673649733342158391553634861881672912828    1.0480
   19    3.141592518631721485902605727690317088857455506769    1.0458
   20    3.141592586110757362182650160519128513352999120108    1.0438
   21    3.141592619850275300322649972528593014623558604783    1.0419
   22    3.141592636720034269392647077982707613891275279949    1.0402
   23    3.141592645154913753927645280640937707104331623918    1.0387
   24    3.141592649372353496195144338211449352908264027632    1.0372
   25    3.141592651481073367328893861526879750709905898485    1.0359
   26    3.141592652535433302895768622500866771473186296911    1.0346
   27    3.141592653062613270679206002902394259587633929136    1.0335
   28    3.141592653326203254570924693092474750861458674379    1.0324
   29    3.141592653457998246516784038186179589900446163143    1.0314
   30    3.141592653523895742489713710732865083595199297042    1.0304
   31    3.141592653556844490476178547006186964714483287681    1.0295
   32    3.141592653573318864469410965142845297058113710962    1.0287
   33    3.141592653581556051466027174211174137202927476098    1.0279
   34    3.141592653585674644964335278745338516521959177852    1.0271
   35    3.141592653587733941713489331012420701087303131128    1.0264
   36    3.141592653588763590088066357145961792733203620566    1.0257
   37    3.141592653589278414275354870212732338476557429384    1.0251
   38    3.141592653589535826368999126746117611338284779306    1.0244
   39    3.141592653589664532415821255012810247767904759956    1.0239
   40    3.141592653589728885439232319146156565982559288492    1.0233
   41    3.141592653589761061950937851212829725089867120037    1.0228
   42    3.141592653589777150206790617246166304643518606719    1.0223
   43    3.141592653589785194334717000262834594420344046423    1.0218
   44    3.141592653589789216398680191771168739308756728321    1.0213
   45    3.141592653589791227430661787525335811752963064526    1.0209
   46    3.141592653589792232946652585402419347975066232035    1.0204
   47    3.141592653589792735704647984340961116086117815715    1.0200
   48    3.141592653589792987083645683810232000141643607546    1.0196
   49    3.141592653589793112773144533544867442169406503461    1.0192
   50    3.141592653589793175617893958412185163183287951418    1.0189
   51    3.141592653589793207040268670845844023690228675396    1.0185
   52    3.141592653589793222751456027062673453943699037385    1.0182
   53    3.141592653589793230607049705171088169070434218380    1.0179
   54    3.141592653589793234534846544225295526633801808877    1.0175
   55    3.141592653589793236498744963752399205415485604126    1.0172
   56    3.141592653589793237480694173515951044806327501750    1.0170
   57    3.141592653589793237971668778397726964501748450562    1.0167
   58    3.141592653589793238217156080838614924349458924969    1.0164
   59    3.141592653589793238339899732059058904273314162172    1.0161
   60    3.141592653589793238401271557669280894235241780773    1.0159
   61    3.141592653589793238431957470474391889216205590074    1.0156
   62    3.141592653589793238447300426876947386706687494724    1.0154
   63    3.141592653589793238454971905078225135451928447049    1.0151
   64    3.141592653589793238458807644178864009824548923212    1.0149
   65    3.141592653589793238460725513729183447010859161293    1.0147
   66    3.141592653589793238461684448504343165604014280334    1.0145
   67    3.141592653589793238462163915891923024900591839854    1.0143
   68    3.141592653589793238462403649585712954548880619614    1.0141
   69    3.141592653589793238462523516432607919373025009495    1.0139
   70    3.141592653589793238462583449856055401785097204435    1.0137
   71    3.141592653589793238462613416567779142991133301905    1.0135
   72    3.141592653589793238462628399923641013594151350640    1.0133
   73    3.141592653589793238462635891601571948895660375007    1.0131
   74    3.141592653589793238462639637440537416546414887191    1.0130
   75    3.141592653589793238462641510360020150371792143283    1.0128
   76    3.141592653589793238462642446819761517284480771329    1.0126
   77    3.141592653589793238462642915049632200740825085352    1.0125
   78    3.141592653589793238462643149164567542468997242363    1.0123
   79    3.141592653589793238462643266222035213333083320869    1.0122
   80    3.141592653589793238462643324750769048765126360122    1.0120
   81    3.141592653589793238462643354015135966481147879748    1.0119
   82    3.141592653589793238462643368647319425339158639561    1.0117
   83    3.141592653589793238462643375963411154768164019468    1.0116
   84    3.141592653589793238462643379621457019482666709421    1.0115
   85    3.141592653589793238462643381450479951839918054398    1.0113
   86    3.141592653589793238462643382364991418018543726886    1.0112
   87    3.141592653589793238462643382822247151107856563130    1.0111
   88    3.141592653589793238462643383050875017652512981253    1.0110
   89    3.141592653589793238462643383165188950924841190314    1.0108
   90    3.141592653589793238462643383222345917561005294844    1.0107
   91    3.141592653589793238462643383250924400879087347109    1.0106
   92    3.141592653589793238462643383265213642538128373242    1.0105
   93    3.141592653589793238462643383272358263367648886308    1.0104
   94    3.141592653589793238462643383275930573782409142841    1.0103
   95    3.141592653589793238462643383277716728989789271108    1.0102
   96    3.141592653589793238462643383278609806593479335241    1.0101
   97    3.141592653589793238462643383279056345395324367308    1.0100
   98    3.141592653589793238462643383279279614796246883341    1.0099
   99    3.141592653589793238462643383279391249496708141358    1.0098

Failed to reach desired accuracy after 100 iterations
v = newton(sin(x/2)-1, x, 3., 1000, 1000) 
       
WARNING: Output truncated!  
full_output.txt



    0    3.070825911596367388554289564395476011563056867234
    1    3.106212974670069156119698463357558359202627319516    1.2620
    2    3.123903275451775195995730417915502395924008917970    1.2074
    3    3.132748022180150073113814822791002769180681873175    1.1718
    4    3.137170345092209144731084409216070576607233906051    1.1466
    5    3.139381500241900151581167911189442655888315914806    1.1278
    6    3.140487077028458886079922644646299178837445615916    1.1133
    7    3.141039865323202580561672392450848974170535654229    1.1018
    8    3.141316259458257474123710950218447425842136802019    1.0924
    9    3.141454456524245301864160500019928726587545766901    1.0845
   10    3.141523555057046763359604208329640875676285173801    1.0779
   11    3.141558104323423437560643746327768603851915001854    1.0723
   12    3.141575378956608767592833391968667150565429248736    1.0674
   13    3.141584016273201056725387110811889940385948399599    1.0632
   14    3.141588334931497154306221337281427527602168017933    1.0594
   15    3.141590494260645197223458121554845829686643637338    1.0561
   16    3.141591573925219217947928972576313004739440683816    1.0531
   17    3.141592113757506228218395955447795308875001895069    1.0504
   18    3.141592383673649733342158391553634861881672912828    1.0480
   19    3.141592518631721485902605727690317088857455506769    1.0458
   20    3.141592586110757362182650160519128513352999120108    1.0438
   21    3.141592619850275300322649972528593014623558604783    1.0419
   22    3.141592636720034269392647077982707613891275279949    1.0402
   23    3.141592645154913753927645280640937707104331623918    1.0387
   24    3.141592649372353496195144338211449352908264027632    1.0372
   25    3.141592651481073367328893861526879750709905898485    1.0359
   26    3.141592652535433302895768622500866771473186296911    1.0346
   27    3.141592653062613270679206002902394259587633929136    1.0335
   28    3.141592653326203254570924693092474750861458674379    1.0324
   29    3.141592653457998246516784038186179589900446163143    1.0314
   30    3.141592653523895742489713710732865083595199297042    1.0304
   31    3.141592653556844490476178547006186964714483287681    1.0295
   32    3.141592653573318864469410965142845297058113710962    1.0287
   33    3.141592653581556051466027174211174137202927476098    1.0279
   34    3.141592653585674644964335278745338516521959177852    1.0271
   35    3.141592653587733941713489331012420701087303131128    1.0264
   36    3.141592653588763590088066357145961792733203620566    1.0257
   37    3.141592653589278414275354870212732338476557429384    1.0251
   38    3.141592653589535826368999126746117611338284779306    1.0244
   39    3.141592653589664532415821255012810247767904759956    1.0239
   40    3.141592653589728885439232319146156565982559288492    1.0233
   41    3.141592653589761061950937851212829725089867120037    1.0228
   42    3.141592653589777150206790617246166304643518606719    1.0223
   43    3.141592653589785194334717000262834594420344046423    1.0218
   44    3.141592653589789216398680191771168739308756728321    1.0213
   45    3.141592653589791227430661787525335811752963064526    1.0209
   46    3.141592653589792232946652585402419347975066232035    1.0204
   47    3.141592653589792735704647984340961116086117815715    1.0200
   48    3.141592653589792987083645683810232000141643607546    1.0196
   49    3.141592653589793112773144533544867442169406503461    1.0192
   50    3.141592653589793175617893958412185163183287951418    1.0189
   51    3.141592653589793207040268670845844023690228675396    1.0185
   52    3.141592653589793222751456027062673453943699037385    1.0182
   53    3.141592653589793230607049705171088169070434218380    1.0179
   54    3.141592653589793234534846544225295526633801808877    1.0175
   55    3.141592653589793236498744963752399205415485604126    1.0172
   56    3.141592653589793237480694173515951044806327501750    1.0170
   57    3.141592653589793237971668778397726964501748450562    1.0167
   58    3.141592653589793238217156080838614924349458924969    1.0164

...

  940    3.141592653589793238462643383279502884197169399375    1.0010
  941    3.141592653589793238462643383279502884197169399375    1.0010
  942    3.141592653589793238462643383279502884197169399375    1.0010
  943    3.141592653589793238462643383279502884197169399375    1.0010
  944    3.141592653589793238462643383279502884197169399375    1.0010
  945    3.141592653589793238462643383279502884197169399375    1.0010
  946    3.141592653589793238462643383279502884197169399375    1.0010
  947    3.141592653589793238462643383279502884197169399375    1.0010
  948    3.141592653589793238462643383279502884197169399375    1.0010
  949    3.141592653589793238462643383279502884197169399375    1.0010
  950    3.141592653589793238462643383279502884197169399375    1.0010
  951    3.141592653589793238462643383279502884197169399375    1.0010
  952    3.141592653589793238462643383279502884197169399375    1.0010
  953    3.141592653589793238462643383279502884197169399375    1.0010
  954    3.141592653589793238462643383279502884197169399375    1.0010
  955    3.141592653589793238462643383279502884197169399375    1.0010
  956    3.141592653589793238462643383279502884197169399375    1.0010
  957    3.141592653589793238462643383279502884197169399375    1.0010
  958    3.141592653589793238462643383279502884197169399375    1.0010
  959    3.141592653589793238462643383279502884197169399375    1.0010
  960    3.141592653589793238462643383279502884197169399375    1.0010
  961    3.141592653589793238462643383279502884197169399375    1.0010
  962    3.141592653589793238462643383279502884197169399375    1.0010
  963    3.141592653589793238462643383279502884197169399375    1.0010
  964    3.141592653589793238462643383279502884197169399375    1.0010
  965    3.141592653589793238462643383279502884197169399375    1.0010
  966    3.141592653589793238462643383279502884197169399375    1.0010
  967    3.141592653589793238462643383279502884197169399375    1.0010
  968    3.141592653589793238462643383279502884197169399375    1.0010
  969    3.141592653589793238462643383279502884197169399375    1.0010
  970    3.141592653589793238462643383279502884197169399375    1.0010
  971    3.141592653589793238462643383279502884197169399375    1.0010
  972    3.141592653589793238462643383279502884197169399375    1.0010
  973    3.141592653589793238462643383279502884197169399375    1.0010
  974    3.141592653589793238462643383279502884197169399375    1.0010
  975    3.141592653589793238462643383279502884197169399375    1.0010
  976    3.141592653589793238462643383279502884197169399375    1.0010
  977    3.141592653589793238462643383279502884197169399375    1.0010
  978    3.141592653589793238462643383279502884197169399375    1.0010
  979    3.141592653589793238462643383279502884197169399375    1.0010
  980    3.141592653589793238462643383279502884197169399375    1.0010
  981    3.141592653589793238462643383279502884197169399375    1.0010
  982    3.141592653589793238462643383279502884197169399375    1.0010
  983    3.141592653589793238462643383279502884197169399375    1.0010
  984    3.141592653589793238462643383279502884197169399375    1.0010
  985    3.141592653589793238462643383279502884197169399375    1.0010
  986    3.141592653589793238462643383279502884197169399375    1.0010
  987    3.141592653589793238462643383279502884197169399375    1.0010
  988    3.141592653589793238462643383279502884197169399375    1.0010
  989    3.141592653589793238462643383279502884197169399375    1.0010
  990    3.141592653589793238462643383279502884197169399375    1.0010
  991    3.141592653589793238462643383279502884197169399375    1.0010
  992    3.141592653589793238462643383279502884197169399375    1.0010
  993    3.141592653589793238462643383279502884197169399375    1.0010
  994    3.141592653589793238462643383279502884197169399375    1.0010
  995    3.141592653589793238462643383279502884197169399375    1.0007
  996    3.141592653589793238462643383279502884197169399375    1.0004
  997    3.141592653589793238462643383279502884197169399375    +Infin

Reached desired accuracy after 998 iterations
WARNING: Output truncated!  
full_output.txt



    0    3.070825911596367388554289564395476011563056867234
    1    3.106212974670069156119698463357558359202627319516    1.2620
    2    3.123903275451775195995730417915502395924008917970    1.2074
    3    3.132748022180150073113814822791002769180681873175    1.1718
    4    3.137170345092209144731084409216070576607233906051    1.1466
    5    3.139381500241900151581167911189442655888315914806    1.1278
    6    3.140487077028458886079922644646299178837445615916    1.1133
    7    3.141039865323202580561672392450848974170535654229    1.1018
    8    3.141316259458257474123710950218447425842136802019    1.0924
    9    3.141454456524245301864160500019928726587545766901    1.0845
   10    3.141523555057046763359604208329640875676285173801    1.0779
   11    3.141558104323423437560643746327768603851915001854    1.0723
   12    3.141575378956608767592833391968667150565429248736    1.0674
   13    3.141584016273201056725387110811889940385948399599    1.0632
   14    3.141588334931497154306221337281427527602168017933    1.0594
   15    3.141590494260645197223458121554845829686643637338    1.0561
   16    3.141591573925219217947928972576313004739440683816    1.0531
   17    3.141592113757506228218395955447795308875001895069    1.0504
   18    3.141592383673649733342158391553634861881672912828    1.0480
   19    3.141592518631721485902605727690317088857455506769    1.0458
   20    3.141592586110757362182650160519128513352999120108    1.0438
   21    3.141592619850275300322649972528593014623558604783    1.0419
   22    3.141592636720034269392647077982707613891275279949    1.0402
   23    3.141592645154913753927645280640937707104331623918    1.0387
   24    3.141592649372353496195144338211449352908264027632    1.0372
   25    3.141592651481073367328893861526879750709905898485    1.0359
   26    3.141592652535433302895768622500866771473186296911    1.0346
   27    3.141592653062613270679206002902394259587633929136    1.0335
   28    3.141592653326203254570924693092474750861458674379    1.0324
   29    3.141592653457998246516784038186179589900446163143    1.0314
   30    3.141592653523895742489713710732865083595199297042    1.0304
   31    3.141592653556844490476178547006186964714483287681    1.0295
   32    3.141592653573318864469410965142845297058113710962    1.0287
   33    3.141592653581556051466027174211174137202927476098    1.0279
   34    3.141592653585674644964335278745338516521959177852    1.0271
   35    3.141592653587733941713489331012420701087303131128    1.0264
   36    3.141592653588763590088066357145961792733203620566    1.0257
   37    3.141592653589278414275354870212732338476557429384    1.0251
   38    3.141592653589535826368999126746117611338284779306    1.0244
   39    3.141592653589664532415821255012810247767904759956    1.0239
   40    3.141592653589728885439232319146156565982559288492    1.0233
   41    3.141592653589761061950937851212829725089867120037    1.0228
   42    3.141592653589777150206790617246166304643518606719    1.0223
   43    3.141592653589785194334717000262834594420344046423    1.0218
   44    3.141592653589789216398680191771168739308756728321    1.0213
   45    3.141592653589791227430661787525335811752963064526    1.0209
   46    3.141592653589792232946652585402419347975066232035    1.0204
   47    3.141592653589792735704647984340961116086117815715    1.0200
   48    3.141592653589792987083645683810232000141643607546    1.0196
   49    3.141592653589793112773144533544867442169406503461    1.0192
   50    3.141592653589793175617893958412185163183287951418    1.0189
   51    3.141592653589793207040268670845844023690228675396    1.0185
   52    3.141592653589793222751456027062673453943699037385    1.0182
   53    3.141592653589793230607049705171088169070434218380    1.0179
   54    3.141592653589793234534846544225295526633801808877    1.0175
   55    3.141592653589793236498744963752399205415485604126    1.0172
   56    3.141592653589793237480694173515951044806327501750    1.0170
   57    3.141592653589793237971668778397726964501748450562    1.0167
   58    3.141592653589793238217156080838614924349458924969    1.0164

...

  940    3.141592653589793238462643383279502884197169399375    1.0010
  941    3.141592653589793238462643383279502884197169399375    1.0010
  942    3.141592653589793238462643383279502884197169399375    1.0010
  943    3.141592653589793238462643383279502884197169399375    1.0010
  944    3.141592653589793238462643383279502884197169399375    1.0010
  945    3.141592653589793238462643383279502884197169399375    1.0010
  946    3.141592653589793238462643383279502884197169399375    1.0010
  947    3.141592653589793238462643383279502884197169399375    1.0010
  948    3.141592653589793238462643383279502884197169399375    1.0010
  949    3.141592653589793238462643383279502884197169399375    1.0010
  950    3.141592653589793238462643383279502884197169399375    1.0010
  951    3.141592653589793238462643383279502884197169399375    1.0010
  952    3.141592653589793238462643383279502884197169399375    1.0010
  953    3.141592653589793238462643383279502884197169399375    1.0010
  954    3.141592653589793238462643383279502884197169399375    1.0010
  955    3.141592653589793238462643383279502884197169399375    1.0010
  956    3.141592653589793238462643383279502884197169399375    1.0010
  957    3.141592653589793238462643383279502884197169399375    1.0010
  958    3.141592653589793238462643383279502884197169399375    1.0010
  959    3.141592653589793238462643383279502884197169399375    1.0010
  960    3.141592653589793238462643383279502884197169399375    1.0010
  961    3.141592653589793238462643383279502884197169399375    1.0010
  962    3.141592653589793238462643383279502884197169399375    1.0010
  963    3.141592653589793238462643383279502884197169399375    1.0010
  964    3.141592653589793238462643383279502884197169399375    1.0010
  965    3.141592653589793238462643383279502884197169399375    1.0010
  966    3.141592653589793238462643383279502884197169399375    1.0010
  967    3.141592653589793238462643383279502884197169399375    1.0010
  968    3.141592653589793238462643383279502884197169399375    1.0010
  969    3.141592653589793238462643383279502884197169399375    1.0010
  970    3.141592653589793238462643383279502884197169399375    1.0010
  971    3.141592653589793238462643383279502884197169399375    1.0010
  972    3.141592653589793238462643383279502884197169399375    1.0010
  973    3.141592653589793238462643383279502884197169399375    1.0010
  974    3.141592653589793238462643383279502884197169399375    1.0010
  975    3.141592653589793238462643383279502884197169399375    1.0010
  976    3.141592653589793238462643383279502884197169399375    1.0010
  977    3.141592653589793238462643383279502884197169399375    1.0010
  978    3.141592653589793238462643383279502884197169399375    1.0010
  979    3.141592653589793238462643383279502884197169399375    1.0010
  980    3.141592653589793238462643383279502884197169399375    1.0010
  981    3.141592653589793238462643383279502884197169399375    1.0010
  982    3.141592653589793238462643383279502884197169399375    1.0010
  983    3.141592653589793238462643383279502884197169399375    1.0010
  984    3.141592653589793238462643383279502884197169399375    1.0010
  985    3.141592653589793238462643383279502884197169399375    1.0010
  986    3.141592653589793238462643383279502884197169399375    1.0010
  987    3.141592653589793238462643383279502884197169399375    1.0010
  988    3.141592653589793238462643383279502884197169399375    1.0010
  989    3.141592653589793238462643383279502884197169399375    1.0010
  990    3.141592653589793238462643383279502884197169399375    1.0010
  991    3.141592653589793238462643383279502884197169399375    1.0010
  992    3.141592653589793238462643383279502884197169399375    1.0010
  993    3.141592653589793238462643383279502884197169399375    1.0010
  994    3.141592653589793238462643383279502884197169399375    1.0010
  995    3.141592653589793238462643383279502884197169399375    1.0007
  996    3.141592653589793238462643383279502884197169399375    1.0004
  997    3.141592653589793238462643383279502884197169399375    +Infin

Reached desired accuracy after 998 iterations
w = newton(cos(x/2), x, 3., 100, 1000) 
       
    0    3.141829688605304897577961785869606578146673746086
    1    3.141592653588683408937189967914590050353053599708    4.2738
    2    3.141592653589793238462643383279502884311086147017    2.8885
    3    3.141592653589793238462643383279502884197169399375    3.0902
    4    3.141592653589793238462643383279502884197169399375    3.0250
    5    3.141592653589793238462643383279502884197169399375    3.0096

Reached desired accuracy after 6 iterations
    0    3.141829688605304897577961785869606578146673746086
    1    3.141592653588683408937189967914590050353053599708    4.2738
    2    3.141592653589793238462643383279502884311086147017    2.8885
    3    3.141592653589793238462643383279502884197169399375    3.0902
    4    3.141592653589793238462643383279502884197169399375    3.0250
    5    3.141592653589793238462643383279502884197169399375    3.0096

Reached desired accuracy after 6 iterations
w = newton(cos(x/2), x, 3., 100, 3000) 
       
    0    3.141829688605304897577961785869606578146673746086
    1    3.141592653588683408937189967914590050353053599708    4.2738
    2    3.141592653589793238462643383279502884311086147017    2.8885
    3    3.141592653589793238462643383279502884197169399375    3.0902
    4    3.141592653589793238462643383279502884197169399375    3.0250
    5    3.141592653589793238462643383279502884197169399375    3.0096
    6    3.141592653589793238462643383279502884197169399375    3.0031

Reached desired accuracy after 7 iterations
    0    3.141829688605304897577961785869606578146673746086
    1    3.141592653588683408937189967914590050353053599708    4.2738
    2    3.141592653589793238462643383279502884311086147017    2.8885
    3    3.141592653589793238462643383279502884197169399375    3.0902
    4    3.141592653589793238462643383279502884197169399375    3.0250
    5    3.141592653589793238462643383279502884197169399375    3.0096
    6    3.141592653589793238462643383279502884197169399375    3.0031

Reached desired accuracy after 7 iterations
w = newton(cos(x/2), x, 3., 100, 9000) 
       
    0    3.141829688605304897577961785869606578146673746086
    1    3.141592653588683408937189967914590050353053599708    4.2738
    2    3.141592653589793238462643383279502884311086147017    2.8885
    3    3.141592653589793238462643383279502884197169399375    3.0902
    4    3.141592653589793238462643383279502884197169399375    3.0250
    5    3.141592653589793238462643383279502884197169399375    3.0096
    6    3.141592653589793238462643383279502884197169399375    3.0031
    7    3.141592653589793238462643383279502884197169399375    3.0010

Reached desired accuracy after 8 iterations
    0    3.141829688605304897577961785869606578146673746086
    1    3.141592653588683408937189967914590050353053599708    4.2738
    2    3.141592653589793238462643383279502884311086147017    2.8885
    3    3.141592653589793238462643383279502884197169399375    3.0902
    4    3.141592653589793238462643383279502884197169399375    3.0250
    5    3.141592653589793238462643383279502884197169399375    3.0096
    6    3.141592653589793238462643383279502884197169399375    3.0031
    7    3.141592653589793238462643383279502884197169399375    3.0010

Reached desired accuracy after 8 iterations
w = newton(cos(x/2), x, 3., 100, 27000) 
       
    0    3.141829688605304897577961785869606578146673746086
    1    3.141592653588683408937189967914590050353053599708    4.2738
    2    3.141592653589793238462643383279502884311086147017    2.8885
    3    3.141592653589793238462643383279502884197169399375    3.0902
    4    3.141592653589793238462643383279502884197169399375    3.0250
    5    3.141592653589793238462643383279502884197169399375    3.0096
    6    3.141592653589793238462643383279502884197169399375    3.0031
    7    3.141592653589793238462643383279502884197169399375    3.0010
    8    3.141592653589793238462643383279502884197169399375    3.0003

Reached desired accuracy after 9 iterations
    0    3.141829688605304897577961785869606578146673746086
    1    3.141592653588683408937189967914590050353053599708    4.2738
    2    3.141592653589793238462643383279502884311086147017    2.8885
    3    3.141592653589793238462643383279502884197169399375    3.0902
    4    3.141592653589793238462643383279502884197169399375    3.0250
    5    3.141592653589793238462643383279502884197169399375    3.0096
    6    3.141592653589793238462643383279502884197169399375    3.0031
    7    3.141592653589793238462643383279502884197169399375    3.0010
    8    3.141592653589793238462643383279502884197169399375    3.0003

Reached desired accuracy after 9 iterations