Runge-Kutta method: Difference between revisions
Content deleted Content added
Line 2,241:
y(9) = 451.562459 Error = 0.4072316e-4
y(10) = 675.999949 Error = 0.5098329e-4
</pre>
=={{header|Lua}}==
<lang Lua>local df = function (t, y)
-- derivative of function by value y at time t
return t*y^0.5
end
local dt = 0.1
local y = 1
print ("t", "realY"..' ', "y", ' '.."error")
print ("---", "-------"..' ', "---------------", ' '.."--------------------")
for i = 0, 100 do
local t = i*dt
if t%1 == 0 then
local realY = (t*t+4)^2/16
print (t, realY..' ', y, ' '..realY-y)
end
local dy1 = df(t, y)
local dy2 = df(t+dt/2, y+dt/2*dy1)
local dy3 = df(t+dt/2, y+dt/2*dy2)
local dy4 = df(t+dt, y+dt*dy3)
y = y + dt*(dy1+2*dy2+2*dy3+dy4)/6
end</lang>
{{Out}}
<pre>t realY y error
--- ------- --------------- --------------------
0.0 1.0 1 0.0
1.0 1.5625 1.5624998542781 1.457218921086e-007
2.0 4.0 3.9999990805208 9.1947919989011e-007
3.0 10.5625 10.562497090438 2.9095624469733e-006
4.0 25.0 24.999993765091 6.2349093639114e-006
5.0 52.5625 52.562489180303 1.0819697415343e-005
6.0 100.0 99.999983405404 1.6594596417008e-005
7.0 175.5625 175.56247648227 2.3517728749312e-005
8.0 289.0 288.9999684348 3.156520142511e-005
9.0 451.5625 451.56245927684 4.0723160338985e-005
10.0 676.0 675.99994901671 5.0983290293516e-005
</pre>
|