I am trying to implement the trapezodial rule in c# as a function:
Int_a^b f(x) = (b-a) * [f(a) + f(b)] / 2
Is there a feature in c# which allows me to write the function as such?
double integrate(double b, double a, function f)
{
return (b-a) * (f(a) + f(b)) / 2;
}
Where f
can be any polynomial expression defined inside another function, for example:
double f (double x)
{
return x*x + 2*x;
}
In your case you want to pass a Func<double, double>
. Like so
double integrate(double b, double a, Func<double, double> f)
{
return (b-a) * (f(a) + f(b)) / 2;
}
double integrand = integrate(0, 2 * Math.PI, x => x*x + 2*x);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With