Numerical analysis
num derivativenum derivative2num integratenum poly_addnum poly_derivenum poly_evalnum poly_integratenum poly_mulnum poly_rootsnum solvenum fftnum ifftnum interpolatenum maximizenum minimizenum odenum uncertaintynum solve_systemnum integrate2num sumnum tablenum odenum odenum minimizenum minimizenum maximizenum maximizenum interpolatenum interpolate_derivativenum interpolate_derivativenum interpolate_integralnum interpolate2

num derivative <expression> <variable> <x0>


Description

    Get the first derivative of a function at a point.
Automatic differentiation: the result is exact up to rounding (no finite differences).

Parameters

    expression:   The function, ex: x^2-2 - String - required
    variable:   The name of the variable, ex: x - String - required
    x0:   The point - Number - required
admin
num derivative "x^3" "x" 2
mentdb
12
admin
num derivative "sin(x)" "x" 0
mentdb
1
Velocity from a position law x(t) = 5 t^2 + 2 t
admin
num derivative "5*t^2+2*t" "t" 3
mentdb
32
Derivative of a logarithm
admin
num derivative "ln(x)" "x" 2
mentdb
0.5
Slope of a velocity at t = 2 (acceleration)
admin
num derivative "4.9*t^2" "t" 2
mentdb
19.6
Special function: J0'(1) = -J1(1)
admin
num derivative "bessel_j(0, x)" "x" 1
mentdb
-0.44005058574493355
Derivative of exp(-3x) at 0
admin
num derivative "exp(-3*x)" "x" 0
mentdb
-3

num derivative2 <expression> <variable> <x0>


Description

    Get the second derivative of a function at a point (automatic differentiation).

Parameters

    expression:   The function, ex: x^2-2 - String - required
    variable:   The name of the variable, ex: x - String - required
    x0:   The point - Number - required
admin
num derivative2 "x^3" "x" 2
mentdb
12
admin
num derivative2 "sin(x)" "x" 1.5707963267948966
mentdb
-1
Curvature of exp at 0
admin
num derivative2 "exp(x)" "x" 0
mentdb
1
Concavity of a cosine at 0: -1
admin
num derivative2 "cos(x)" "x" 0
mentdb
-1
Inflection point of x^3: 0
admin
num derivative2 "x^3" "x" 0
mentdb
0
Acceleration from a position
admin
num derivative2 "5*t^3-2*t" "t" 1
mentdb
30

num integrate <expression> <variable> <a> <b>


Description

    Get the definite integral of a function on [a, b]. The bounds can be infinite: inf, -inf (change of variable to a finite interval).
Adaptive Gauss-Kronrod 7-15: relative precision about 1e-12, integrable singularities at the bounds are supported (sqrt(x), 1/sqrt(x) on [0, 1]).
If a > b, the result is negative.

Parameters

    expression:   The function, ex: x^2-2 - String - required
    variable:   The name of the variable, ex: x - String - required
    a:   The lower bound - Number - required
    b:   The upper bound - Number - required
admin
num integrate "x^2" "x" 0 1
mentdb
0.3333333333333333
admin
num integrate "exp(-x^2)" "x" -10 10
mentdb
1.7724538509055163
Infinite bound
admin
num integrate "1/(1+x^2)" "x" "-inf" "inf"
mentdb
3.141592653589793
Special function
admin
num integrate "bessel_j(0, x)" "x" 0 10
mentdb
1.067011303956737
Area under sin between 0 and pi = 2
admin
num integrate "sin(x)" "x" 0 3.141592653589793
mentdb
2
Normal law: P(-1.96 < Z < 1.96)
admin
num integrate "exp(-x^2/2)/sqrt(2*pi)" "x" -1.96 1.96
mentdb
0.9500042097035593
Infinite bound: integral of exp(-x) from 0 to infinity = 1
admin
num integrate "exp(-x)" "x" 0 "inf"
mentdb
0.9999999999999999
Singularity at the bound: integral of 1/sqrt(x) on [0, 1] = 2
admin
num integrate "1/sqrt(x)" "x" 0 1
mentdb
1.9999999999997646
Work of a spring F = k x (k = 200 N/m) from 0 to 0.1 m
admin
num integrate "200*x" "x" 0 0.1
mentdb
1
Length of an arc: y = x^2 from 0 to 1
admin
num integrate "sqrt(1+4*x^2)" "x" 0 1
mentdb
1.4789428575445975
Oscillating function
admin
num integrate "sin(x)/x" "x" 1e-9 100
mentdb
1.562225465889056
Reversed bounds: negative value
admin
num integrate "x" "x" 1 0
mentdb
-0.5

num poly_add <json_coeffs1> <json_coeffs2>


Description

    Add two polynomials

Parameters

    json_coeffs1:   The coefficients of the first polynomial (ascending order) - String - required
    json_coeffs2:   The coefficients of the second polynomial (ascending order) - String - required
admin
num poly_add "[1, 2]" "[0, 0, 3]"
mentdb
[1,2,3]
Sum of two polynomials
admin
num poly_add "[1, 2, 3]" "[-1, 0, 1]"
mentdb
[0,2,4]
The coefficients are in increasing powers: (1 + x) + (2 - x)
admin
num poly_add "[1, 1]" "[2, -1]"
mentdb
[3]
Terms that cancel
admin
num poly_add "[0, 0, 1]" "[0, 0, -1]"
mentdb
[0]

num poly_derive <json_coeffs>


Description

    Get the derivative of a polynomial

Parameters

    json_coeffs:   The coefficients (ascending order) - String - required
admin
num poly_derive "[1, 2, 3]"
mentdb
[2,6]
Derivative of x^3
admin
num poly_derive "[0, 0, 0, 1]"
mentdb
[0,0,3]
Constant: 0
admin
num poly_derive "[5]"
mentdb
[0]
Derivative of 1 + x + x^2 + x^3
admin
num poly_derive "[1, 1, 1, 1]"
mentdb
[1,2,3]

num poly_eval <json_coeffs> <x>


Description

    Evaluate a polynomial at x

Parameters

    json_coeffs:   The coefficients (ascending order) - String - required
    x:   The value - Number - required
admin
num poly_eval "[-2, 0, 1]" 3
mentdb
7
Value of 1 + 2x + 3x^2 at x = 2
admin
num poly_eval "[1, 2, 3]" 2
mentdb
17
1 + x + x^2 at x = 0.5
admin
num poly_eval "[1, 1, 1]" 0.5
mentdb
1.75
Root: x^2 - 4 at x = 2
admin
num poly_eval "[-4, 0, 1]" 2
mentdb
0
Taylor polynomial of exp at x = 1
admin
num poly_eval "[1, 1, 0.5, 0.16666666666666666, 0.041666666666666664]" 1
mentdb
2.708333333333333

num poly_integrate <json_coeffs>


Description

    Get the primitive of a polynomial (constant = 0)

Parameters

    json_coeffs:   The coefficients (ascending order) - String - required
admin
num poly_integrate "[1, 2, 3]"
mentdb
[0,1,1,1]
Primitive of 1
admin
num poly_integrate "[1]"
mentdb
[0,1]
Primitive of 3x^2: x^3
admin
num poly_integrate "[0, 0, 3]"
mentdb
[0,0,0,1]
Primitive of 1 + x
admin
num poly_integrate "[1, 1]"
mentdb
[0,1,0.5]

num poly_mul <json_coeffs1> <json_coeffs2>


Description

    Multiply two polynomials

Parameters

    json_coeffs1:   The coefficients of the first polynomial (ascending order) - String - required
    json_coeffs2:   The coefficients of the second polynomial (ascending order) - String - required
admin
num poly_mul "[1, 1]" "[-1, 1]"
mentdb
[-1,0,1]
(1 + x)^2
admin
num poly_mul "[1, 1]" "[1, 1]"
mentdb
[1,2,1]
(1 + x)^3 in two steps
admin
num poly_mul (num poly_mul "[1, 1]" "[1, 1]") "[1, 1]"
mentdb
[1,3,3,1]
(x - 1)(x - 2)(x - 3)
admin
num poly_mul (num poly_mul "[-1, 1]" "[-2, 1]") "[-3, 1]"
mentdb
[-6,11,-6,1]

num poly_roots <json_coeffs>


Description

    Get all the roots of a polynomial, real and complex (JSON array of strings, format a+bi), sorted by real part then imaginary part. A multiple root is repeated.

Parameters

    json_coeffs:   The coefficients (ascending order) - String - required
admin
num poly_roots "[-2, 0, 1]"
mentdb
["-1.414213562373095","1.414213562373095"]
admin
num poly_roots "[5, 2, 1]"
mentdb
["-1-2i","-1+2i"]
Complex roots of x^2 + 1
admin
num poly_roots "[1, 0, 1]"
mentdb
["-i","i"]
Cubic x^3 - 6x^2 + 11x - 6 = (x-1)(x-2)(x-3)
admin
num poly_roots "[-6, 11, -6, 1]"
mentdb
["1","2","3"]
Double root: (x - 1)^2
admin
num poly_roots "[1, -2, 1]"
mentdb
["1","1"]
Degree 4: x^4 - 1
admin
num poly_roots "[-1, 0, 0, 0, 1]"
mentdb
["-1","-i","i","1"]
Roots of a product built with poly_mul
admin
num poly_roots (num poly_mul "[-1, 1]" "[2, 1]")
mentdb
["-2","1"]

num solve <expression> <variable> <a> <b>


Description

    Find a root of f(x) = 0 in [a, b]. The function must change sign on the interval (Brent method, then Newton polishing).

Parameters

    expression:   The function, ex: x^2-2 - String - required
    variable:   The name of the variable, ex: x - String - required
    a:   The lower bound - Number - required
    b:   The upper bound - Number - required
admin
num solve "x^2-2" "x" 0 2
mentdb
1.414213562373095
admin
num solve "cos(x)-x" "x" 0 1
mentdb
0.7390851332151607
First zero of J0
admin
num solve "bessel_j(0, x)" "x" 2 3
mentdb
2.404825557695773
Root of a polynomial in [1, 2]
admin
num solve "x^3-x-2" "x" 1 2
mentdb
1.5213797068045676
Interest rate: 1000 becomes 1500 in 8 years
admin
num solve "1000*(1+r)^8-1500" "r" 0 1
mentdb
0.05198950550864404
exp(x) = 3: x = ln(3)
admin
num solve "exp(x)-3" "x" 0 5
mentdb
1.0986122886681096
Kepler equation E - 0.5 sin(E) = 1
admin
num solve "E-0.5*sin(E)-1" "E" 0 3
mentdb
1.4987011335178484
No change of sign on the interval: error
admin
num solve "x^2+1" "x" -1 1
mentdb
Sorry, an error appears (no sign change on [-1, 1]: f(-1) = 2, f(1) = 2).

num fft <json_array>


Description

    Get the discrete Fourier transform (JSON array of numbers or complex numbers a+bi). The values are padded with zeros to the next power of 2. No normalization (like numpy.fft.fft).

Parameters

    json_array:   The values (numbers or complex numbers) - String - required
admin
num fft "[1, 2, 3, 4]"
mentdb
["10","-2+2i","-2","-2-2i"]
Spectrum of a constant: everything at frequency 0
admin
num fft "[1, 1, 1, 1]"
mentdb
["4","0","0","0"]
Impulse: flat spectrum
admin
num fft "[1, 0, 0, 0]"
mentdb
["1","1","1","1"]
Alternating signal: everything at the Nyquist frequency
admin
num fft "[1, -1, 1, -1]"
mentdb
["0","0","4","0"]
3 values: padded with zeros to 4
admin
num fft "[1, 2, 3]"
mentdb
["6","-2-2i","2","-2+2i"]
Complex input
admin
num fft "[\"1+i\", \"0\", \"0\", \"0\"]"
mentdb
["1+i","1+i","1+i","1+i"]

num ifft <json_array>


Description

    Get the inverse discrete Fourier transform (1/n normalization, like numpy.fft.ifft)

Parameters

    json_array:   The values (numbers or complex numbers) - String - required
admin
num ifft "[\"10\", \"-2+2i\", \"-2\", \"-2-2i\"]"
mentdb
["1","2","3","4"]
Inverse of the FFT gives the signal back
admin
num ifft (num fft "[1, 2, 3, 4]")
mentdb
["1","2","3","4"]
Flat spectrum: impulse
admin
num ifft "[1, 1, 1, 1]"
mentdb
["1","0","0","0"]

num interpolate <json_xs> <json_ys> <x> <mode>


Description

    Interpolate y at x from points (sorted automatically).
Modes: linear, spline (natural cubic spline), lagrange (polynomial through all the points).
Only lagrange extrapolates outside [min(x), max(x)].

Parameters

    json_xs:   The x values - String - required
    json_ys:   The y values - String - required
    x:   The point - Number - required
    mode:   linear, spline or lagrange - String - required
admin
num interpolate "[0, 1, 2, 3]" "[0, 1, 4, 9]" 1.5 "spline"
mentdb
2.2
admin
num interpolate "[0, 1, 2, 3]" "[0, 1, 4, 9]" 1.5 "linear"
mentdb
2.5
Natural spline at several x
admin
num interpolate "[0, 1, 2, 3, 4, 5]" "[0, 0.8, 0.9, 0.1, -0.8, -1]" "[0.5, 2.5, 4.5]" "natural"
mentdb
[0.448086124401914, 0.5789473684210527, -0.967822966507177]
Monotone data without overshoot (pchip)
admin
num interpolate "[0, 1, 2, 3]" "[0, 0, 1, 1]" "[0.5, 1.5, 2.5]" "pchip"
mentdb
[0, 0.5, 1]
Step function (previous)
admin
num interpolate "[0, 10, 20]" "[1.5, 2.0, 2.4]" 15 "previous"
mentdb
2
Akima
admin
num interpolate "[0, 1, 2, 3, 4, 5]" "[0, 0.8, 0.9, 0.1, -0.8, -1]" 2.5 "akima"
mentdb
0.5210937499999999
Nearest point
admin
num interpolate "[0, 10, 20]" "[1.5, 2.0, 2.4]" 14 "nearest"
mentdb
2
Next point
admin
num interpolate "[0, 10, 20]" "[1.5, 2.0, 2.4]" 5 "next"
mentdb
2
Clamped spline (zero slopes at the ends)
admin
num interpolate "[0, 1, 2, 3]" "[0, 1, 0, 1]" 1.5 "clamped"
mentdb
0.5000000000000001
Lagrange polynomial
admin
num interpolate "[0, 1, 2, 3]" "[1, 2, 0, 2]" 1.5 "lagrange"
mentdb
0.9375
Temperature of water density table at 15 degC (cubic)
admin
num interpolate "[0, 10, 20, 30, 40]" "[999.84, 999.70, 998.21, 995.65, 992.22]" 15 "cubic"
mentdb
999.105
Outside the data: error
admin
num interpolate "[0, 1, 2]" "[0, 1, 4]" 5 "linear"
mentdb
Sorry, an error appears (x = 5 is out of [0, 2] (only 'lagrange' extrapolates)).
Akima: no oscillation near an outlier
admin
num interpolate "[0, 1, 2, 3, 4, 5, 6]" "[0, 0, 0, 1, 0, 0, 0]" "[1.5, 2.5]" "akima"
mentdb
[0, 0.5]
Natural spline (same as spline)
admin
num interpolate "[0, 1, 2, 3]" "[0, 1, 4, 9]" 1.5 "natural"
mentdb
2.2
Worked example: Calibration curve: interpolation (A thermocouple: voltage (mV) measured at a few temperatures (degC). The temperature of a reading of 2.5 mV, and the sensitivity dV/dT.)
admin
#Worked example: Calibration curve: interpolation; #Temperature for 2.5 mV (temperature as a function of the voltage, monotone pchip); num interpolate "[0, 1.019, 2.023, 3.096, 4.096]" "[0, 25, 50, 75, 100]" 2.5 "pchip"; #Sensitivity at 50 degC (derivative of the voltage); num interpolate_derivative "[0, 25, 50, 75, 100]" "[0, 1.019, 2.023, 3.096, 4.096]" 50 "cubic"; #Chart of the calibration; plot interpolate "[0, 25, 50, 75, 100]" "[0, 1.019, 2.023, 3.096, 4.096]" "cubic";
mentdb
The chart is shown in the Plot tab of the output of the editor

num maximize <expression> <variable> <a> <b>


Description

    Find the maximum of a function on [a, b]: scan of 200 points, Brent method around the best one, then Newton polishing on f'(x) = 0.
Returns {x, value}.

Parameters

    expression:   The function, ex: x^2-2 - String - required
    variable:   The name of the variable, ex: x - String - required
    a:   The lower bound - Number - required
    b:   The upper bound - Number - required
admin
num maximize "sin(x)" "x" 0 3
mentdb
{"x": 1.5707963267948966, "value": 1}
Maximum of the power transferred P = R/(R+5)^2
admin
num maximize "R/(R+5)^2" "R" 0 50
mentdb
{"x": 5, "value": 0.05}
Maximum of x exp(-x)
admin
num maximize "x*exp(-x)" "x" 0 5
mentdb
{"x": 1, "value": 0.36787944117144233}
Best angle for the range of a projectile (degrees)
admin
num maximize "sin(2*a*pi/180)" "a" 0 90
mentdb
{"x": 45, "value": 1}
Area of a rectangle of perimeter 20
admin
num maximize "x*(10-x)" "x" 0 10
mentdb
{"x": 5, "value": 25}

num minimize <expression> <variable> <a> <b>


Description

    Find the minimum of a function on [a, b] (global on the scan of 200 points, then Brent and Newton polishing).
Returns {x, value}.

Parameters

    expression:   The function, ex: x^2-2 - String - required
    variable:   The name of the variable, ex: x - String - required
    a:   The lower bound - Number - required
    b:   The upper bound - Number - required
admin
num minimize "x^4-3*x^2+x" "x" -3 3
mentdb
{"x": -1.300839565941577, "value": -3.5139050389347886}
admin
num minimize "x^2-4*x" "x" -10 10
mentdb
{"x": 2, "value": -4}
Minimum of a cost
admin
num minimize "x+100/x" "x" 1 50
mentdb
{"x": 10, "value": 20}
Shortest distance from (3, 1) to the curve y = x^2
admin
num minimize "(x-3)^2+(x^2-1)^2" "x" -3 3
mentdb
{"x": 1.289623901485061, "value": 3.3651275396764504}
Minimum of a function with a special function
admin
num minimize "bessel_j(0, x)" "x" 2 5
mentdb
{"x": 3.8317059702075125, "value": -0.40275939570255287}
Cost of a cylindrical can of 1 L: area with r
admin
num minimize "2*pi*r^2+2*0.001/r" "r" 0.01 0.5
mentdb
{"x": 0.0541926070139289, "value": 0.05535810445932085}

num ode <expressions> <y0> <t0> <t1> <nb_steps>


Description

    Solve a differential equation y' = f(t, y) or a system, from t0 to t1 (Dormand-Prince 8(5,3), adaptive step, tolerance 1e-12).
- One equation: an expression in t and y, y0 a number.
- A system: a JSON array of expressions in t, y1, y2... yn, y0 a JSON array.
Returns [[t, y...], ...] at nb_steps+1 equally spaced points.
A second order equation y'' = g is written as a system: ["y2", "g"] with y1 = y, y2 = y'.

Parameters

    expressions:   An expression (t, y) or a JSON array of expressions (t, y1...yn) - String - required
    y0:   The initial value (number or JSON array) - String - required
    t0:   The initial time - Number - required
    t1:   The final time - Number - required
    nb_steps:   The number of output intervals (1 to 10000) - Number - required
admin
num ode "-2*t*y" 1 0 2 2
mentdb
[ [0,1], [1,0.36787944117177024], [2,0.018315638888744514] ]
admin
num ode "[\"y2\", \"-y1\"]" "[1, 0]" 0 3.141592653589793 2
mentdb
[ [0,1,0], [1.5707963267948966,2.2160051571518125E-13,-0.9999999999999845], [3.141592653589793,-0.9999999999999685,-4.435063427621344E-13] ]
Old form: one equation y' = -2 t y
admin
num ode "-2*t*y" 1 0 1 2
mentdb
[ [0,1], [0.5,0.7788007830713138], [1,0.36787944117215393] ]
Damped oscillator, 5 times
admin
num ode "{\"x\": \"v\", \"v\": \"-0.5*v - 4*x\"}" "{\"x\": 1, \"v\": 0}" 0 4 "{\"points\": 5}"
mentdb
{"t": [0, 1, 2, 3, 4], "x": [1, -0.2230979954748353, -0.4668947505330462, 0.4275428418908705, 0.015589074523268139], "v": [0, -1.437591689066472, 0.8997813813429967, 0.30877481101850157, -0.7390055903790133], "method": "dp853"}
Free fall with air resistance until the ground (event)
admin
num ode "{\"z\": \"v\", \"v\": \"-_g0 + 0.02*v^2\"}" "{\"z\": 100, \"v\": 0}" 0 60 "{\"points\": 2, \"event\": \"z\", \"direction\": -1}"
mentdb
{"t": [0, 6.070725570327769], "z": [100, -4.0101255649460654E-13], "v": [0, -21.939729876648734], "method": "dp853", "event": {"t": 6.070725570327769, "z": -4.0101255649460654E-13, "v": -21.939729876648734}}
Stiff chemical kinetics (Robertson): implicit method
admin
num ode "{\"a\": \"-0.04*a + 1e4*b*c\", \"b\": \"0.04*a - 1e4*b*c - 3e7*b^2\", \"c\": \"3e7*b^2\"}" "{\"a\": 1, \"b\": 0, \"c\": 0}" 0 1000 "{\"points\": 2, \"method\": \"stiff\"}"
mentdb
{"t": [0, 1000], "a": [1, 0.33687453057880784], "b": [0, 2.0137023175350857E-6], "c": [0, 0.6631234557188748], "method": "radau"}
Lotka-Volterra predator-prey, 3 times
admin
num ode "{\"prey\": \"1.1*prey - 0.4*prey*pred\", \"pred\": \"0.1*prey*pred - 0.4*pred\"}" "{\"prey\": 10, \"pred\": 10}" 0 20 "{\"points\": 3}"
mentdb
{"t": [0, 10, 20], "prey": [10, 1.1226461550490554, 0.03211427990043338], "pred": [10, 0.28927267091214304, 1.4605400298178421], "method": "dp853"}
Old form: exponential decay y' = -0.5 y
admin
num ode "-0.5*y" 1 0 4 4
mentdb
[ [0,1], [1,0.6065306597126627], [2,0.36787944117147786], [3,0.22313016014846418], [4,0.1353352832366446] ]
Logistic growth with the new form, 5 times
admin
num ode "{\"N\": \"0.8*N*(1-N/100)\"}" "{\"N\": 5}" 0 10 "{\"points\": 5}"
mentdb
{"t": [0, 2.5, 5, 7.5, 10], "N": [5, 28.000456216554113, 74.18413371616752, 95.50220053833608, 99.36665779828142], "method": "dp853"}
Precision options: rtol and atol
admin
num ode "{\"y\": \"-y\"}" "{\"y\": 1}" 0 10 "{\"points\": 2, \"rtol\": 1e-12, \"atol\": 1e-14}"
mentdb
{"t": [0, 10], "y": [1, 4.5399929762877376E-5], "method": "dp853"}
SIR epidemic model
admin
num ode "{\"S\": \"-0.3*S*I\", \"I\": \"0.3*S*I - 0.1*I\", \"R\": \"0.1*I\"}" "{\"S\": 0.99, \"I\": 0.01, \"R\": 0}" 0 100 "{\"points\": 5}"
mentdb
{"t": [0, 25, 50, 75, 100], "S": [0.99, 0.3865727937509217, 0.08214399850417996, 0.06145026033183859, 0.059130728721926235], "I": [0.01, 0.2999656222655743, 0.08811228261202575, 0.012057449453134163, 0.001551201508317014], "R": [0, 0.3134615839835039, 0.8297437188837942, 0.9264922902150271, 0.9393180697697566], "method": "dp853"}
Pendulum with large amplitude (non-linear), 4 times
admin
num ode "{\"th\": \"w\", \"w\": \"-9.81/1*sin(th)\"}" "{\"th\": 2.5, \"w\": 0}" 0 3 "{\"points\": 4}"
mentdb
{"t": [0, 1, 2, 3], "th": [2.5, -0.9967697935813276, -2.107636914460269, 2.2284790442266535], "w": [0, -5.135410713018086, 2.3841785167280864, 1.93003482580656], "method": "dp853"}
Event when x crosses 0 going down
admin
num ode "{\"x\": \"v\", \"v\": \"-x\"}" "{\"x\": 1, \"v\": 0}" 0 10 "{\"points\": 2, \"event\": \"x\", \"direction\": -1}"
mentdb
{"t": [0, 1.5707963268128147], "x": [1, -1.1102230246251565E-16], "v": [0, -1.0000000000690774], "method": "dp853", "event": {"t": 1.5707963268128147, "x": -1.1102230246251565E-16, "v": -1.0000000000690774}}
Unknown method: error
admin
num ode "{\"y\": \"-y\"}" "{\"y\": 1}" 0 1 "{\"method\": \"euler\"}"
mentdb
Sorry, an error appears (unknown method 'euler' (auto, dp853, radau or stiff)).
Worked example: Differential equations: a projectile with air resistance (A ball thrown at 30 m/s with an angle of 45 degrees; the air resistance is proportional to v^2 (k = 0.02 per m). num ode integrates the motion until the ball hits the ground (event y = 0, decreasing).)
admin
#Worked example: Differential equations: a projectile with air resistance; #Initial speed components; math eval "30*cos(45)" "{\"_angle\": \"deg\"}"; #Motion until the impact; num ode "{\"x\": \"vx\", \"y\": \"vy\", \"vx\": \"-0.02*sqrt(vx^2+vy^2)*vx\", \"vy\": \"-_g0-0.02*sqrt(vx^2+vy^2)*vy\"}" "{\"x\": 0, \"y\": 0, \"vx\": 21.213203435596427, \"vy\": 21.213203435596427}" 0 10 "{\"points\": 2, \"event\": \"y\", \"direction\": -1}"; #Without air: range v^2 sin(2a) / g; math eval "30^2*sin(90)/9.80665" "{\"_angle\": \"deg\"}"; #Trajectory y(x); plot ode (num ode "{\"x\": \"vx\", \"y\": \"vy\", \"vx\": \"-0.02*sqrt(vx^2+vy^2)*vx\", \"vy\": \"-_g0-0.02*sqrt(vx^2+vy^2)*vy\"}" "{\"x\": 0, \"y\": 0, \"vx\": 21.213203435596427, \"vy\": 21.213203435596427}" 0 10 "{\"points\": 300, \"event\": \"y\", \"direction\": -1}") "x" "y";
mentdb
The chart is shown in the Plot tab of the output of the editor
Worked example: Chemical kinetics: a stiff system (A -> B -> C with very different rates (k1 = 1 per s, k2 = 1000 per s): the system is stiff. The automatic method detects it and uses the implicit Radau IIA method.)
admin
#Worked example: Chemical kinetics: a stiff system; #Concentrations at t = 1, 2, 5 s; num ode "{\"A\": \"-1*A\", \"B\": \"1*A - 1000*B\", \"C\": \"1000*B\"}" "{\"A\": 1, \"B\": 0, \"C\": 0}" 0 5 "{\"points\": 6, \"atol\": 1e-14}"; #Exact value of A(5) = exp(-5); math eval "exp(-5)";
mentdb
0.006737946999085467
Worked example: Heat: how long does a coffee take to cool down? (Newton law of cooling dT/dt = -k (T - Ta), with Ta = 20 degC and T(0) = 90 degC. k is fitted from 4 measures, then the time to reach 60 degC is found with an event of num ode and checked with the exact solution.)
admin
#Worked example: Heat: how long does a coffee take to cool down?; #Measures every 2 minutes; -> "[tm]" "[0, 2, 4, 6, 8]"; -> "[Tm]" "[90, 77.3, 66.9, 58.5, 51.6]"; #Fit of T(t) = 20 + (T0 - 20) exp(-k t); stat fit_model "20+(T0-20)*exp(-k*t)" "t" [tm] [Tm] "{\"T0\": 90, \"k\": 0.1}"; #Time to reach 60 degC with k = 0.1 (from the fit): event T - 60 = 0; num ode "{\"T\": \"-0.1*(T-20)\"}" "{\"T\": 90}" 0 60 "{\"points\": 2, \"event\": \"T-60\"}"; #Check with the exact solution t = ln((T0 - Ta)/(T - Ta)) / k; math calc "ln(70/40)/0.1"; #The same time with num solve on the exact solution; num solve "20+70*exp(-0.1*t)-60" "t" 0 60;
mentdb
5.5961578793542275

num uncertainty <expression> <json_vars>


Description

    Propagate the uncertainties through a formula (first order, independent variables): u(f) = sqrt(sum((df/dxi*u(xi))^2)), with exact derivatives.
- variables (JSON object): [value, uncertainty] for a measured value, a number alone for an exact value
- option "_angle": "deg" or "grad" for the trigonometric functions
Returns {value, uncertainty, relative, text, derivatives, variance_share}: 'text' is rounded as in 'math round_uncertainty', 'variance_share' gives the part of each variable in the final uncertainty (sum = 1).

Parameters

    expression:   The formula, ex: m*g*h - String - required
    json_vars:   The variables: {"x": [value, uncertainty], "c": 3} - String - required
admin
num uncertainty "m*g*h" "{\"m\": [1.250, 0.005], \"g\": 9.81, \"h\": [2.40, 0.03]}"
mentdb
{"value": 29.43, "uncertainty": 0.38625123174560877, "relative": 0.013124404748406687, "text": "29.4 ± 0.4", "derivatives": {"m": 23.544, "h": 12.262500000000001}, "variance_share": {"m": 0.09288824383164009, "h": 0.90711175616836}}
admin
num uncertainty "sin(t)" "{\"t\": [30, 0.5], \"_angle\": \"deg\"}"
mentdb
{"value": 0.49999999999999994, "uncertainty": 0.007557497350975908, "relative": 0.015114994701951818, "text": "0.500 ± 0.008", "derivatives": {"t": 0.015114994701951816}, "variance_share": {"t": 1}}
Density of a cylinder rho = m / (pi r^2 h)
admin
num uncertainty "m/(pi*r^2*h)" "{\"m\": [0.2345, 0.0002], \"r\": [0.0125, 5e-05], \"h\": [0.05, 0.0001]}"
mentdb
{"value": 9554.38954369266, "uncertainty": 79.20779191777947, "relative": 0.008290199133660883, "text": "9550 ± 80", "derivatives": {"m": 40743.6654315252, "r": -1528702.3269908256, "h": -191087.7908738532}, "variance_share": {"m": 0.010583866958675556, "r": 0.9312151840388936, "h": 0.05820094900243085}}
Period of a pendulum T = 2 pi sqrt(L/g)
admin
num uncertainty "2*pi*sqrt(L/g)" "{\"L\": [0.75, 0.002], \"g\": 9.81}"
mentdb
{"value": 1.7373047071809469, "uncertainty": 0.0023164062762412625, "relative": 0.0013333333333333333, "text": "1.7373 ± 0.0023", "derivatives": {"L": 1.1582031381206312}, "variance_share": {"L": 1}}
Density of a cube: m / a^3
admin
num uncertainty "m/a^3" "{\"m\": [0.5432, 0.0005], \"a\": [0.04, 0.0002]}"
mentdb
{"value": 8487.5, "uncertainty": 127.5519808254658, "relative": 0.01502821570844958, "text": "8490 ± 130", "derivatives": {"m": 15624.999999999998, "a": -636562.4999999999}, "variance_share": {"m": 0.0037515060045704746, "a": 0.9962484939954295}}
Resistance R = U / I
admin
num uncertainty "U/I" "{\"U\": [12.0, 0.1], \"I\": [0.50, 0.01]}"
mentdb
{"value": 24, "uncertainty": 0.52, "relative": 0.021666666666666667, "text": "24.0 ± 0.5", "derivatives": {"U": 2, "I": -48}, "variance_share": {"U": 0.14792899408284027, "I": 0.8520710059171598}}
With a constant: kinetic energy
admin
num uncertainty "m*v^2/2" "{\"m\": [2.00, 0.01], \"v\": [3.0, 0.1]}"
mentdb
{"value": 9, "uncertainty": 0.6016851336039475, "relative": 0.06685390373377194, "text": "9.0 ± 0.6", "derivatives": {"m": 4.5, "v": 6}, "variance_share": {"m": 0.005593536357986325, "v": 0.9944064636420137}}
Worked example: Propagation of uncertainties: g from a pendulum (g = 4 pi^2 L / T^2, with L = 0.750 +- 0.002 m and T = 1.7413 +- 0.0013 s. num uncertainty computes the value, the combined uncertainty (exact derivatives) and the share of each variable.)
admin
#Worked example: Propagation of uncertainties: g from a pendulum; #g and its uncertainty; num uncertainty "4*pi^2*L/T^2" "{\"L\": [0.75, 0.002], \"T\": [1.7413, 0.0013]}"; #The same formula with units; unit calc "4*pi^2*0.750 m/(1.7413 s)^2";
mentdb
9.76503490634258 m/s^2

num solve_system <json_equations> <json_init>


Description

    Solve a system of non-linear equations from initial values (Levenberg-Marquardt with the exact Jacobian, then Newton steps), like fsolve in Python or MATLAB.
- the equations: a JSON array of expressions, "f = g" or "f" (= 0)
- the unknowns are the keys of the initial values; with more equations than unknowns, the least squares solution is used if it satisfies all the equations
- like every non-linear solver, it finds the solution near the initial values: a system can have several solutions
Returns {solution, residuals, iterations}.

Parameters

    json_equations:   The equations (JSON array of expressions) - String - required
    json_init:   The unknowns and their initial values (JSON object) - String - required
admin
num solve_system "[\"x^2+y^2=4\", \"x*y=1\"]" "{\"x\": 2, \"y\": 0.5}"
mentdb
{"solution": {"x": 1.9318516525781366, "y": 0.5176380902050415}, "residuals": [0, -1.1102230246251565E-16], "iterations": 5}
Intersection of a circle and a line
admin
num solve_system "[\"x^2+y^2=25\", \"y=x+1\"]" "{\"x\": 2, \"y\": 3}"
mentdb
{"solution": {"x": 3, "y": 4}, "residuals": [0, 0], "iterations": 6}
Chemical equilibrium (3 unknowns)
admin
num solve_system "[\"x*y/z = 0.5\", \"x + z = 1\", \"y + z = 2\"]" "{\"x\": 0.5, \"y\": 1.5, \"z\": 0.5}"
mentdb
{"solution": {"x": 0.28077640640441515, "y": 1.2807764064044151, "z": 0.7192235935955849}, "residuals": [0, 0, 0], "iterations": 7}
Linear system of 2 equations
admin
num solve_system "[\"2*x+y=5\", \"x-y=1\"]" "{\"x\": 0, \"y\": 0}"
mentdb
{"solution": {"x": 2, "y": 1}, "residuals": [0, 0], "iterations": 2}
Intersection of a circle and a line: other initial values give the other solution
admin
num solve_system "[\"x^2+y^2=25\", \"y=x+1\"]" "{\"x\": -5, \"y\": -4}"
mentdb
{"solution": {"x": -4, "y": -3}, "residuals": [0, 0], "iterations": 6}
Equilibrium of a chemical reaction
admin
num solve_system "[\"x^2/((1-x)*(2-x))=4\"]" "{\"x\": 0.5}"
mentdb
{"solution": {"x": 0.8452994616207484}, "residuals": [-1.3322676295501878E-15], "iterations": 8}

num integrate2 <expression> <var_x> <a> <b> <var_y> <c> <d>


Description

    Double integral of f(x, y) for x from a to b and y from c to d (adaptive Gauss-Kronrod in both directions), like dblquad in Python.
- c and d can be expressions of x: a triangle (0 to x), a disk (-sqrt(1-x^2) to sqrt(1-x^2))...
- a and b: numbers or constant expressions (pi, 2*pi...)

Parameters

    expression:   The function f(x, y) - String - required
    var_x:   The outer variable, ex: x - String - required
    a:   The first bound of x - Number - required
    b:   The last bound of x - Number - required
    var_y:   The inner variable, ex: y - String - required
    c:   The first bound of y (number or expression of x) - String - required
    d:   The last bound of y (number or expression of x) - String - required
admin
num integrate2 "x*y" "x" 0 1 "y" 0 "x"
mentdb
0.125
admin
num integrate2 "1" "x" -1 1 "y" "-sqrt(1-x^2)" "sqrt(1-x^2)"
mentdb
3.141592653589928
Volume under a Gaussian
admin
num integrate2 "exp(-(x^2+y^2))" "x" -5 5 "y" -5 5
mentdb
3.141592653580133
Volume under the plane z = x + y on [0, 1] x [0, 1]
admin
num integrate2 "x+y" "x" 0 1 "y" 0 1
mentdb
1
Triangle 0 <= y <= x <= 1: area 1/2
admin
num integrate2 "1" "x" 0 1 "y" 0 "x"
mentdb
0.5
Mass of a plate of density x^2 + y^2 on the unit disc
admin
num integrate2 "x^2+y^2" "x" -1 1 "y" "-sqrt(1-x^2)" "sqrt(1-x^2)"
mentdb
1.5707963267949439

num sum <expression> <variable> <a> <b>


Description

    Get the sum of f(k) for k = a to b (integers, at most 10000000 terms, Kahan summation)

Parameters

    expression:   The function, ex: x^2-2 - String - required
    variable:   The name of the variable, ex: x - String - required
    a:   The first integer - Number - required
    b:   The last integer - Number - required
admin
num sum "k^2" "k" 1 100
mentdb
338350
Sum of 1/k^2 (-> pi^2/6)
admin
num sum "1/k^2" "k" 1 100000
mentdb
1.6449240668982263
Geometric series
admin
num sum "0.5^k" "k" 0 50
mentdb
1.9999999999999991
Harmonic numbers: H_10
admin
num sum "1/k" "k" 1 10
mentdb
2.9289682539682538
Leibniz series for pi/4
admin
num sum "(-1)^k/(2*k+1)" "k" 0 1000
mentdb
0.7856479135848858
Number e: sum of 1/k!
admin
num sum "1/gamma(k+1)" "k" 0 20
mentdb
2.718281828459045
Total of a loan: 12 payments with interest
admin
num sum "100*1.01^k" "k" 1 12
mentdb
1280.9328043328942

num table <expression> <variable> <a> <b> <step>


Description

    Get a table of values [[x, f(x)], ...] for x = a, a+step, ... <= b (at most 100000 rows)

Parameters

    expression:   The function, ex: x^2-2 - String - required
    variable:   The name of the variable, ex: x - String - required
    a:   The first x - Number - required
    b:   The last x - Number - required
    step:   The step (> 0) - Number - required
admin
num table "x^2" "x" 0 1 0.25
mentdb
[ [0,0], [0.25,0.0625], [0.5,0.25], [0.75,0.5625], [1,1] ]
Table of a damped oscillation
admin
num table "exp(-t)*cos(2*pi*t)" "t" 0 1 0.25
mentdb
[ [0,1], [0.25,4.7687794308092406E-17], [0.5,-0.6065306597126334], [0.75,-8.677232802578295E-17], [1,0.36787944117144233] ]
Table of a damped oscillation
admin
num table "exp(-x)*sin(5*x)" "x" 0 1 0.2
mentdb
[ [0,0], [0.2,0.6889381730850401], [0.4,0.6095202930098793], [0.6,0.07744830250893793], [0.8,-0.3400532812580398], [1,-0.35276852628880606] ]
Table of a polynomial
admin
num table "2*x^2-1" "x" 0 2 0.5
mentdb
[ [0,-1], [0.5,-0.5], [1,1], [1.5,3.5], [2,7] ]
Step larger than the interval: one row
admin
num table "x" "x" 0 1 5
mentdb
[ [0,0] ]

num ode <equations> <initial> <t0> <t1>


Description

    Solve a system of ordinary differential equations with named variables (as solve_ivp in Python): a JSON object gives the derivative of each variable, t is the time.
- the expressions use the syntax of 'math eval'; [k] session variables, _g0 constants (CODATA)
- result: {"t": [...], "x": [...], "v": [...], "method": ...} at 101 equally spaced times (draw it with 'plot ode')
- method auto: Dormand-Prince 8(5,3) (rtol 1e-10, atol 1e-12), or Radau IIA order 5 (implicit) when the problem is stiff
- the old form (JSON array of expressions in t, y1, y2...) is still available

Parameters

    equations:   JSON object: the derivative of each variable, ex: {"x": "v", "v": "-[k]*x"} - String - required
    initial:   JSON object: the value of each variable at t0 - String - required
    t0:   The first time - Number - required
    t1:   The last time (can be < t0) - Number - required
admin
num ode "{\"x\": \"v\", \"v\": \"-x\"}" "{\"x\": 1, \"v\": 0}" 0 3
mentdb
{"t": [0, 0.03, 0.06, ...], "x": [1, 0.99955003374898, ...], "v": [0, -0.0299955002024957, ...], "method": "dp853"}
Logistic growth with session variables
admin
-> "[r]" 0.8; -> "[K]" 100; num ode "{\"N\": \"[r]*N*(1-N/[K])\"}" "{\"N\": 5}" 0 10
mentdb
{"t": [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 8, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10], "N": [5, 5.3939729289562495, 5.817087955073017, 6.2711929634649515, 6.758203512598109, 7.280096915005272, 7.838904588067428, 8.436702474443273, 9.075599333010905, 9.757722708988846, 10.485202407751006, 11.260151322340846, 12.084643501424514, 12.96068939390496, 13.890208269926376, 14.874997896424489, 15.916701639047226, 17.01677327073286, 18.17643988905443, 19.39666347699928, 20.678101781178533, 22.021069322125168, 23.425499486454374, 24.890908772001737, 26.416364355347305, 28.000456216507406, 29.641275078210246, 31.336397388096973, 33.08287848420617, 34.877254932733905, 36.715556811467664, 38.593330435974515, 40.505671696860745, 42.447269808286386, 44.412460877822774, 46.39529031646795, 48.38958273791966, 50.38901767096841, 52.38720914926952, 54.377787066267786, 56.35447810184573, 58.31118404701775, 60.24205547231254, 62.14155889607686, 64.0045358963913, 65.8262529554831, 67.60244120637965, 69.32932564499137, 71.00364375467603, 72.62265384500091, 74.18413371607369, 75.68637051322482, 77.1281428275946, 78.50869622456885, 79.82771344637038, 81.08528054309784, 82.28185014611036, 83.41820301829023, 84.4954089073039, 85.5147876002294, 86.47787093973542, 87.38636642111535, 88.24212285222148, 89.0470984295677, 89.80333146700238, 90.51291391053361, 91.17796768514435, 91.80062384688615, 92.38300445562054, 92.92720703942155, 93.43529148947894, 93.90926920279443, 94.3510942774327, 94.76265655999778, 95.14577634587341, 95.50220053824842, 95.83360008086042, 96.14156849071354, 96.42762132990622, 96.69319646944474, 96.93965501196215, 97.16828275418037, 97.38029208342397, 97.57682421528811, 97.75895169152622, 97.92768106826186, 98.08395573469954, 98.2286588116031, 98.36261608694728, 98.48659895336917, 98.60132731840254, 98.70747246403447, 98.80565983694295, 98.89647175492729, 98.98045001859444, 99.05809842038074, 99.12988514552947, 99.19624506176793, 99.25758189618651, 99.3142702992627, 99.36665779714015], "method": "dp853"}
Free fall from 100 m (named variables)
admin
num ode "{\"z\": \"v\", \"v\": \"-_g0\"}" "{\"z\": 100, \"v\": 0}" 0 2
mentdb
{"t": [0, 0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.76, 0.78, 0.8, 0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.96, 0.98, 1, 1.02, 1.04, 1.06, 1.08, 1.1, 1.12, 1.14, 1.16, 1.18, 1.2, 1.22, 1.24, 1.26, 1.28, 1.3, 1.32, 1.34, 1.36, 1.38, 1.4, 1.42, 1.44, 1.46, 1.48, 1.5, 1.52, 1.54, 1.56, 1.58, 1.6, 1.62, 1.64, 1.66, 1.68, 1.7, 1.72, 1.74, 1.76, 1.78, 1.8, 1.82, 1.84, 1.86, 1.88, 1.9, 1.92, 1.94, 1.96, 1.98, 2], "z": [100, 99.99803867, 99.99215468, 99.98234803, 99.96861872, 99.95096674999999, 99.92939211999999, 99.90389482999998, 99.87447487999998, 99.84113226999997, 99.80386699999997, 99.76267906999996, 99.71756847999997, 99.66853522999997, 99.61557931999998, 99.55870074999999, 99.49789951999999, 99.43317563, 99.36452908, 99.29195987, 99.215468, 99.13505347, 99.05071628, 98.96245643, 98.87027392, 98.77416875, 98.67414092, 98.57019043, 98.46231728, 98.35052146999999, 98.23480299999999, 98.11516186999998, 97.99159807999997, 97.86411162999997, 97.73270251999996, 97.59737074999997, 97.45811631999997, 97.31493922999998, 97.16783947999998, 97.01681706999999, 96.86187199999999, 96.70300427, 96.54021388, 96.37350083, 96.20286512, 96.02830675, 95.84982572, 95.66742203, 95.48109568, 95.29084667, 95.09667499999999, 94.89858066999999, 94.69656367999998, 94.49062402999998, 94.28076171999997, 94.06697674999997, 93.84926911999996, 93.62763882999997, 93.40208587999997, 93.17261026999998, 92.93921199999998, 92.70189106999999, 92.46064747999999, 92.21548123, 91.96639232, 91.71338075, 91.45644652, 91.19558963, 90.93081008, 90.66210787, 90.389483, 90.11293547, 89.83246528, 89.54807242999999, 89.25975691999999, 88.96751874999998, 88.67135791999998, 88.37127442999997, 88.06726827999996, 87.75933946999996, 87.44748799999996, 87.13171386999997, 86.81201707999998, 86.48839762999998, 86.16085551999998, 85.82939074999999, 85.49400331999999, 85.15469322999999, 84.81146048, 84.46430507, 84.113227, 83.75822627, 83.39930288, 83.03645682999999, 82.66968811999999, 82.29899674999999, 81.92438271999998, 81.54584602999998, 81.16338667999997, 80.77700466999997, 80.38669999999996], "v": [0, -0.19613299999999997, -0.3922659999999999, -0.5883989999999998, -0.7845319999999998, -0.9806649999999998, -1.1767979999999996, -1.3729309999999997, -1.5690639999999996, -1.7651969999999995, -1.9613299999999996, -2.1574629999999995, -2.353595999999999, -2.5497289999999992, -2.7458619999999994, -2.941994999999999, -3.138127999999999, -3.3342609999999993, -3.530393999999999, -3.726526999999999, -3.922659999999999, -4.118792999999998, -4.314925999999999, -4.5110589999999995, -4.707191999999999, -4.903325, -5.099458, -5.295591000000001, -5.491724000000001, -5.687857, -5.883990000000001, -6.080123000000001, -6.276256000000002, -6.472389000000002, -6.668522000000003, -6.864655000000002, -7.060788000000002, -7.256921000000003, -7.453054000000003, -7.649187000000004, -7.8453200000000045, -8.041453000000004, -8.237586000000004, -8.433719000000004, -8.629852000000003, -8.825985000000003, -9.022118000000003, -9.218251000000002, -9.414384000000002, -9.610517000000002, -9.806650000000001, -10.002783, -10.198916, -10.395049, -10.591182, -10.787315, -10.983448, -11.179580999999997, -11.375713999999997, -11.571846999999996, -11.767979999999996, -11.964112999999996, -12.160245999999995, -12.356378999999995, -12.552511999999995, -12.748644999999994, -12.944777999999994, -13.140910999999994, -13.337043999999993, -13.533176999999991, -13.729309999999991, -13.92544299999999, -14.12157599999999, -14.31770899999999, -14.51384199999999, -14.70997499999999, -14.906107999999989, -15.102240999999989, -15.298373999999988, -15.494506999999988, -15.690639999999988, -15.886772999999987, -16.082905999999987, -16.279038999999987, -16.475171999999986, -16.671304999999986, -16.867437999999986, -17.063570999999985, -17.259703999999985, -17.455836999999985, -17.651969999999984, -17.848102999999984, -18.044235999999984, -18.240368999999983, -18.436501999999983, -18.632634999999983, -18.828767999999982, -19.024900999999982, -19.22103399999998, -19.41716699999998, -19.61329999999998], "method": "dp853"}
RC circuit: charge of a capacitor
admin
num ode "{\"U\": \"(10-U)/(1000*0.001)\"}" "{\"U\": 0}" 0 5
mentdb
{"t": [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2, 2.05, 2.1, 2.15, 2.2, 2.25, 2.3, 2.35, 2.4, 2.45, 2.5, 2.55, 2.6, 2.65, 2.7, 2.75, 2.8, 2.85, 2.9, 2.95, 3, 3.05, 3.1, 3.15, 3.2, 3.25, 3.3, 3.35, 3.4, 3.45, 3.5, 3.55, 3.6, 3.65, 3.7, 3.75, 3.8, 3.85, 3.9, 3.95, 4, 4.05, 4.1, 4.15, 4.2, 4.25, 4.3, 4.35, 4.4, 4.45, 4.5, 4.55, 4.6, 4.65, 4.7, 4.75, 4.8, 4.85, 4.9, 4.95, 5], "U": [0, 0.4877057549928603, 0.9516258196404047, 1.3929202357494226, 1.8126924692201825, 2.2119921692859523, 2.5918177931828223, 2.9531191028128667, 3.2967995396436085, 3.6237184837822687, 3.9346934028736675, 4.230501896195135, 4.511883639059738, 4.779542232389842, 5.034146962085907, 5.276334472589855, 5.506710358827786, 5.725850680512735, 5.934303402594011, 6.13258976545499, 6.321205588285579, 6.5006225088884495, 6.671289163019208, 6.83363230620947, 6.988057880877982, 7.134952031398101, 7.2746820696598755, 7.407597393541087, 7.5340303605839365, 7.654297119062025, 7.768698398515703, 7.87752026173257, 7.981034820053447, 8.07950091379246, 8.173164759472654, 8.262260565495549, 8.347011117784135, 8.427628336863725, 8.50431380777365, 8.577259284134866, 8.646647167633875, 8.71265096412196, 8.775435717470183, 8.835158422265032, 8.891968416376661, 8.946007754381355, 8.997411562771962, 9.046308377844504, 9.092820467105874, 9.137064135006293, 9.17915001376101, 9.219183339988467, 9.25726421785666, 9.293487869395701, 9.3279448726025, 9.360721387932921, 9.391899373747819, 9.421556791251614, 9.449767799435927, 9.476602940515676, 9.50212931632136, 9.526410756088591, 9.549507976064422, 9.571478731329599, 9.59237796021634, 9.612257921682781, 9.6311683259876, 9.649156458991552, 9.666267300396742, 9.682543636219323, 9.698026165776819, 9.712753603457609, 9.726762775527078, 9.740088712212449, 9.752764735296608, 9.76482254143991, 9.776292281438344, 9.787202635616229, 9.797580885541956, 9.807452982246131, 9.816843611112658, 9.825776253605063, 9.834273245982386, 9.842355835151453, 9.850044231795222, 9.857357660910006, 9.86431440987799, 9.8709318741952, 9.877226600969314, 9.883214330296044, 9.888910034617576, 9.894327956161472, 9.899481642553663, 9.904383980694565, 9.909047228983042, 9.913483047968793, 9.9177025295098, 9.921716224507742, 9.925534169290756, 9.929165910709479, 9.932620530009144], "method": "dp853"}
Radioactive chain A -> B -> C
admin
num ode "{\"A\": \"-0.5*A\", \"B\": \"0.5*A-0.1*B\", \"C\": \"0.1*B\"}" "{\"A\": 1, \"B\": 0, \"C\": 0}" 0 10
mentdb
{"t": [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 8, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10], "A": [1, 0.9512294245007139, 0.9048374180359595, 0.8607079764250578, 0.8187307530779818, 0.7788007830714048, 0.7408182206817178, 0.7046880897187134, 0.6703200460356392, 0.6376281516217732, 0.6065306597126334, 0.5769498103804868, 0.5488116360940265, 0.522045776761016, 0.4965853037914095, 0.4723665527410147, 0.44932896411722156, 0.42741493194872665, 0.40656965974059905, 0.3867410234545012, 0.3678794411714423, 0.3499377491111553, 0.33287108369807955, 0.31663676937905333, 0.3011942119122022, 0.2865047968601902, 0.27253179303401265, 0.2592402606458915, 0.24659696394160655, 0.23457028809379768, 0.22313016014842987, 0.2122479738267431, 0.20189651799465547, 0.1920499086207542, 0.1826835240527347, 0.17377394345044514, 0.16529888822158653, 0.1572371663136276, 0.1495686192226351, 0.1422740715865136, 0.1353352832366127, 0.1287349035878043, 0.12245642825298193, 0.11648415777349702, 0.11080315836233391, 0.10539922456186439, 0.1002588437228038, 0.09536916221554967, 0.09071795328941257, 0.08629358649937054, 0.08208499862389884, 0.07808166600115321, 0.07427357821433389, 0.0706512130604296, 0.06720551273974976, 0.06392786120670757, 0.06081006262521798, 0.05784432087483847, 0.055023220056407245, 0.052339705948432395, 0.04978706836786396, 0.047358924391140936, 0.045049202393557815, 0.04285212686704021, 0.040762203978366225, 0.03877420783172203, 0.036883167401240036, 0.035084354100845046, 0.03337326996032611, 0.03174563637806796, 0.030197383422318525, 0.028724639654239457, 0.02732372244729258, 0.025991128778755365, 0.024723526470339405, 0.02351774585600912, 0.022370771856165608, 0.021279736438377175, 0.020241911445804398, 0.01925470177538693, 0.018315638888734186, 0.01742237463949352, 0.01657267540176126, 0.015764416484854493, 0.014995576820477717, 0.014264233908999264, 0.013568559012200948, 0.012906812580479892, 0.012277339903068452, 0.01167856697039546, 0.011108996538242327, 0.010567204383852676, 0.010051835744633602, 0.009561601930543525, 0.009095277101695838, 0.008651695203120655, 0.00822974704902005, 0.007828377549225794, 0.007446583070924356, 0.007083408929052139, 0.006737946999085489], "B": [0, 0.04852551156056758, 0.0942015690884947, 0.137171946404313, 0.17757335759292672, 0.21553580178663642, 0.25118289112816355, 0.28463216273404346, 0.3159953754387456, 0.34537879206181865, 0.3728834479041577, 0.398605406145052, 0.4226360007789139, 0.44506206769943163, 0.4659661645092454, 0.4854267796050539, 0.5035185310612372, 0.5203123558095712, 0.5358756895883411, 0.5502726381110763, 0.5635641398831743, 0.5758081210737895, 0.5870596428304985, 0.5973710414053508, 0.606792061442939, 0.6153699827640182, 0.6231497409619419, 0.6301740421137019, 0.6364834718926485, 0.6421165993559592, 0.6471100756666097, 0.6514987279969324, 0.655315648848794, 0.6585922810139647, 0.6613584983873435, 0.663642682835335, 0.6654717973118053, 0.6668714554046584, 0.6678659874871506, 0.6684785036395636, 0.668730953498783, 0.6686441831856437, 0.6682379894525933, 0.6675311711872742, 0.666541578401009, 0.665286158824886, 0.6637810022301525, 0.6620413825839386, 0.6600817981459101, 0.6579160096063066, 0.655557076360918, 0.6530173910138907, 0.6503087121948252, 0.6474421957724067, 0.6444284245427996, 0.6412774364672236, 0.6379987515294959, 0.6346013972808731, 0.6310939331362433, 0.6274844734825931, 0.6237807096577029, 0.6199899308541984, 0.6161190440013956, 0.612174592674821, 0.6081627750808527, 0.6040894611616173, 0.5999602088630739, 0.5957802796071217, 0.5915546530065793, 0.5872880408599843, 0.5829849004613636, 0.578649447258407, 0.5742856668908486, 0.5698973266393086, 0.5654879863133685, 0.5610610086062567, 0.5566195689421792, 0.5521666648410632, 0.5477051248242736, 0.5432376168837107, 0.5387666565356088, 0.5342946144793091, 0.5298237238802972, 0.5253560872958509, 0.5208936832607521, 0.516438372549659, 0.5119919041319346, 0.5075559208339487, 0.5031319647231409, 0.4987214822274374, 0.49432582900294586, 0.489946274562229, 0.4855840066748506, 0.48124013555132183, 0.47691569782103144, 0.4726116603142256, 0.46832892365761486, 0.4640683256927162, 0.4598306447255939, 0.4556166026162419, 0.45142686771544605], "C": [0, 2.450639387184356E-4, 9.610128755457658E-4, 0.002120077170629231, 0.003695889329091456, 0.005663415141958709, 0.007998888190118583, 0.010679747547243077, 0.013684578525615106, 0.016993056316408097, 0.020585892383208897, 0.024444783474461374, 0.028552363127059725, 0.032892155539552374, 0.03744853169934512, 0.04220666765393143, 0.04715250482154125, 0.05227271224170209, 0.05755465067105978, 0.06298633843442243, 0.0685564189453833, 0.074254129815055, 0.08006927347142181, 0.0859921892155958, 0.09201372664485875, 0.09812522037579142, 0.10431846600404529, 0.1105856972404064, 0.11691956416574475, 0.1233131125502429, 0.12975976418496013, 0.1362532981763242, 0.14278783315655022, 0.1493578103652808, 0.15595797755992152, 0.1625833737142195, 0.1692293144666078, 0.17589137828171358, 0.18256539329021393, 0.1892474247739225, 0.19593376326460404, 0.2026209132265518, 0.20930558229442447, 0.21598467103922847, 0.22265526323665671, 0.2293146166132494, 0.23596015404704337, 0.24258945520051134, 0.24920024856467696, 0.25579040389432245, 0.2623579250151828, 0.26890094298495576, 0.27541770959084055, 0.28190659116716343, 0.2883660627174504, 0.2947947023260686, 0.30119118584528587, 0.30755428184428824, 0.31388284680734924, 0.3201758205689743, 0.32643222197443295, 0.3326511447546605, 0.33883175360504636, 0.3449732804581386, 0.3510750209407809, 0.3571363310066605, 0.36315662373568597, 0.3691353662920332, 0.37507207703309453, 0.3809663227619476, 0.3868177161163177, 0.39262591308735345, 0.3983906106618586, 0.4041115445819358, 0.4097884872162919, 0.4154212455377339, 0.42100965920165484, 0.42655359872055926, 0.4320529637299217, 0.437507681340902, 0.4429177045756566, 0.448283010881197, 0.45360360071794126, 0.4588794962192942, 0.46411073991876983, 0.46929739354134153, 0.4744395368558642, 0.47953726658557116, 0.4845906953737904, 0.48959995080216695, 0.49456517445881154, 0.499486521053918, 0.5043641575805154, 0.5091982625181343, 0.5139890250772723, 0.5187366444826533, 0.5234413292933646, 0.5281032967580576, 0.5327227722034814, 0.5372999884547056, 0.5418351852854681], "method": "dp853"}

num ode <equations> <initial> <t0> <t1> <options>


Description

    Solve a system of ordinary differential equations with options: {"points": number of times (2 to 100001, default 101), "method": "auto" | "dp853" | "radau" (or "stiff"), "rtol": 1e-10, "atol": 1e-12, "event": an expression of t and the variables (the integration stops when it crosses 0), "direction": -1 (decreasing), 1 (increasing) or 0}.
With an event, the result has "event": {"t": ..., "x": ...} and stops there

Parameters

    equations:   JSON object: the derivative of each variable - String - required
    initial:   JSON object: the value of each variable at t0 - String - required
    t0:   The first time - Number - required
    t1:   The last time - Number - required
    options:   JSON object: points, method, rtol, atol, event, direction - String - required
admin
num ode "{\"x\": \"v\", \"v\": \"-x\"}" "{\"x\": 1, \"v\": 0}" 0 3 "{\"points\": 4}"
mentdb
{"t": [0, 1, 2, 3], "x": [1, 0.5403023058732985, -0.41614683653630213, -0.9899924965961377], "v": [0, -0.8414709848036515, -0.9092974268292137, -0.14112000807637803], "method": "dp853"}
admin
num ode "{\"x\": \"v\", \"v\": \"-_g0\"}" "{\"x\": 10, \"v\": 0}" 0 5 "{\"points\": 3, \"event\": \"x\"}"
mentdb
{"t": [0, 1.4280869812290369], "x": [10, -1.7763568394002505E-15], "v": [0, -14.004749194469737], "method": "dp853", "event": {"t": 1.4280869812290369, "x": -1.7763568394002505E-15, "v": -14.004749194469737}}

num minimize <expression> <start>


Description

    Minimum of a function of several variables from a starting point (as scipy.optimize.minimize): Newton's method with the exact gradient and Hessian (automatic differentiation).
Returns {variables, value, iterations, method, converged}. A local method: the result depends on the starting point. [k] session variables, _c constants

Parameters

    expression:   The function of several variables - String - required
    start:   JSON object: the starting value of each variable, ex: {"x": 0, "y": 0} - String - required
admin
num minimize "(1-x)^2+100*(y-x^2)^2" "{\"x\": -1.2, \"y\": 1}"
mentdb
{"x": 1, "y": 1, "value": 0, "iterations": 23, "method": "newton", "converged": true}
Least squares by hand: best line through 3 points
admin
num minimize "(a+b*1-2)^2+(a+b*2-3.9)^2+(a+b*3-6.1)^2" "{\"a\": 0, \"b\": 1}"
mentdb
{"a": -0.1, "b": 2.05, "value": 0.015, "iterations": 2, "method": "newton", "converged": true}
Himmelblau function from (0, 0)
admin
num minimize "(x^2+y-11)^2+(x+y^2-7)^2" "{\"x\": 0, \"y\": 0}"
mentdb
{"x": 3, "y": 2, "value": 0, "iterations": 10, "method": "newton", "converged": true}
Least squares by hand: line through 3 points
admin
num minimize "(a+b*0-1)^2+(a+b*1-3.1)^2+(a+b*2-4.9)^2" "{\"a\": 0, \"b\": 0}"
mentdb
{"a": 1.05, "b": 1.95, "value": 0.015, "iterations": 2, "method": "newton", "converged": true}
Minimum of a function of 3 variables
admin
num minimize "(x-1)^2+(y+2)^2+(z-3)^2+x*y" "{\"x\": 0, \"y\": 0, \"z\": 0}"
mentdb
{"x": 2.6666666666666665, "y": -3.3333333333333335, "z": 3, "value": -4.333333333333334, "iterations": 2, "method": "newton", "converged": true}
Unbounded function: error
admin
num minimize "x+y" "{\"x\": 0, \"y\": 0}"
mentdb
Sorry, an error appears (no minimum found: the function seems to decrease without limit (at x = -1.0E11, y = -1.0E11)).

num minimize <expression> <start> <options>


Description

    Minimum of a function of several variables with bounds and constraints: {"bounds": {"x": [min, max]} (null: no bound), "constraints": ["x + y <= 1", "x*y = 2", "x >= 0"], "tol": 1e-12, "max_iter": 1000}.
Augmented Lagrangian, then Newton on the optimality conditions (KKT) for full precision; the result has max_violation (the largest violation of a constraint)

Parameters

    expression:   The function of several variables - String - required
    start:   JSON object: the starting value of each variable - String - required
    options:   JSON object: bounds, constraints, tol, max_iter - String - required
admin
num minimize "(x-1)^2+(y-2.5)^2" "{\"x\": 2, \"y\": 0}" "{\"constraints\": [\"x - 2*y + 2 >= 0\", \"-x - 2*y + 6 >= 0\", \"-x + 2*y + 2 >= 0\"], \"bounds\": {\"x\": [0, null], \"y\": [0, null]}}"
mentdb
{"x": 1.4, "y": 1.7, "value": 0.8, "iterations": 16, "method": "augmented_lagrangian", "converged": true, "max_violation": 0}
Point of the circle x^2 + y^2 = 1 closest to (2, 1)
admin
num minimize "(x-2)^2+(y-1)^2" "{\"x\": 1, \"y\": 0}" "{\"constraints\": \"x^2 + y^2 = 1\"}"
mentdb
{"x": 0.8944271909999159, "y": 0.4472135954999579, "value": 1.527864045000421, "iterations": 40, "method": "augmented_lagrangian", "converged": true, "max_violation": 1.1102230246251565E-16}
Bounds only
admin
num minimize "(x-5)^2+(y+3)^2" "{\"x\": 0, \"y\": 0}" "{\"bounds\": {\"x\": [0, 2], \"y\": [-1, 1]}}"
mentdb
{"x": 2, "y": -1, "value": 13, "iterations": 35, "method": "augmented_lagrangian", "converged": true, "max_violation": 0}
Production: minimum cost with a production of 100 units (Cobb-Douglas)
admin
num minimize "20*K+10*L" "{\"K\": 10, \"L\": 10}" "{\"constraints\": \"K^0.5*L^0.5 = 100\", \"bounds\": {\"K\": [0, null], \"L\": [0, null]}}"
mentdb
{"K": 70.71067811865476, "L": 141.4213562373095, "value": 2828.42712474619, "iterations": 8, "method": "augmented_lagrangian", "converged": true, "max_violation": 0}
Several equality and inequality constraints
admin
num minimize "x^2+y^2+z^2" "{\"x\": 1, \"y\": 1, \"z\": 1}" "{\"constraints\": [\"x+y+z = 1\", \"x >= 0.5\"]}"
mentdb
{"x": 0.5, "y": 0.25, "z": 0.25, "value": 0.375, "iterations": 19, "method": "augmented_lagrangian", "converged": true, "max_violation": 0}
Portfolio: minimum variance with an expected return of 8 %
admin
num minimize "0.04*a^2+0.09*b^2+0.012*a*b" "{\"a\": 0.5, \"b\": 0.5}" "{\"constraints\": [\"a + b = 1\", \"0.06*a + 0.12*b = 0.08\"]}"
mentdb
{"a": 0.6666666666666665, "b": 0.3333333333333334, "value": 0.030444444444444444, "iterations": 16, "method": "augmented_lagrangian", "converged": true, "max_violation": 1.3877787807814457E-17}
Worked example: Optimization: the cheapest can (A can of 330 mL (330 cm3): the metal costs 2 times more for the top and the bottom than for the side. Minimize the cost 2 * 2 pi r^2 + 2 pi r h with the volume pi r^2 h = 330.)
admin
#Worked example: Optimization: the cheapest can; #Minimum of the cost with the constraint of volume; num minimize "2*2*pi*r^2 + 2*pi*r*h" "{\"r\": 3, \"h\": 10}" "{\"constraints\": \"pi*r^2*h = 330\", \"bounds\": {\"r\": [0, null], \"h\": [0, null]}}"; #Check of the volume; math eval "pi*r^2*h" "{\"r\": 2.972359660434031, \"h\": 11.889438641736128}";
mentdb
330.00000000000006

num maximize <expression> <start>


Description

    Maximum of a function of several variables from a starting point (see 'num minimize')

Parameters

    expression:   The function of several variables - String - required
    start:   JSON object: the starting value of each variable - String - required
admin
num maximize "-(x-2)^2-(y+1)^2+5" "{\"x\": 0, \"y\": 0}"
mentdb
{"x": 2, "y": -1, "value": 5, "iterations": 2, "method": "newton", "converged": true}
Maximum of a two-variable profit
admin
num maximize "60*x+40*y-2*x^2-y^2-x*y" "{\"x\": 1, \"y\": 1}"
mentdb
{"x": 11.428571428571429, "y": 14.285714285714286, "value": 628.5714285714288, "iterations": 2, "method": "newton", "converged": true}
Profit of two products
admin
num maximize "40*x+30*y-x^2-y^2" "{\"x\": 0, \"y\": 0}"
mentdb
{"x": 20, "y": 15, "value": 625, "iterations": 2, "method": "newton", "converged": true}
Maximum of a Gaussian in 2 dimensions
admin
num maximize "exp(-((x-1)^2+(y-2)^2))" "{\"x\": 0, \"y\": 0}"
mentdb
{"x": 1, "y": 2, "value": 1, "iterations": 15, "method": "newton", "converged": true}

num maximize <expression> <start> <options>


Description

    Maximum of a function of several variables with bounds and constraints (see 'num minimize'). Ex: the box of largest volume with a surface of 24

Parameters

    expression:   The function of several variables - String - required
    start:   JSON object: the starting value of each variable - String - required
    options:   JSON object: bounds, constraints, tol, max_iter - String - required
admin
num maximize "x*y*z" "{\"x\": 1, \"y\": 1, \"z\": 1}" "{\"constraints\": \"2*(x*y + y*z + x*z) = 24\", \"bounds\": {\"x\": [0, null], \"y\": [0, null], \"z\": [0, null]}}"
mentdb
{"x": 2, "y": 2, "z": 2, "value": 8, "iterations": 15, "method": "augmented_lagrangian", "converged": true, "max_violation": 0}
Cylinder of largest volume with an area of 100 (r, h)
admin
num maximize "pi*r^2*h" "{\"r\": 1, \"h\": 1}" "{\"constraints\": \"2*pi*r^2 + 2*pi*r*h = 100\", \"bounds\": {\"r\": [0, null], \"h\": [0, null]}}"
mentdb
{"r": 2.3032943298089035, "h": 4.606588659617806, "value": 76.77647766029678, "iterations": 24, "method": "augmented_lagrangian", "converged": true, "max_violation": 0}
Box without a lid: maximum volume with 1 m2 of material
admin
num maximize "x*y*h" "{\"x\": 0.5, \"y\": 0.5, \"h\": 0.2}" "{\"constraints\": \"x*y + 2*h*(x+y) = 1\", \"bounds\": {\"x\": [0, null], \"y\": [0, null], \"h\": [0, null]}}"
mentdb
{"x": 0.5773502691896257, "y": 0.5773502691896257, "h": 0.28867513459481287, "value": 0.09622504486493762, "iterations": 25, "method": "augmented_lagrangian", "converged": true, "max_violation": 0}
Linear programme: 2 products, 2 resources
admin
num maximize "3*x+5*y" "{\"x\": 0, \"y\": 0}" "{\"constraints\": [\"x <= 4\", \"2*y <= 12\", \"3*x + 2*y <= 18\"], \"bounds\": {\"x\": [0, null], \"y\": [0, null]}}"
mentdb
{"x": 2, "y": 6, "value": 36, "iterations": 8, "method": "augmented_lagrangian", "converged": true, "max_violation": 0}

num interpolate <xs> <ys> <x> <mode>


Description

    Interpolation (as scipy.interpolate) at one x or at a JSON array of x. Modes: linear, nearest, previous, next, cubic (spline not-a-knot, as CubicSpline), natural (natural spline, = spline), clamped (zero slopes at the ends), pchip (monotone, no overshoot), akima, lagrange (polynomial, extrapolates)

Parameters

    xs:   JSON array of x - String - required
    ys:   JSON array of y - String - required
    x:   A number or a JSON array of numbers - String - required
    mode:   linear, nearest, previous, next, cubic, natural, clamped, pchip, akima, spline, lagrange - String - required
admin
num interpolate "[0, 1, 2, 3, 4]" "[0, 1, 8, 27, 64]" "[0.5, 1.5, 2.5]" "cubic"
mentdb
[0.125, 3.375, 15.625]
admin
num interpolate "[0, 1, 2, 3]" "[0, 1, 1, 0]" 1.5 "pchip"
mentdb
1

num interpolate_derivative <xs> <ys> <x> <mode>


Description

    First derivative of the interpolating function at x (a number or a JSON array)

Parameters

    xs:   JSON array of x - String - required
    ys:   JSON array of y - String - required
    x:   A number or a JSON array of numbers - String - required
    mode:   The mode (see 'num interpolate') - String - required
admin
num interpolate_derivative "[0, 1, 2, 3, 4]" "[0, 1, 8, 27, 64]" 2 "cubic"
mentdb
12
Velocity from measured positions
admin
num interpolate_derivative "[0, 1, 2, 3, 4]" "[0, 4.9, 19.6, 44.1, 78.4]" "[1, 2, 3]" "cubic"
mentdb
[9.8, 19.6, 29.400000000000002]
Velocity from measured positions (pchip)
admin
num interpolate_derivative "[0, 1, 2, 3, 4]" "[0, 4.9, 19.6, 44.1, 78.4]" 2.5 "pchip"
mentdb
25.010416666666664
Linear: slope of the segment
admin
num interpolate_derivative "[0, 1, 2]" "[0, 2, 3]" 0.5 "linear"
mentdb
2

num interpolate_derivative <xs> <ys> <x> <mode> <order>


Description

    Derivative of order 1, 2 or 3 of the interpolating function at x

Parameters

    xs:   JSON array of x - String - required
    ys:   JSON array of y - String - required
    x:   A number or a JSON array of numbers - String - required
    mode:   The mode - String - required
    order:   1, 2 or 3 - Number - required
admin
num interpolate_derivative "[0, 1, 2, 3, 4]" "[0, 1, 8, 27, 64]" 2 "cubic" 2
mentdb
12
Acceleration from measured positions (second derivative)
admin
num interpolate_derivative "[0, 1, 2, 3, 4]" "[0, 4.9, 19.6, 44.1, 78.4]" 2 "cubic" 2
mentdb
9.79999999999999
Third derivative of a cubic spline
admin
num interpolate_derivative "[0, 1, 2, 3, 4]" "[0, 1, 8, 27, 64]" 2.5 "cubic" 3
mentdb
6
Order 1 is the same as the 4 parameters
admin
num interpolate_derivative "[0, 1, 2, 3, 4]" "[0, 1, 8, 27, 64]" 2 "cubic" 1
mentdb
12

num interpolate_integral <xs> <ys> <a> <b> <mode>


Description

    Integral of the interpolating function from a to b (exact for the piecewise polynomial)

Parameters

    xs:   JSON array of x - String - required
    ys:   JSON array of y - String - required
    a:   The lower bound - Number - required
    b:   The upper bound - Number - required
    mode:   The mode - String - required
admin
num interpolate_integral "[0, 1, 2, 3, 4]" "[0, 1, 8, 27, 64]" 0 4 "cubic"
mentdb
64
admin
num interpolate_integral "[0, 1, 2, 3]" "[1, 3, 2, 4]" 0 3 "linear"
mentdb
7.5
Distance from measured speeds
admin
num interpolate_integral "[0, 10, 20, 30, 40]" "[0, 12, 20, 24, 25]" 0 40 "cubic"
mentdb
696.6666666666667
Distance from a table of speeds (m/s) every 10 s
admin
num interpolate_integral "[0, 10, 20, 30]" "[0, 8, 12, 13]" 0 30 "pchip"
mentdb
273.3333333333333
Part of the interval
admin
num interpolate_integral "[0, 1, 2, 3]" "[0, 1, 4, 9]" 1 2 "cubic"
mentdb
2.333333333333333
Worked example: Real data: distance and acceleration from speed readings (A car speed (m/s) read every 2 seconds. The distance is the integral of the speed (interpolated by a monotone curve), the acceleration is its derivative; the smoothing removes the noise of the readings.)
admin
#Worked example: Real data: distance and acceleration from speed readings; #The readings; -> "[ts]" "[0, 2, 4, 6, 8, 10, 12]"; -> "[vs]" "[0, 5.1, 9.8, 13.9, 17.2, 19.6, 21.0]"; #Distance travelled in 12 s (m); num interpolate_integral [ts] [vs] 0 12 "pchip"; #Distance with straight segments (trapezoids); num interpolate_integral [ts] [vs] 0 12 "linear"; #Acceleration at t = 5 s (m/s2); num interpolate_derivative [ts] [vs] 5 "cubic"; #Speed at t = 7 s; num interpolate [ts] [vs] 7 "pchip"; #Smoothed speeds (Savitzky-Golay); dsp savgol [vs] 5 2; #Speed in km/h at the end; unit convert 21.0 "m/s" "km/h"; #Chart of the speed; plot interpolate [ts] [vs] "pchip";
mentdb
The chart is shown in the Plot tab of the output of the editor

num interpolate2 <xs> <ys> <z> <x> <y> <mode>


Description

    Interpolation on a 2D grid: z[i][j] = f(xs[i], ys[j]). Modes: linear (bilinear, as RegularGridInterpolator), cubic (bicubic spline, as RectBivariateSpline, at least 4 x and 4 y), nearest. x and y: numbers or JSON arrays of the same size

Parameters

    xs:   JSON array of x (rows) - String - required
    ys:   JSON array of y (columns) - String - required
    z:   JSON matrix: one row per x, one value per y - String - required
    x:   A number or a JSON array - String - required
    y:   A number or a JSON array - String - required
    mode:   linear, cubic, nearest - String - required
admin
num interpolate2 "[0, 1, 2]" "[0, 1]" "[[0, 1], [1, 2], [2, 3]]" 0.5 0.5 "linear"
mentdb
1
admin
num interpolate2 "[0, 1, 2, 3]" "[0, 1, 2, 3]" "[[0, 1, 4, 9], [1, 2, 5, 10], [4, 5, 8, 13], [9, 10, 13, 18]]" 1.5 2.5 "cubic"
mentdb
8.5
Bilinear interpolation in a temperature grid
admin
num interpolate2 "[0, 10, 20]" "[0, 100]" "[[15, 12], [17, 13], [20, 15]]" 15 50 "linear"
mentdb
16.25
Nearest point of the grid
admin
num interpolate2 "[0, 1, 2]" "[0, 1]" "[[0, 1], [1, 2], [2, 3]]" 1.4 0.6 "nearest"
mentdb
2
At a point of the grid: the value
admin
num interpolate2 "[0, 1, 2]" "[0, 1]" "[[0, 1], [1, 2], [2, 3]]" 1 1 "linear"
mentdb
2
Outside the grid: error
admin
num interpolate2 "[0, 1, 2]" "[0, 1]" "[[0, 1], [1, 2], [2, 3]]" 3 0 "linear"
mentdb
Sorry, an error appears (x = 3 is out of [0, 2]).