Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,25 @@
PROGRAM RUNGE_KUTTA
CONST DELTA_T=0.1
FUNCTION Y1(T,Y)
Y1=T*SQR(Y)
END FUNCTION
BEGIN
Y=1.0
FOR I%=0 TO 100 DO
T=I%*DELTA_T
IF T=INT(T) THEN ! print every tenth
ACTUAL=((T^2+4)^2)/16 ! exact solution
PRINT("Y(";T;")=";Y;TAB(20);"Error=";ACTUAL-Y)
END IF
K1=Y1(T,Y)
K2=Y1(T+DELTA_T/2,Y+DELTA_T/2*K1)
K3=Y1(T+DELTA_T/2,Y+DELTA_T/2*K2)
K4=Y1(T+DELTA_T,Y+DELTA_T*K3)
Y+=DELTA_T*(K1+2*(K2+K3)+K4)/6
END FOR
END PROGRAM

View file

@ -0,0 +1,31 @@
' version 03-10-2015
' compile with: fbc -s console
' translation of BBC BASIC
Dim As Double y = 1, t, actual, k1, k2, k3, k4
Print
For i As Integer = 0 To 100
t = i / 10
If t = Int(t) Then
actual = ((t ^ 2 + 4) ^ 2) / 16
Print "y("; Str(t); ") ="; y ; Tab(27); "Error = "; actual - y
End If
k1 = t * Sqr(y)
k2 = (t + 0.05) * Sqr(y + 0.05 * k1)
k3 = (t + 0.05) * Sqr(y + 0.05 * k2)
k4 = (t + 0.10) * Sqr(y + 0.10 * k3)
y += 0.1 * (k1 + 2 * (k2 + k3) + k4) / 6
Next i
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,29 @@
include "ConsoleWindow"
def tab 9
local fn dydx( x as double, y as double ) as double
end fn = x * sqr(y)
local fn exactY( x as long ) as double
end fn = ( x ^2 + 4 ) ^2 / 16
dim as long i
dim as double h, k1, k2, k3, k4, x, y, result
h = 0.1
y = 1
for i = 0 to 100
x = i * h
if x == int(x)
result = fn exactY( x )
print "y("; mid$( str$(x), 2, len(str$(x) )); ") = "; y, "Error = "; result - y
end if
k1 = h * fn dydx( x, y )
k2 = h * fn dydx( x + h / 2, y + k1 / 2 )
k3 = h * fn dydx( x + h / 2, y + k2 / 2 )
k4 = h * fn dydx( x + h, y + k3 )
y = y + 1 / 6 * ( k1 + 2 * k2 + 2 * k3 + k4 )
next

View file

@ -0,0 +1,13 @@
decimals(8)
y = 1.0
for i = 0 to 100
t = i / 10
if t = floor(t)
actual = (pow((pow(t,2) + 4),2)) / 16
see "y(" + t + ") = " + y + " error = " + (actual - y) + nl ok
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
next

View file

@ -0,0 +1,21 @@
func runge_kutta(yp) {
func (t, y, δt) {
var a = (δt * yp(t, y));
var b = (δt * yp(t + δt/2, y + a/2));
var c = (δt * yp(t + δt/2, y + b/2));
var d = (δt * yp(t + δt, y + c));
(a + 2*(b + c) + d) / 6;
}
}
define δt = 0.1;
var δy = runge_kutta(func(t, y) { t * y.sqrt });
var(t, y) = (0, 1);
loop {
t.is_int &&
printf("y(%2d) = %12f ± %e\n", t, y, abs(y - ((t**2 + 4)**2 / 16)));
t <= 10 || break;
y += δy(t, y, δt);
t += δt;
}

View file

@ -0,0 +1,38 @@
import Foundation
func rk4(dx: Double, x: Double, y: Double, f: (Double, Double) -> Double) -> Double {
let k1 = dx * f(x, y)
let k2 = dx * f(x + dx / 2, y + k1 / 2)
let k3 = dx * f(x + dx / 2, y + k2 / 2)
let k4 = dx * f(x + dx, y + k3)
return y + (k1 + 2 * k2 + 2 * k3 + k4) / 6
}
var y = [Double]()
var x: Double = 0.0
var y2: Double = 0.0
var x0: Double = 0.0
var x1: Double = 10.0
var dx: Double = 0.1
var i = 0
var n = Int(1 + (x1 - x0) / dx)
y.append(1)
for i in 1..<n {
y.append(rk4(dx, x: x0 + dx * (Double(i) - 1), y: y[i - 1]) { (x: Double, y: Double) -> Double in
return x * sqrt(y)
})
}
print(" x y rel. err.")
print("------------------------------")
for (var i = 0; i < n; i += 10) {
x = x0 + dx * Double(i)
y2 = pow(x * x / 4 + 1, 2)
print(String(format: "%2g %11.6g %11.5g", x, y[i], y[i]/y2 - 1))
}

View file

@ -0,0 +1,7 @@
def until(cond; next):
def _until: if cond then . else (next|_until) end;
_until;
def while(cond; update):
def _while: if cond then ., (update | _while) else empty end;
_while;

View file

@ -0,0 +1,8 @@
# yprime maps [t,y] to a number, i.e. t * sqrt(y)
def yprime: .[0] * (.[1] | sqrt);
# The exact solution of yprime:
def actual:
. as $t
| (( $t*$t) + 4 )
| . * . / 16;

View file

@ -0,0 +1,9 @@
# n is the number of decimal places of precision
def round(n):
(if . < 0 then -1 else 1 end) as $s
| $s*10*.*n | if (floor % 10) > 4 then (.+5) else . end | ./10 | floor/n | .*$s;
def abs: if . < 0 then -. else . end;
# Is the input an integer?
def integerq: ((. - ((.+.01) | floor)) | abs) < 0.01;

View file

@ -0,0 +1,14 @@
def dt: 0.1;
# Input: [t, y]; yp is a filter that accepts [t,y] as input
def runge_kutta(yp):
.[0] as $t | .[1] as $y
| (dt * yp) as $a
| (dt * ([ ($t + (dt/2)), $y + ($a/2) ] | yp)) as $b
| (dt * ([ ($t + (dt/2)), $y + ($b/2) ] | yp)) as $c
| (dt * ([ ($t + dt) , $y + $c ] | yp)) as $d
| ($a + (2*($b + $c)) + $d) / 6
;
# Input: [t,y]
def dy(f): runge_kutta(f);

View file

@ -0,0 +1,10 @@
# state: [t,y]
[0,1]
| while( .[0] <= 10;
.[0] as $t | .[1] as $y
| [$t + dt, $y + dy(yprime) ] )
| .[0] as $t | .[1] as $y
| if $t | integerq then
"y(\($t|round(1))) = \($y|round(10000)) ± \( ($t|actual) - $y | abs)"
else empty
end

View file

@ -0,0 +1,16 @@
$ time jq -r -n -f rk4.pl.jq
y(0) = 1 ± 0
y(1) = 1.5625 ± 1.4572189210859676e-07
y(2) = 4 ± 9.194792029987298e-07
y(3) = 10.5625 ± 2.9095624576314094e-06
y(4) = 25 ± 6.234909392333066e-06
y(5) = 52.5625 ± 1.081969734428867e-05
y(6) = 100 ± 1.659459609015812e-05
y(7) = 175.5625 ± 2.3517728038768837e-05
y(8) = 289 ± 3.156520000402452e-05
y(9) = 451.5625 ± 4.072315812209126e-05
y(10) = 675.9999 ± 5.0983286655537086e-05
real 0m0.048s
user 0m0.013s
sys 0m0.006s

View file

@ -0,0 +1,43 @@
# Input: [t, y, dt]
def newRK4Step(yp):
.[0] as $t | .[1] as $y | .[2] as $dt
| ($dt * ([$t, $y]|yp)) as $dy1
| ($dt * ([$t+$dt/2, $y+$dy1/2]|yp)) as $dy2
| ($dt * ([$t+$dt/2, $y+$dy2/2]|yp)) as $dy3
| ($dt * ([$t+$dt, $y+$dy3] |yp)) as $dy4
| $y + ($dy1+2*($dy2+$dy3)+$dy4)/6
;
def printErr: # input: [t, y]
def abs: if . < 0 then -. else . end;
.[0] as $t | .[1] as $y
| "y(\($t)) = \($y) with error: \( (($t|actual) - $y) | abs )"
;
def main(t0; y0; tFinal; dtPrint):
def ypStep: newRK4Step(yprime) ;
0.1 as $dtStep # step value
# [ t, y] is the state vector
| [ t0, y0 ]
| while( .[0] <= tFinal;
.[0] as $t | .[1] as $y
| ($t + dtPrint) as $t1
| (((dtPrint/$dtStep) + 0.5) | floor) as $steps
| [$steps, $t, $y] # state vector
| until( .[0] <= 1;
.[0] as $steps
| .[1] as $t
| .[2] as $y
| [ ($steps - 1), ($t + $dtStep), ([$t, $y, $dtStep]|ypStep) ]
)
| .[1] as $t | .[2] as $y
| [$t1, ([ $t, $y, ($t1-$t)] | ypStep)] # adjust step to integer time
)
| printErr # print results
;
# main(t0; y0; tFinal; dtPrint)
main(0; 1; 10; 1)

View file

@ -0,0 +1,16 @@
$ time jq -n -r -f runge-kutta.jq
y(0) = 1 with error: 0
y(1) = 1.562499854278108 with error: 1.4572189210859676e-07
y(2) = 3.9999990805207974 with error: 9.194792025546406e-07
y(3) = 10.562497090437544 with error: 2.9095624558550526e-06
y(4) = 24.999993765090615 with error: 6.234909385227638e-06
y(5) = 52.562489180302656 with error: 1.081969734428867e-05
y(6) = 99.99998340540387 with error: 1.6594596132790684e-05
y(7) = 175.56247648227188 with error: 2.3517728124033965e-05
y(8) = 288.9999684347997 with error: 3.156520028824161e-05
y(9) = 451.56245927684154 with error: 4.0723158463151776e-05
y(10) = 675.9999490167129 with error: 5.0983287110284436e-05
real 0m0.023s
user 0m0.014s
sys 0m0.006s