This site uses strictly necessary cookies. More Information

X**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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.