awk — Numerical Calculus

Numerical derivative — finite difference approximation
# f(x) = x³ at x=2, expect f'(2) = 12
awk 'BEGIN{
    x=2; h=0.0001
    f_plus=(x+h)^3
    f_minus=(x-h)^3
    deriv=(f_plus-f_minus)/(2*h)
    printf "f'\''(%.0f) ≈ %.6f\n", x, deriv
}'
Trapezoidal rule — integrate f(x) = x² from 0 to 1
awk 'BEGIN{
    a=0; b=1; n=1000
    h=(b-a)/n
    sum=0.5*((a)^2 + (b)^2)
    for(i=1;i<n;i++) sum+=(a+i*h)^2
    printf "∫x² dx from %d to %d ≈ %.6f (exact: 0.333333)\n", a, b, sum*h
}'
Simpson’s rule — more accurate numerical integration
# ∫sin(x) dx from 0 to π, expect 2.0
awk 'BEGIN{
    pi=atan2(0,-1)
    a=0; b=pi; n=1000
    h=(b-a)/n
    sum=sin(a)+sin(b)
    for(i=1;i<n;i++){
        x=a+i*h
        sum+=sin(x)*((i%2==0)?2:4)
    }
    printf "∫sin(x) dx from 0 to π ≈ %.6f (exact: 2.0)\n", sum*h/3
}'
Rate of change from timestamped data — requests per second
cat <<'EOF' | awk '{
    if(NR>1){
        dt=$1-prev_t
        dy=$2-prev_y
        if(dt>0) printf "t=%d→%d: Δy/Δt = %.2f per sec\n", prev_t, $1, dy/dt
    }
    prev_t=$1; prev_y=$2
}'
0 100
5 250
10 480
15 600
20 750
30 1100
EOF
Numerical integration of sampled data — trapezoidal on discrete points
# Power consumption samples (watts) every 5 minutes — total energy in watt-hours
cat <<'EOF' | awk '{t[NR]=$1; v[NR]=$2} END{
    total=0
    for(i=2;i<=NR;i++){
        dt=(t[i]-t[i-1])/60  # minutes to hours
        total+=(v[i]+v[i-1])/2*dt
    }
    printf "total energy: %.2f Wh over %d minutes\n", total, t[NR]-t[1]
}'
0 150
5 175
10 200
15 320
20 280
25 190
30 160
EOF
Newton’s method — find root of f(x) = x³ - 2 (cube root of 2)
awk 'BEGIN{
    x=1.5   # initial guess
    for(i=1;i<=20;i++){
        fx=x^3-2
        fpx=3*x^2
        x_new=x-fx/fpx
        if(sqrt((x_new-x)^2)<1e-10) break
        x=x_new
    }
    printf "³√2 ≈ %.10f (iterations: %d)\n", x, i
}'
Exponential growth/decay — continuous model
# N(t) = N₀ × e^(kt)
# Bacteria doubles every 20 minutes. Population at t=60?
awk 'BEGIN{
    N0=1000
    k=log(2)/20    # doubling time → growth rate
    for(t=0;t<=60;t+=10){
        Nt=N0*exp(k*t)
        printf "t=%2d min: N=%.0f\n", t, Nt
    }
}'
Limit approximation — lim(sin(x)/x) as x→0
awk 'BEGIN{
    printf "%-12s %s\n", "x", "sin(x)/x"
    x=1
    for(i=1;i<=8;i++){
        printf "%-12.8f %.10f\n", x, sin(x)/x
        x=x/10
    }
    printf "\nlimit = 1.0\n"
}'
Second derivative — curvature detection
# f(x) = sin(x), f''(x) = -sin(x)
awk 'BEGIN{
    x=1.0; h=0.001
    f_deriv2=(sin(x+h)-2*sin(x)+sin(x-h))/(h^2)
    printf "f'\'''\''(%.1f) ≈ %.6f (exact: %.6f)\n", x, f_deriv2, -sin(x)
}'
Area under a curve from sampled network throughput
# Mbps samples every 10 seconds — total data transferred in MB
cat <<'EOF' | awk '{t[NR]=$1; v[NR]=$2} END{
    total=0
    for(i=2;i<=NR;i++){
        dt=t[i]-t[i-1]
        avg=(v[i]+v[i-1])/2   # Mbps
        total+=avg*dt/8        # Mbps × seconds / 8 = MB
    }
    printf "total transferred: %.2f MB over %d seconds\n", total, t[NR]-t[1]
}'
0 100
10 150
20 200
30 180
40 220
50 190
60 170
EOF