Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168695
Image: ubuntu2004
%latex $$ \left\lbrace\begin{array}{rcl} y'(t) &=& 1 + \frac{y}{t} + \left( \frac{y}{t}\right)^2\\ y(1) &=& 0 \end{array}\right.$$ The exact solution of the above IVP is given by $y(t) = t \tan(\ln t) $ a)Obtain numerical solution for the IVP given above by using Euler’s method, Midpoint method (Runge-Kutta method order 2), Modified Euler’s method, and Runge-Kutta method order 4. Use h = 0.01 and calculate the error for each method at each point. Organize your results into a table or tables. Modified Euler's Method is found at the top of page 277
%latex The algorithm for writing this method follows the basic steps found on page 257-258. The iterative steps are just replaced with those found on page 277. Look for the ugly line of code for that. I will comment the code that calculates the error. I have emailed the teacher as to whether he means actual error or error bound. In this case, I will assume actual error.
def mod_eulers_method(a,b,N,alpha): h = (b-a)/N t = a f = lambda x,y: 1.0 + (y/x) + (y/x)^2 #this is the first equation g = lambda x: x*tan(ln(x)) # this is the exact solution for the IVP omega = alpha #for storage of values, we create a list w = [] w.append(( 0 , omega , n(abs(g(1)-omega)) ) )# this stores y(1) = 0 & actual error for i in range(1,N): omega = omega + (h/2)*( f(t,omega) + f(a + (i+1)*h, omega + h* f(t,omega)) ) w.append(( i , omega , n(abs(g(t)-omega)) ) )# this stores y(t) & actual error t = a + i * h return w
#since h = 0.01, N = (4-1)/.01 = 300 ans = mod_eulers_method(1,4,300,0) # returns a table of points (i, w_i) mat_ans = matrix(ans)# creates a matrix of ans Latex_Table = latex(mat_ans)#makes the matrix into a table array in Latex
Latex_Table
WARNING: Output truncated!
\left(\begin{array}{rrr} 0.000000000000000 & 0.000000000000000 & 0.000000000000000 \\ 1.00000000000000 & 0.0100495001922338 & 0.0100495001922338 \\ 2.00000000000000 & 0.0201994742660851 & 0.0101493084162907 \\ 3.00000000000000 & 0.0304508854841002 & 0.0102495649637608 \\ 4.00000000000000 & 0.0408046809916414 & 0.0103502445877904 \\ 5.00000000000000 & 0.0512617934552398 & 0.0104513238661965 \\ 6.00000000000000 & 0.0618231425885605 & 0.0105527810607882 \\ 7.00000000000000 & 0.0724896365754331 & 0.0106545959885280 \\ 8.00000000000000 & 0.0832621733985296 & 0.0107567499034232 \\ 9.00000000000000 & 0.0941416420814873 & 0.0108592253881485 \\ 10.0000000000000 & 0.105128923851575 & 0.0109620062545049 \\ 11.0000000000000 & 0.116224893229364 & 0.0110650774519109 \\ 12.0000000000000 & 0.127430419051305 & 0.0111684249832005 \\ 13.0000000000000 & 0.138746365430579 & 0.0112720358270770 \\ 14.0000000000000 & 0.150173592661154 & 0.0113758978666336 \\ 15.0000000000000 & 0.161712958069539 & 0.0114799998234088 \\ 16.0000000000000 & 0.173365316818347 & 0.0115843311964990 \\ 17.0000000000000 & 0.185131522665459 & 0.0116888822062907 \\ 18.0000000000000 & 0.197012428682233 & 0.0117936437424199 \\ 19.0000000000000 & 0.209008887933954 & 0.0118986073156023 \\ 20.0000000000000 & 0.221121754125447 & 0.0120037650130106 \\ 21.0000000000000 & 0.233351882214534 & 0.0121091094569030 \\ 22.0000000000000 & 0.245700128995830 & 0.0122146337662367 \\ 23.0000000000000 & 0.258167353657147 & 0.0123203315210224 \\ 24.0000000000000 & 0.270754418310629 & 0.0124261967291980 \\ 25.0000000000000 & 0.283462188500552 & 0.0125322237958187 \\ 26.0000000000000 & 0.296291533689607 & 0.0126384074943785 \\ 27.0000000000000 & 0.309243327725317 & 0.0127447429400958 \\ 28.0000000000000 & 0.322318449288137 & 0.0128512255650072 \\ 29.0000000000000 & 0.335517782322678 & 0.0129578510947287 \\ 30.0000000000000 & 0.348842216453374 & 0.0130646155267555 \\ 31.0000000000000 & 0.362292647385820 & 0.0131715151101815 \\ 32.0000000000000 & 0.375869977294956 & 0.0132785463267296 \\ 33.0000000000000 & 0.389575115201133 & 0.0133857058729931 \\ 34.0000000000000 & 0.403408977335079 & 0.0134929906437965 \\ 35.0000000000000 & 0.417372487492691 & 0.0136003977165914 \\ 36.0000000000000 & 0.431466577380509 & 0.0137079243368095 \\ 37.0000000000000 & 0.445692186952703 & 0.0138155679041016 \\ 38.0000000000000 & 0.460050264740314 & 0.0139233259593967 \\ 39.0000000000000 & 0.474541768173468 & 0.0140311961727201 \\ 40.0000000000000 & 0.489167663897234 & 0.0141391763317132 \\ 41.0000000000000 & 0.503928928081750 & 0.0142472643308078 \\ 42.0000000000000 & 0.518826546727200 & 0.0143554581609998 \\ 43.0000000000000 & 0.533861515964208 & 0.0144637559001840 \\ 44.0000000000000 & 0.549034842350164 & 0.0145721557040061 \\ 45.0000000000000 & 0.564347543161988 & 0.0146806557971939 \\ 46.0000000000000 & 0.579800646685773 & 0.0147892544653337 \\ 47.0000000000000 & 0.595395192503787 & 0.0148979500470579 \\ 48.0000000000000 & 0.611132231779220 & 0.0150067409266135 \\ 49.0000000000000 & 0.627012827539093 & 0.0151156255267835 \\ 50.0000000000000 & 0.643038054955699 & 0.0152246023021357 \\ 51.0000000000000 & 0.659209001626946 & 0.0153336697325723 \\ 52.0000000000000 & 0.675526767855939 & 0.0154428263171584 \\ 53.0000000000000 & 0.691992466930126 & 0.0155520705682104 \\ 54.0000000000000 & 0.708607225400342 & 0.0156614010056192 \\ 55.0000000000000 & 0.725372183360029 & 0.0157708161513945 \\ 56.0000000000000 & 0.742288494724945 & 0.0158803145244116 \\ 57.0000000000000 & 0.759357327513629 & 0.0159898946353410 \\ 58.0000000000000 & 0.776579864128890 & 0.0160995549817503 \\ ... 241.000000000000 & 9.42438122781171 & 0.0231847460917294 \\ 242.000000000000 & 9.53896219668758 & 0.0228235656899241 \\ 243.000000000000 & 9.65522288612619 & 0.0224459782006079 \\ 244.000000000000 & 9.77319928953110 & 0.0220513835348033 \\ 245.000000000000 & 9.89292844448658 & 0.0216391568950733 \\ 246.000000000000 & 10.0144484708622 & 0.0212086476020961 \\ 247.000000000000 & 10.1377986105983 & 0.0207591778572098 \\ 248.000000000000 & 10.2630192692602 & 0.0202900414370148 \\ 249.000000000000 & 10.3901520594523 & 0.0198005023158281 \\ 250.000000000000 & 10.5192398461920 & 0.0192897932113034 \\ 251.000000000000 & 10.6503267943445 & 0.0187571140486238 \\ 252.000000000000 & 10.7834584182317 & 0.0182016303377583 \\ 253.000000000000 & 10.9186816335298 & 0.0176224714584308 \\ 254.000000000000 & 11.0560448115814 & 0.0170187288467787 \\ 255.000000000000 & 11.1955978362529 & 0.0163894540771867 \\ 256.000000000000 & 11.3373921634786 & 0.0157336568325164 \\ 257.000000000000 & 11.4814808836397 & 0.0150503027553075 \\ 258.000000000000 & 11.6279187869385 & 0.0143383111720183 \\ 259.000000000000 & 11.7767624319343 & 0.0135965526816921 \\ 260.000000000000 & 11.9280702174239 & 0.0128238466000195 \\ 261.000000000000 & 12.0819024578565 & 0.0120189582486958 \\ 262.000000000000 & 12.2383214624879 & 0.0111805960796119 \\ 263.000000000000 & 12.3973916184933 & 0.0103074086223369 \\ 264.000000000000 & 12.5591794782695 & 0.00939798124244362 \\ 265.000000000000 & 12.7237538511764 & 0.00845083269743974 \\ 266.000000000000 & 12.8911858999839 & 0.00746441147590993 \\ 267.000000000000 & 13.0615492423056 & 0.00643709190407016 \\ 268.000000000000 & 13.2349200573265 & 0.00536717000332487 \\ 269.000000000000 & 13.4113771981459 & 0.00425285908012540 \\ 270.000000000000 & 13.5910023100866 & 0.00309228502896630 \\ 271.000000000000 & 13.7738799553411 & 0.00188348132660821 \\ 272.000000000000 & 13.9600977443549 & 0.000624383695090103 \\ 273.000000000000 & 14.1497464743764 & 0.000687175592300804 \\ 274.000000000000 & 14.3429202756331 & 0.00205347378859955 \\ 275.000000000000 & 14.5397167656270 & 0.00347690416438518 \\ 276.000000000000 & 14.7402372120831 & 0.00495998294433164 \\ 277.000000000000 & 14.9445867051198 & 0.00650535672441777 \\ 278.000000000000 & 15.1528743392573 & 0.00811581040752429 \\ 279.000000000000 & 15.3652134059255 & 0.00979427569891733 \\ 280.000000000000 & 15.5817215971851 & 0.0115438402069472 \\ 281.000000000000 & 15.8025212214317 & 0.0133677571979032 \\ 282.000000000000 & 16.0277394319120 & 0.0152694560592543 \\ 283.000000000000 & 16.2575084689510 & 0.0172525535295946 \\ 284.000000000000 & 16.4919659168569 & 0.0193208657601431 \\ 285.000000000000 & 16.7312549765537 & 0.0214784212775783 \\ 286.000000000000 & 16.9755247550767 & 0.0237294749261068 \\ 287.000000000000 & 17.2249305731581 & 0.0260785228726803 \\ 288.000000000000 & 17.4796342922375 & 0.0285303187686985 \\ 289.000000000000 & 17.7398046623431 & 0.0310898911700477 \\ 290.000000000000 & 18.0056176924129 & 0.0337625623278512 \\ 291.000000000000 & 18.2772570447655 & 0.0365539684733527 \\ 292.000000000000 & 18.5549144555745 & 0.0394700817333451 \\ 293.000000000000 & 18.8387901833723 & 0.0425172338259046 \\ 294.000000000000 & 19.1290934877835 & 0.0457021417030852 \\ 295.000000000000 & 19.4260431408956 & 0.0490319353230504 \\ 296.000000000000 & 19.7298679738898 & 0.0525141877552748 \\ 297.000000000000 & 20.0408074618018 & 0.0561569478433626 \\ 298.000000000000 & 20.3591123495534 & 0.0599687756749745 \\ 299.000000000000 & 20.6850453226907 & 0.0639587811355682 \end{array}\right)