# 381 - A15 - Differential Equations - Taylor's Methods

## 252 days ago by Professor381

Solve numerically the initial value problem:

$y' = f(t,y)$

$y(a) = y_0$

So, below we will need be given $f(t,y)$ and the starting point $(a,y_0)$.

var('t,y') example = 2 if example == 1: f = 2*y t0 = 0 y0 = 2 # for higher order methods....the derivative of f with respect to t treating any y terms with implicit differentiation and noting that y' = f fp = 2*f # actually fp = 2y' = 2f = 4y fpp = 4*f # actually fpp = 4y' = 4f = 8y exact = y0*exp(2*t) end if example == 2: f = t - y t0 = 0 y0 = 2 fp = 1 - f # actually fp = 1 - y' = 1 - f = 1 - (t-y) = 1 - t + y fpp = -1 + f # actually fpp = -1 + y' exact = ((t-1)*exp(t)+3)*exp(-t) end # set up discrete time steps for each method below steps = 8 delt = 1/8 ts = [t0] for k in range(steps+1): # create uniform steps to make things easy. Not using any adjustable step size ts.append(ts[k] + delt) end
# var('t') y = function('y')(t) exact = desolve(diff(y,t) == t-y, y, ics=[t0,y0]) show(exact) # Need to make this automatic using f and not having to explicitly enter f's formula # exact = desolve(diff(z,x) == f(t=t,y=z), z, ics=[t0,y0]) # show(exact)
# Euler's Method doing 3 explicit steps t1 = t0 + delt y1 = y0 + f(t=t0,y=y0)*delt pretty_print((t1,y1)) t2 = t1 + delt y2 = y1 + f(t=t1,y=y1)*delt pretty_print((t2,y2)) t3 = t2 + delt y3 = y2 + f(t=t2,y=y2)*delt pretty_print((t3,y3))
 Traceback (click to the left of this block for traceback) ... NameError: name 't0' is not defined Traceback (most recent call last): pretty_print((t2,y2)) File "", line 1, in File "/tmp/tmpyBlgKa/___code___.py", line 3, in t1 = t0 + delt NameError: name 't0' is not defined
# Euler's method using a loop. ys = [y0] euler_pts = [(t0,y0)] for k in range(steps): tempy = ys[k] + f(t=ts[k],y=ys[k])*delt ys.append(tempy) euler_pts.append((ts[k+1],tempy)) end G = points(euler_pts,color='magenta') G += plot(exact,(t,t0,t0+steps*delt),color='green') S = plot(spline(euler_pts),(t,t0,t0+steps*delt),color='blue') show(G+S)
# Second order Taylors method ys = [y0] tay2_pts = [(t0,y0)] for k in range(steps): tempy = ys[k] + f(t=ts[k],y=ys[k])*delt + fp(t=ts[k],y=ys[k])*delt^2/2 ys.append(tempy) tay2_pts.append((ts[k+1],tempy)) end G = points(tay2_pts,color='magenta') G += plot(exact,(t,t0,t0+steps*delt),color='green') S = plot(spline(tay2_pts),(t,t0,t0+steps*delt),color='blue') show(G+S)
# Third order Taylors method ys = [y0] tay2_pts = [(t0,y0)] for k in range(steps): tempy = ys[k] + f(t=ts[k],y=ys[k])*delt + fp(t=ts[k],y=ys[k])*delt^2/2 + fpp(t=ts[k],y=ys[k])*delt^3/6 ys.append(tempy) tay2_pts.append((ts[k+1],tempy)) end G = points(tay2_pts,color='magenta') G += plot(exact,(t,t0,t0+steps*delt),color='green') S = plot(spline(tay2_pts),(t,t0,t0+steps*delt),color='blue') show(G+S)
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://sagenb.mc.edu/home/pub/117/", "fetch_time": 1723214278000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-33/segments/1722640767846.53/warc/CC-MAIN-20240809142005-20240809172005-00846.warc.gz", "warc_record_offset": 23542045, "warc_record_length": 3658, "token_count": 1053, "char_count": 2818, "score": 4.15625, "int_score": 4, "crawl": "CC-MAIN-2024-33", "snapshot_type": "latest", "language": "en", "language_score": 0.641935408115387, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00049-of-00064.parquet"}