CoCalc Shared Files2016-04-07-100427.sagews
Author: Ian Morrison
Views : 31





var('x', 'u', 'n', 'v')
def nr(f, u) :
fu = f(u)
dfu = diff(f(x),x)(u)
du = -fu/dfu
nextu = u+du
print "%22.14f %22.14f %22.14f %22.14f %22.14f" % (RDF(u), RDF(fu), RDF(dfu), RDF(du), RDF(nextu))
return nextu

(x, u, n, v)
def g(x) : return x^2-2

u = 1
print "                     u                   f(u)                  f'(u)                     du                  new u"
u = nr(g, u)
u = nr(g, u)
u = nr(g, u)
u = nr(g, u)
u = nr(g, u)
2.^(1/2)

u f(u) f'(u) du new u 1.00000000000000 -1.00000000000000 2.00000000000000 0.50000000000000 1.50000000000000
<string>:1: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...) See http://trac.sagemath.org/5930 for details.
1.50000000000000 0.25000000000000 3.00000000000000 -0.08333333333333 1.41666666666667 1.41666666666667 0.00694444444444 2.83333333333333 -0.00245098039216 1.41421568627451 1.41421568627451 0.00000600730488 2.82843137254902 -0.00000212389982 1.41421356237469 1.41421356237469 0.00000000000451 2.82842712474938 -0.00000000000159 1.41421356237310 1.41421356237310
def run_nr(f, u, n) :
v = u
print "                     u                   f(u)                  f'(u)                     du                  new u"
for i in range(n) :
v = nr(f, v)

var ('x')
def quiz(x) : return x^3+x-1
run_nr(quiz, 1, 6)

x u f(u) f'(u) du new u 1.00000000000000 1.00000000000000 4.00000000000000 -0.25000000000000 0.75000000000000 0.75000000000000 0.17187500000000 2.68750000000000 -0.06395348837209 0.68604651162791 0.68604651162791 0.00894103663828 2.41197944835046 -0.00370692903059 0.68233958259731 0.68233958259731 0.00002823062169 2.39676191793723 -0.00001177865080 0.68232780394651 0.68232780394651 0.00000000028399 2.39671369611541 -0.00000000011849 0.68232780382802 0.68232780382802 0.00000000000000 2.39671369563030 -0.00000000000000 0.68232780382802
# Find the square root of 2
def g(x) : return 10*(x-1)*(x-2)*(x-3)-1

run_nr(g, 3., 6)

u f(u) f'(u) du new u 3.00000000000000 -1.00000000000000 20.00000000000000 0.05000000000000 3.05000000000000 3.05000000000000 0.07625000000000 23.07499999999999 -0.00330444203684 3.04669555796316 3.04669555796316 0.00034359829783 22.86714773179454 -0.00001502584852 3.04668053211465 3.04668053211465 0.00000000708953 22.86620408923398 -0.00000000031004 3.04668053180460 3.04668053180460 0.00000000000000 22.86620406976296 -0.00000000000000 3.04668053180460 3.04668053180460 0.00000000000000 22.86620406976296 -0.00000000000000 3.04668053180460
# Problem 3.8.13
def j(x) : return x^4-2*x^3+5*x^2-6
run_nr(j, 1, 6)

u f(u) f'(u) du new u 1.00000000000000 -2.00000000000000 8.00000000000000 0.25000000000000 1.25000000000000 1.25000000000000 0.34765625000000 10.93750000000000 -0.03178571428571 1.21821428571429 1.21821428571429 0.00685070840667 10.50941095681487 -0.00065186416582 1.21756242154846 1.21756242154846 0.00000280159325 10.50081654192802 -0.00000026679766 1.21756215475080 1.21756215475080 0.00000000000047 10.50081302586903 -0.00000000000004 1.21756215475076 1.21756215475076 0.00000000000000 10.50081302586844 -0.00000000000000 1.21756215475076
# Problem 3.8.15
def k(x) : return sin(x)-x^2
run_nr(k,1, 5)

u f(u) f'(u) du new u 1.00000000000000 -0.15852901519210 -1.45969769413186 -0.10860400467125 0.89139599532875 0.89139599532875 -0.01663717417748 -1.15446536556104 -0.01441115054101 0.87698484478774 0.87698484478774 -0.00028814922393 -1.11449754776967 -0.00025854630592 0.87672629848182 0.87672629848182 -0.00000009254050 -1.11378170244405 -0.00000008308675 0.87672621539507 0.87672621539507 -0.00000000000001 -1.11378147240601 -0.00000000000001 0.87672621539506
# Problem 3.8.17
def m(x) : return 3*cos(x)-x-1
run_nr(k,1., 5)

u f(u) f'(u) du new u 1.00000000000000 -0.15852901519210 -1.45969769413186 -0.10860400467125 0.89139599532875 0.89139599532875 -0.01663717417748 -1.15446536556104 -0.01441115054101 0.87698484478774 0.87698484478774 -0.00028814922393 -1.11449754776967 -0.00025854630592 0.87672629848182 0.87672629848182 -0.00000009254050 -1.11378170244405 -0.00000008308675 0.87672621539507 0.87672621539507 -0.00000000000001 -1.11378147240601 -0.00000000000001 0.87672621539506
# Problem 3.8.23
def n(x) : return -2*x^7-5*x^4+9*x^3+5
plot(n,-2,2)

run_nr(n,1, 5)
print " "
run_nr(n,1, 9)
print " "
run_nr(n,-1, 5)
print " "
run_nr(n,-1, 6)

u f(u) f'(u) du new u 1.00000000000000 7.00000000000000 -7.00000000000000 1.00000000000000 2.00000000000000 2.00000000000000 -259.00000000000000 -948.00000000000000 -0.27320675105485 1.72679324894515 1.72679324894515 -84.67669304640032 -393.63817978902074 -0.21511300832603 1.51168024061911 1.51168024061911 -26.09864823939298 -174.45519804447991 -0.14960086332732 1.36207937729179 1.36207937729179 -6.86274794496542 -89.84924153175224 -0.07638069980302 1.28569867748878 u f(u) f'(u) du new u 1.00000000000000 7.00000000000000 -7.00000000000000 1.00000000000000 2.00000000000000 2.00000000000000 -259.00000000000000 -948.00000000000000 -0.27320675105485 1.72679324894515 1.72679324894515 -84.67669304640032 -393.63817978902074 -0.21511300832603 1.51168024061911 1.51168024061911 -26.09864823939298 -174.45519804447991 -0.14960086332732 1.36207937729179 1.36207937729179 -6.86274794496542 -89.84924153175224 -0.07638069980302 1.28569867748878 1.28569867748878 -1.14941351784140 -61.11000049776096 -0.01880892666469 1.26688975082409 1.26688975082409 -0.05609848060663 -55.21646979423029 -0.00101597369074 1.26587377713335 1.26587377713335 -0.00015567155144 -54.91022448280755 -0.00000283501940 1.26587094211395 1.26587094211395 -0.00000000120894 -54.90937162251990 -0.00000000002202 1.26587094209193 u f(u) f'(u) du new u -1.00000000000000 -7.00000000000000 33.00000000000000 0.21212121212121 -0.78787878787879 -0.78787878787879 -0.95145720853489 23.19312920412043 0.04102323581097 -0.74685555206782 -0.74685555206782 -0.04575438083030 20.96256363146472 0.00218267105277 -0.74467288101505 -0.74467288101505 -0.00012927002312 20.84412319342857 0.00000620174914 -0.74466667926591 -0.74466667926591 -0.00000000104325 20.84378675536839 0.00000000005005 -0.74466667921585 u f(u) f'(u) du new u -1.00000000000000 -7.00000000000000 33.00000000000000 0.21212121212121 -0.78787878787879 -0.78787878787879 -0.95145720853489 23.19312920412043 0.04102323581097 -0.74685555206782 -0.74685555206782 -0.04575438083030 20.96256363146472 0.00218267105277 -0.74467288101505 -0.74467288101505 -0.00012927002312 20.84412319342857 0.00000620174914 -0.74466667926591 -0.74466667926591 -0.00000000104325 20.84378675536839 0.00000000005005 -0.74466667921585 -0.74466667921585 -0.00000000000000 20.84378675265318 0.00000000000000 -0.74466667921585
# Problem 3.8.32
def p(x) : return abs(x)^0.5
plot(p(x), (x,-10,10))+plot((1/4)*x+1, (x,-5,8), color="red")+plot(-(1/4)*x+1, (x,-8,5), color="orange")

run_nr(p, 4, 10)

u f(u) f'(u) du new u 4.00000000000000 2.00000000000000 0.25000000000000 -8.00000000000000 -4.00000000000000 -4.00000000000000 2.00000000000000 -0.25000000000000 8.00000000000000 4.00000000000000 4.00000000000000 2.00000000000000 0.25000000000000 -8.00000000000000 -4.00000000000000 -4.00000000000000 2.00000000000000 -0.25000000000000 8.00000000000000 4.00000000000000 4.00000000000000 2.00000000000000 0.25000000000000 -8.00000000000000 -4.00000000000000 -4.00000000000000 2.00000000000000 -0.25000000000000 8.00000000000000 4.00000000000000 4.00000000000000 2.00000000000000 0.25000000000000 -8.00000000000000 -4.00000000000000 -4.00000000000000 2.00000000000000 -0.25000000000000 8.00000000000000 4.00000000000000 4.00000000000000 2.00000000000000 0.25000000000000 -8.00000000000000 -4.00000000000000 -4.00000000000000 2.00000000000000 -0.25000000000000 8.00000000000000 4.00000000000000
# Problem 3.8.35
def q(x) : return x^2*sin(x)
def dq(x) : return diff(q(x),x)
def d2q(x) : return diff(dq(x),x)
d2q(x)

-x^2*sin(x) + 4*x*cos(x) + 2*sin(x)
plot(d2q(x), (x,-3,3))

def dtwoq(x) : return -x^2*sin(x) + 4*x*cos(x) + 2*sin(x)


run_nr(dtwoq, 2, 5)

run_nr(dtwoq, -2, 5)

u f(u) f'(u) du new u 2.00000000000000 -5.14776954602850 -11.74386279500246 -0.43833699659869 1.56166300340131 1.56166300340131 -0.38172113508934 -9.33706199440475 -0.04088236056675 1.52078064283456 1.52078064283456 -0.00825793177922 -8.92893096228724 -0.00092485111758 1.51985579171698 1.51985579171698 -0.00000439981964 -8.91941426996553 -0.00000049328571 1.51985529843127 1.51985529843127 -0.00000000000125 -8.91940919080853 -0.00000000000014 1.51985529843113 u f(u) f'(u) du new u -2.00000000000000 5.14776954602850 -11.74386279500246 0.43833699659869 -1.56166300340131 -1.56166300340131 0.38172113508934 -9.33706199440475 0.04088236056675 -1.52078064283456 -1.52078064283456 0.00825793177922 -8.92893096228724 0.00092485111758 -1.51985579171698 -1.51985579171698 0.00000439981964 -8.91941426996554 0.00000049328571 -1.51985529843127 -1.51985529843127 0.00000000000125 -8.91940919080853 0.00000000000014 -1.51985529843113
# 3.9.39 by guessing
var('ii')
ii=0.04/12. # Start at 4% annualized interest
A = (375./ii)*(1.-(1.+ii)^(-60.))
(ii,A)
ii=0.08/12. # Start at 4% annualized interest
A = (375./ii)*(1.-(1.+ii)^(-60.))
(ii,A)
ii=0.09/12. # Start at 4% annualized interest
A = (375./ii)*(1.-(1.+ii)^(-60.))
(ii,A)
ii=0.091/12. # Start at 4% annualized interest
A = (375./ii)*(1.-(1.+ii)^(-60.))
(ii,A)
ii=0.0915/12. # Start at 4% annualized interest
A = (375./ii)*(1.-(1.+ii)^(-60.))
(ii,A)

ii (0.00333333333333333, 20362.1508379631) (0.00666666666666667, 18494.4125008596) (0.00750000000000000, 18065.0150703615) (0.00758333333333333, 18022.8483394203) (0.00762500000000000, 18001.8169105983)
def at(x): return arctan(1.0*x)

run_nr(at, 1.3918, 20)
`
u f(u) f'(u) du new u 1.39180000000000 0.94776579172313 0.34047105477837 -2.78368976869432 -1.39188976869432 -1.39188976869432 -0.94779635406508 0.34044209006828 2.78401637669119 1.39212660799687 1.39212660799687 0.94787697508413 0.34036568633535 -2.78487818584100 -1.39275157784413 -1.39275157784413 -0.94808963039814 0.34016417493938 2.78715308737929 1.39440150953517 1.39440150953517 0.94865043961915 0.33963289293554 -2.79316420568011 -1.39876269614495 -1.39876269614495 -0.95012858801105 0.33823353906901 2.80909040134308 1.41032770519813 1.41032770519813 0.95401895605539 0.33455734080018 -2.85158578130015 -1.44125807610202 -1.44125807610202 -0.96421773840514 0.32496812919690 2.96711477764917 1.52585670154715 1.52585670154715 0.99065564441307 0.30045922124371 -3.29713842801153 -1.77128172646438 -1.77128172646438 -1.05684115627379 0.24169540892217 4.37261576869301 2.60133404222863 2.60133404222863 1.20379432882734 0.12875085356609 -9.34979687889530 -6.74846283666666 -6.74846283666666 -1.42368495114456 0.02148608477045 66.26079001153366 59.51232717486700 59.51232717486700 1.55399466658262 0.00028226922325 -5505.36345649524810 -5445.85112932038101 -5445.85112932038101 -1.57061270077116 0.00000003371852 46580125.01850767433643 46574679.16737835109234 46574679.16737835109234 1.57079630532400 0.00000000000000 -3407372507182167.00000000000000 -3407372460607488.00000000000000 -3407372460607488.00000000000000 -1.57079632679490 0.00000000000000 18237239227000721179396188143616.00000000000000 18237239227000716675796560773120.00000000000000 18237239227000716675796560773120.00000000000000 1.57079632679490 0.00000000000000 -522441980376967839312753787563864380507900870613367536144613376.00000000000000 -522441980376967839312753787563864380507900870613367536144613376.00000000000000 -522441980376967839312753787563864380507900870613367536144613376.00000000000000 -1.57079632679490 0.00000000000000 428741981803559970681319961821848113856309023736486822819966109888955764396609717596923157751743445894330130518114578273927168.00000000000000 428741981803559970681319961821848113856309023736486822819966109888955764396609717596923157751743445894330130518114578273927168.00000000000000 428741981803559970681319961821848113856309023736486822819966109888955764396609717596923157751743445894330130518114578273927168.00000000000000 1.57079632679490 0.00000000000000 -288743289070681743281571501628087771533802043141665558488909911773248511444353398594615085412430022245830488621620797444331825786118200713371097114352741713202493366407727702454423425757449647194513066972034058499524739000027808327136288158087901085696.00000000000000 -288743289070681743281571501628087771533802043141665558488909911773248511444353398594615085412430022245830488621620797444331825786118200713371097114352741713202493366407727702454423425757449647194513066972034058499524739000027808327136288158087901085696.00000000000000 -288743289070681743281571501628087771533802043141665558488909911773248511444353398594615085412430022245830488621620797444331825786118200713371097114352741713202493366407727702454423425757449647194513066972034058499524739000027808327136288158087901085696.00000000000000 -1.57079632679490 0.00000000000000 inf inf