**closed**Mar 04, 2020 at 07:57 AM by Hellium for the following reason:

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".

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.