I know that Erlang isn't generally used for this sort of things, but by any chance is there numerical functions with integral/derivative being already written(like quad in GNU Octave, taking function and two numbers)?
You may solve problem with functional programmatic approach.
As you know derivative defined as dF(X) = ( F(X+dX) - F(X) ) / dX
Let's create generic function, which returns derivative functions:
-module( calc ).
-export( [ d/2 ] ).
d( F, Dx ) ->
fun( X ) ->
( F( X + Dx ) - F ( X ) ) / Dx
end.
Example of usage in interpreter:
1> c(calc).
{ok,calc}
2>
Let's define Sqr function: ( X * X )
2> Sqr = fun( X ) -> X * X end.
#Fun<erl_eval.6.82930912>
3>
Let's get derivative from Sqr
3> DSqr = calc:d( Sqr, 0.001 ).
#Fun<calc.0.111170057>
4>
Check Sqr function:
4> [ Sqr( X ) || X <- lists:seq( 1, 10 ) ].
[1,4,9,16,25,36,49,64,81,100]
5>
And check derivative ( as you know - derivative from ( X * X ) is ( 2 * X ) )
5> [ DSqr( X ) || X <- lists:seq( 1, 10 ) ].
[2.0009999999996975,4.000999999999699,6.000999999999479,
8.0010000000037,10.001000000002591,12.001000000005035,
14.00100000000748,16.000999999988608,18.000999999983947,
20.000999999993496]
Because of using finite value of dX = 0.001 - we didn't get clear result, but it's very close to real [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ]
Integral function might be defined in similar way. Definite integral is:
So antiderivative is:
F(0)
- constant. And definite integral can be expressed through any numerical integration algorithm.