Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168694
Image: ubuntu2004
#simple example of Trapezoid Method var('t','y') f(t,y) = y start = 0.0 end = 1.0 solution(t) = float(e)^t numOfIterations = 10 h = (end-start)/numOfIterations y00 = 1.0 #initial value t00 = start; soln = [[t00,y00]] for i in range(numOfIterations): y00 = y00+h/2*(f(t00,y00) + f(t00+h,y00 + h*f(t00,y00))) t00=t00+h soln.append([t00,y00]) print "%10r %20r %20r"%(t00,y00,solution(t00))
0.100000000000000 1.10500000000000 1.10517091807565 0.200000000000000 1.22102500000000 1.22140275816017 0.300000000000000 1.34923262500000 1.34985880757600 0.400000000000000 1.49090205062500 1.49182469764127 0.500000000000000 1.64744676594062 1.64872127070013 0.600000000000000 1.82042867636439 1.82211880039051 0.700000000000000 2.01157368738265 2.01375270747048 0.800000000000000 2.22278892455783 2.22554092849247 0.900000000000000 2.45618176163640 2.45960311115695 1.00000000000000 2.71408084660822 2.71828182845904
#simple example of Trapezoid Method var('t','y') f(t,y) = t*y+t^3 start = 0.0 end = 1.0 solution(t) = 3*float(e)^(t^2/2)-t^2-2 numOfIterations = 2 h = (end-start)/numOfIterations y00 = 1.0 #initial value t00 = start; soln = [[t00,y00]] for i in range(numOfIterations): y00 = y00+h/2*(f(t00,y00) + f(t00+h,y00 + h*f(t00,y00))) t00=t00+h soln.append([t00,y00]) print "%10r %20r %20r"%(t00,y00,solution(t00))
0.500000000000000 1.15625000000000 1.14944535920048 1.00000000000000 1.95898437500000 1.94616381210038