I am trying to calculate the numerical differentiation of a particular function. This function may vary (for example, can be a line or a circle).
My first thought is to define a generic function:
delegate double Function(double arg);
static double Derivative(Function f, double arg)
{
double h = 10e-6;
double h2 = h*2;
return (f(arg-h2) - 8*f(arg-h) + 8*f(arg+h) - f(arg+h2)) / (h2*6);
}
For a linear line:
double LinearLine(double arg)
{
double m = 2.0; double c = 1.0;
return m*arg + c;
}
I call:
Function myFunc = new Function(LinearLine);
Derivative(myFunc, 3); //Derivative at x=3
However, in this method I will need to hardcode the m and the c into the LinearLine() function.
Alternatively, if I pass m and c into the LinearLine argument list : LinearLine(double m, double c), I will have to rewrite the Derivative() function return value everytime I call a different function. (For example Circle(double radius, double x, double y)).
My question is in such a case, what would be a good way to design such a class/function?