Runge-Kutta method: Difference between revisions

m (syntax highlighting fixup automation)
Line 1,117:
Y( 10 )= 676.0001 Error=-6.103516E-05
</pre>
 
=={{header|Excel}}==
<syntaxhighlight lang="Excel">PRINT "RungaKutta4λ(D)"</syntaxhighlight>
Worksheet formula to manage looping
=LET(
T₊, SEQUENCE(11, 1, 0, 1),
T, DROP(T₊, -1),
τ, SEQUENCE(1 / δt, 1, 0, δt),
calculated, SCAN(1, T, LAMBDA(y₀, t, REDUCE(y₀, t + τ, RungaKutta4λ(Dλ)))),
calcs, VSTACK(1, calculated),
exact, f(T₊),
HSTACK(T₊, calcs, exact, (exact - calcs) / exact)
)
 
Lambda function passed to RK4 to evaluate derivatives
= t * SQRT(y)
 
Curried Lambda function with derivative function and y, t as parameters
RungaKutta4λ
= LAMBDA(D,
LAMBDA(yᵣ, tᵣ,
LET(
δy₁, δt * D(yᵣ, tᵣ),
δy₂, δt * D(yᵣ + δy₁ / 2, tᵣ + δt / 2),
δy₃, δt * D(yᵣ + δy₂ / 2, tᵣ + δt / 2),
δy₄, δt * D(yᵣ + δy₃, tᵣ + δt),
yᵣ₊₁, yᵣ + (δy₁ + 2 * δy₂ + 2 * δy₃ + δy₄) / 6,
yᵣ₊₁
)
)
)
 
Lambda function returning the exact solution
f
= LAMBDA(t, (1 / 16) * (t ^ 2 + 4) ^ 2 )
 
Results
Time Calculated Exact Rel Error
0.00 1.000000 1.000000 0.00E+00
1.00 1.562500 1.562500 9.33E-08
2.00 3.999999 4.000000 2.30E-07
3.00 10.562497 10.562500 2.75E-07
4.00 24.999994 25.000000 2.49E-07
5.00 52.562489 52.562500 2.06E-07
6.00 99.999983 100.000000 1.66E-07
7.00 175.562476 175.562500 1.34E-07
8.00 288.999968 289.000000 1.09E-07
9.00 451.562459 451.562500 9.02E-08
10.00 675.999949 676.000000 7.54E-08
 
=={{header|F_Sharp|F#}}==
4

edits