Jump to content

Euler method: Difference between revisions

Adding SequenceL
(→‎{{header|Common Lisp}}: slightly more modern/idiomatic version)
(Adding SequenceL)
Line 2,448:
90 20.00157
DONE
</pre>
 
 
=={{header|SequenceL}}==
 
<lang sequencel>import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
 
T0 := 100.0;
TR := 20.0;
k := 0.07;
 
main(args(2)) :=
let
results[i] := euler(newtonCooling, T0, 100, stringToInt(args[i]), 0, "delta_t = " ++ args[i]);
in
delimit(results, '\n');
 
newtonCooling(t) := -k * (t - TR);
 
euler: (float -> float) * float * int * int * int * char(1) -> char(1);
euler(f, y, n, h, x, output(1)) :=
let
newOutput := output ++ "\n\t" ++ intToString(x) ++ "\t" ++ floatToString(y, 3);
newY := y + h * f(y);
newX := x + h;
in
output when x > n
else
euler(f, newY, n, h, newX, newOutput);</lang>
Based on C# version [http://rosettacode.org/wiki/Euler_method#C.23] but using tail recursion instead of looping.
{{out}}
 
For step size 10:
<pre>
main.exe 10
"delta_t = 10
0 100.000
10 44.000
20 27.200
30 22.160
40 20.648
50 20.194
60 20.058
70 20.017
80 20.005
90 20.002
100 20.000"
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.