Runge-Kutta method: Difference between revisions

Added Easylang
(Added Easylang)
 
(6 intermediate revisions by 4 users not shown)
Line 906:
y(10.0) = 675.99994902 Error = 7.5419063100e-8
</pre>
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
numfmt 6 0
y = 1
for i = 0 to 100
t = i / 10
if t = floor t
h = t * t + 4
actual = h * h / 16
print "y(" & t & ") = " & y & " Error = " & actual - y
.
k1 = t * sqrt y
k2 = (t + 0.05) * sqrt (y + 0.05 * k1)
k3 = (t + 0.05) * sqrt (y + 0.05 * k2)
k4 = (t + 0.10) * sqrt (y + 0.10 * k3)
y += 0.1 * (k1 + 2 * (k2 + k3) + k4) / 6
.
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
Line 1,119 ⟶ 1,139:
 
=={{header|Excel}}==
<syntaxhighlight lang="Excel">PRINT "RungaKutta4λ(D)"</syntaxhighlight>
//Worksheet formula to manage looping
 
=LET(
T₊, SEQUENCE(11, 1, 0, 1),
Line 1,131 ⟶ 1,152:
)
 
//Lambda function passed to RK4RungaKutta4λ to evaluate derivatives
= t * SQRT(y)
 
Dλ(y,t)
Curried Lambda function with derivative function and y, t as parameters
= LAMBDA(y,t, t * SQRT(y))
RungaKutta4λ
 
//Curried Lambda function with derivative function D and y, t as parameters
 
RungaKutta4λ(Dλ)
= 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 )
 
f(t)
Results
= LAMBDA(t, (1/16) * (t^2 + 4)^2 )
</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|F_Sharp|F#}}==
Line 3,719 ⟶ 3,745:
y(9.0) = 451.56245928 Error: 4.07231581e-05
y(10.0) = 675.99994902 Error: 5.09832864e-05</pre>
 
=={{header|V (Vlang)}}==
{{trans|Ring}}
<syntaxhighlight lang="Zig">
import math
 
fn main() {
mut t, mut k1, mut k2, mut k3, mut k4, mut y := 0.0, 0.0, 0.0, 0.0, 0.0, 1.0
for i in 0..101 {
t = i / 10.0
if t == math.floor(t) {
actual := math.pow((math.pow(t, 2) + 4), 2)/16
println("y(${t:.0}) = ${y:.8f} error = ${(actual - y):.8f}")
}
k1 = t * math.sqrt(y)
k2 = (t + 0.05) * math.sqrt(y + 0.05 * k1)
k3 = (t + 0.05) * math.sqrt(y + 0.05 * k2)
k4 = (t + 0.10) * math.sqrt(y + 0.10 * k3)
y += 0.1 * (k1 + 2 * (k2 + k3) + k4) / 6
}
}
</syntaxhighlight>
 
{{out}}
<pre>
y(0) = 1.00000000 error = 0.00000000
y(1) = 1.56249985 error = 0.00000015
y(2) = 3.99999908 error = 0.00000092
y(3) = 10.56249709 error = 0.00000291
y(4) = 24.99999377 error = 0.00000623
y(5) = 52.56248918 error = 0.00001082
y(6) = 99.99998341 error = 0.00001659
y(7) = 175.56247648 error = 0.00002352
y(8) = 288.99996843 error = 0.00003157
y(9) = 451.56245928 error = 0.00004072
y(10) = 675.99994902 error = 0.00005098
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var rungeKutta4 = Fn.new { |t0, tz, dt, y, yd|
Line 3,769 ⟶ 3,832:
9.0 451.562459 451.562500 -0.000041
10.0 675.999949 676.000000 -0.000051
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func real Y_(T, Y);
real T, Y;
return T*sqrt(Y);
 
def DT = 0.1;
real T, Y, Exact, DY1, DY2, DY3, DY4;
[Text(0, " T RK Exact Error^m^j");
T:= 0.; Y:= 1.;
repeat if Mod(T+.001, 1.) < .01 then
[Format(2, 1);
RlOut(0, T);
Format(5, 7);
RlOut(0, Y);
Exact:= sq(T*T+4.)/16.;
RlOut(0, Exact);
RlOut(0, Y-Exact);
CrLf(0);
];
DY1:= DT * Y_(T, Y);
DY2:= DT * Y_(T+DT/2., Y+DY1/2.);
DY3:= DT * Y_(T+DT/2., Y+DY2/2.);
DY4:= DT * Y_(T+DT, Y+DY3);
Y:= Y + (DY1 + 2.*DY2 + 2.*DY3 + DY4) / 6.;
T:= T + DT;
until T > 10.;
]</syntaxhighlight>
{{out}}
<pre>
T RK Exact Error
0.0 1.0000000 1.0000000 0.0000000
1.0 1.5624999 1.5625000 -0.0000001
2.0 3.9999991 4.0000000 -0.0000009
3.0 10.5624971 10.5625000 -0.0000029
4.0 24.9999938 25.0000000 -0.0000062
5.0 52.5624892 52.5625000 -0.0000108
6.0 99.9999834 100.0000000 -0.0000166
7.0 175.5624765 175.5625000 -0.0000235
8.0 288.9999684 289.0000000 -0.0000316
9.0 451.5624593 451.5625000 -0.0000407
10.0 675.9999490 676.0000000 -0.0000510
</pre>
 
1,973

edits