Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168695
Image: ubuntu2004
#simple Runga-Kutta Order 2 Slash 3 var('t','y') f(t,y) = y start = 0.0 end = 1.0 solution(t) = float(e)^t h = 0.5 #initial step size #let z00 represent higher-order solution z00 = y00 = 1.0 #initial value t00 = start; soln = [[t00,y00,z00]] while t00 < end: s1 = f(t00,y00) s2 = f(t00+h,y00+h*s1) s3 = f(t00+h/2,y00+h/2*((s1+s2)/2)) y01 = z00+h*((s1+s2)/2.0); z01 = z00+h*((s1+4*s3+s2)/6.0); err = abs(z01-y01) if err < .025: t00=t00+h y00 = y01 z00 = z01 soln.append([t00,y00,z00]) else: h = h/2.0 print soln
[[0.000000000000000, 1.00000000000000, 1.00000000000000], [0.500000000000000, 1.62500000000000, 1.64583333333333], [0.750000000000000, 2.10286458333333, 2.10709635416667], [1.00000000000000, 2.69852701822917, 2.70400322808160]]
abs(3.2-3)
0.200000000000000