Question is off-topic or not relevant
Need help putting integral function into C#
Hi, I'm trying to put this function into one of my scripts, but I don't know how to do the integrals in C#. Hopefully someone here can help
Thanks
Answer by Bunny83 · Mar 04, 2020 at 01:37 AM
This is off topic since it's a pure mathematical / computer science question and not related to Unity. Anyways, as you can actually read here:
These integrals cannot be expressed in terms of elementary functions [...] however, many numerical approximations are known
Specifically the expansion into an infinite series. Of course we can not calculate an infinite series however we can simply choose a certain number of terms and stop.
Something like that might work
public static double Phi(double x, int n)
{
double sum = 0;
double x2 = x*x;
double nom = x;
double denom = 1;
double c = 1;
for (int i = 0; i < n; i++)
{
sum += nom / denom;
c += 2;
nom *= x2;
denom *= c;
}
return 0.5 + sum * System.Math.Exp(-x2*0.5) / System.Math.Sqrt(2 * System.Math.PI);
}
Unless I made a mistake this should calculate the "nth" approximation of the Phi function. You have to decide how large you choose your "n".