# How can I numerically solve an equation in Unity c#?

Specifically, I need to solve for x given values of y, a, u and n using this equation:

y = sqrt(a^3 / u) * (x - n * sin(x))

But I haven't even completed high school and I have no idea where to start. I found something called the Newton-Raphson method that seems to do what I want. Can I translate that method into C#? Is there a more performance-mindful way to do it?

Any help is super appreciated... I've been working on this for days and I still have no idea what I'm doing...

Edit: to future googlers, u/Romestus on reddit made this awesome script that solves the equation using Newton's method. I hope it can help you too!

### People who like this

yeah - mathf is comparatively primitive stuff like abs(), sin(), etc. nothing remotely like this.

**Answer** by elenzil
·
Aug 15, 2017 at 06:45 PM

newton-raphson is what i was going to suggest.

i believe for NR you need the *derivative* of the function as well.
that would be a second function you calculate by hand on paper.
this is a moderately complex function to take the derivative of!

fortunately it looks like it can be simplified a bit.

let's replace all of sqrt(a^3/u) by a constant k.

so now

```
y = k * (x - n * sin(x)).
y = (k * x) - (k * n * sin(x)).
y' = [(k * x) - (k * n * sin(x))]'.
y' = (k * x)' - (k * n * sin(x))'.
y' = k - k * n * cos(x).
y' = k * (1 - n * cos(x)).
^----- that's your derivative, where k = sqrt(a^3/u).
```

Then you could either implement NR yourself, or if you google C# Newton Raphson it looks like there's a fair number of examples.

Thank you. I see wolfram alpha can also calculate derivatives for me.

if you google C# Newton Raphson it looks like there's a fair number of examples.

None that I can understand and none that I can figure out how to get working in Unity. I think I'm going to try my own implementation, but I don't have high hopes...

i was thinking about this a bit more, and realized that with this equation, depending on the values of n, you're going to have either 1, 2, 3, 4, or arbitrarily many corresponding values of x. the value of k (the sqrt) doesn't matter in that respect.

for example, here's the graph with n = 10. you can see that for y = 0, there's 7 different values of x. http://imgur.com/a/TkK3o

in that situation, newton-raphson or *any* solver which produces a single value is going to give you results which might not be what you want. a solver which yields *all* the intersections of Y with the function might be what you want, but those are harder to come by. Newton-Raphson will return an approximation of one of the intersection points, depending on the initial value you seed it with.

are you sure you know what you want ?

Yeah, I forgot to mention it in the OP, but the value of n is always between 0 and 1.

I'm good now though. u/Romestus from reddit made a script for me that does exactly what I want!

**Answer** by M-G-Production
·
Aug 16, 2017 at 05:16 PM

I would probably look like that:

```
y = Mathf.Sqrt(Mathf.Pow(a, 3)/u) * (x - n * Mathf.Sin(x))
```

the question is not how to express the formula in C#,

it's *how to solve for X given Y*.

### Your answer

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

### Follow this Question

### Related Questions

Predict the hit position 0 Answers

Unity Math is Wrong for Some Reason 0 Answers

Check if a point is on the right or left of a vector? 2 Answers

Money-system for decillionare game 1 Answer