Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
15
Task/Euler-method/Futhark/euler-method.futhark
Normal file
15
Task/Euler-method/Futhark/euler-method.futhark
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
fun analytic(t0: f64) (time: f64): f64 =
|
||||
20.0 + (t0 - 20.0) * exp64(-0.07*time)
|
||||
|
||||
fun cooling(_time: f64) (temperature: f64): f64 =
|
||||
-0.07 * (temperature-20.0)
|
||||
|
||||
fun main(t0: f64) (a: f64) (b: f64) (h: f64): []f64 =
|
||||
let steps = int((b-a)/h)
|
||||
let temps = replicate steps 0.0
|
||||
loop ((t,temps)=(t0,temps)) = for i < steps do
|
||||
let x = a + f64(i) * h
|
||||
let temps[i] = abs(t-analytic t0 x)
|
||||
in (t + h * cooling x t,
|
||||
temps)
|
||||
in temps
|
||||
13
Task/Euler-method/Nim/euler-method.nim
Normal file
13
Task/Euler-method/Nim/euler-method.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import strutils
|
||||
|
||||
proc euler(f: proc (x,y: float): float; y0, a, b, h: float) =
|
||||
var (t,y) = (a,y0)
|
||||
while t < b:
|
||||
echo formatFloat(t, ffDecimal, 3), " ", formatFloat(y, ffDecimal, 3)
|
||||
t += h
|
||||
y += h * f(t,y)
|
||||
|
||||
proc newtoncooling(time, temp): float =
|
||||
-0.07 * (temp - 20)
|
||||
|
||||
euler(newtoncooling, 100.0, 0.0, 100.0, 10.0)
|
||||
6
Task/Euler-method/Oforth/euler-method-1.oforth
Normal file
6
Task/Euler-method/Oforth/euler-method-1.oforth
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
: euler(f, y, a, b, h)
|
||||
| t |
|
||||
a b h step: t [
|
||||
System.Out t <<wjp(6, JUSTIFY_RIGHT, 3) " : " << y << cr
|
||||
t y f perform h * y + ->y
|
||||
] ;
|
||||
7
Task/Euler-method/Oforth/euler-method-2.oforth
Normal file
7
Task/Euler-method/Oforth/euler-method-2.oforth
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
: newtonCoolingLaw(t, y)
|
||||
y 20 - -0.07 * ;
|
||||
|
||||
: test
|
||||
euler(#newtonCoolingLaw, 100.0, 0.0, 100.0, 2)
|
||||
euler(#newtonCoolingLaw, 100.0, 0.0, 100.0, 5)
|
||||
euler(#newtonCoolingLaw, 100.0, 0.0, 100.0, 10) ;
|
||||
33
Task/Euler-method/Phix/euler-method.phix
Normal file
33
Task/Euler-method/Phix/euler-method.phix
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
constant FMT = " %7.3f"
|
||||
|
||||
procedure ivp_euler(integer f, atom y, integer step, integer end_t)
|
||||
integer t = 0;
|
||||
|
||||
printf(1, " Step %2d: ", step);
|
||||
while t<=end_t do
|
||||
if remainder(t,10)==0 then printf(1, FMT, y) end if
|
||||
y += step * call_func(f,{t, y});
|
||||
t += step
|
||||
end while
|
||||
printf(1, "\n");
|
||||
end procedure
|
||||
|
||||
procedure analytic()
|
||||
printf(1, " Time: ");
|
||||
for t = 0 to 100 by 10 do printf(1," %7g", t) end for
|
||||
printf(1, "\nAnalytic: ");
|
||||
for t = 0 to 100 by 10 do
|
||||
printf(1, FMT, 20 + 80 * exp(-0.07 * t))
|
||||
end for
|
||||
printf(1,"\n");
|
||||
end procedure
|
||||
|
||||
function cooling(atom /*t*/, atom temp)
|
||||
return -0.07 * (temp - 20);
|
||||
end function
|
||||
constant r_cooling = routine_id("cooling")
|
||||
|
||||
analytic();
|
||||
ivp_euler(r_cooling, 100, 2, 100);
|
||||
ivp_euler(r_cooling, 100, 5, 100);
|
||||
ivp_euler(r_cooling, 100, 10, 100);
|
||||
13
Task/Euler-method/Ring/euler-method.ring
Normal file
13
Task/Euler-method/Ring/euler-method.ring
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
decimals(3)
|
||||
see euler("return -0.07*(y-20)", 100, 0, 100, 2) + nl
|
||||
see euler("return -0.07*(y-20)", 100, 0, 100, 5) + nl
|
||||
see euler("return -0.07*(y-20)", 100, 0, 100, 10) + nl
|
||||
|
||||
func euler df, y, a, b, s
|
||||
t = a
|
||||
while t <= b
|
||||
see "" + t + " " + y + nl
|
||||
y += s * eval(df)
|
||||
t += s
|
||||
end
|
||||
return y
|
||||
25
Task/Euler-method/SequenceL/euler-method.sequencel
Normal file
25
Task/Euler-method/SequenceL/euler-method.sequencel
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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);
|
||||
30
Task/Euler-method/Sidef/euler-method.sidef
Normal file
30
Task/Euler-method/Sidef/euler-method.sidef
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
func euler_method(t0, t1, k, step_size) {
|
||||
var results = [[0, t0]]
|
||||
for s in (step_size..100 -> by(step_size)) {
|
||||
t0 -= ((t0 - t1) * k * step_size)
|
||||
results << [s, t0]
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
func analytical(t0, t1, k, time) {
|
||||
(t0 - t1) * exp(-time * k) + t1
|
||||
}
|
||||
|
||||
var (T0, T1, k) = (100, 20, .07)
|
||||
var r2 = euler_method(T0, T1, k, 2).grep { _[0] %% 10 }
|
||||
var r5 = euler_method(T0, T1, k, 5).grep { _[0] %% 10 }
|
||||
var r10 = euler_method(T0, T1, k, 10).grep { _[0] %% 10 }
|
||||
|
||||
say "Time\t 2 err(%) 5 err(%) 10 err(%) Analytic"
|
||||
say "-"*76
|
||||
|
||||
r2.range.each { |i|
|
||||
var an = analytical(T0, T1, k, r2[i][0])
|
||||
printf("%4d\t#{'%9.3f' * 7}\n",
|
||||
r2[i][0],
|
||||
r2[i][1], ( r2[i][1] / an) * 100 - 100,
|
||||
r5[i][1], ( r5[i][1] / an) * 100 - 100,
|
||||
r10[i][1], (r10[i][1] / an) * 100 - 100,
|
||||
an)
|
||||
}
|
||||
24
Task/Euler-method/jq/euler-method-1.jq
Normal file
24
Task/Euler-method/jq/euler-method-1.jq
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# euler_method takes a filter (df), initial condition
|
||||
# (x1,y1), ending x (x2), and step size as parameters;
|
||||
# it emits the y values at each iteration.
|
||||
# df must take [x,y] as its input.
|
||||
def euler_method(df; x1; y1; x2; h):
|
||||
h as $h
|
||||
| [x1, y1]
|
||||
| recurse( if ((.[0] < x2 and x1 < x2) or
|
||||
(.[0] > x2 and x1 > x2)) then
|
||||
[ (.[0] + $h), (.[1] + $h*df) ]
|
||||
else empty
|
||||
end )
|
||||
| .[1] ;
|
||||
|
||||
|
||||
# We could now solve the task by writing for each step-size, $h
|
||||
# euler_method(-0.07 * (.[1]-20); 0; 100; 100; $h)
|
||||
# but for clarity, we shall define a function named "cooling":
|
||||
|
||||
# [x,y] is input
|
||||
def cooling: -0.07 * (.[1]-20);
|
||||
|
||||
# The following solves the task:
|
||||
# (2,5,10) | [., [ euler_method(cooling; 0; 100; 100; .) ] ]
|
||||
10
Task/Euler-method/jq/euler-method-2.jq
Normal file
10
Task/Euler-method/jq/euler-method-2.jq
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def euler_solution(df; x1; y1; x2; h):
|
||||
def recursion(exp): reduce recurse(exp) as $x (.; $x);
|
||||
h as $h
|
||||
| [x1, y1]
|
||||
| recursion( if ((.[0] < x2 and x1 < x2) or
|
||||
(.[0] > x2 and x1 > x2)) then
|
||||
[ (.[0] + $h), (.[1] + $h*df) ]
|
||||
else empty
|
||||
end )
|
||||
| .[1] ;
|
||||
1
Task/Euler-method/jq/euler-method-3.jq
Normal file
1
Task/Euler-method/jq/euler-method-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
(1,2,5,10,20) | [., [ euler_solution(cooling; 0; 100; 100; .) ] ]
|
||||
6
Task/Euler-method/jq/euler-method-4.jq
Normal file
6
Task/Euler-method/jq/euler-method-4.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$ jq -M -n -c -f Euler_method.jq
|
||||
[1,[20.05641373347389]]
|
||||
[2,[20.0424631833732]]
|
||||
[5,[20.01449963666907]]
|
||||
[10,[20.000472392]]
|
||||
[20,[19.180799999999998]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue