# Numerical Approximation in Java

I am trying to solve an equation which I believe cannot be done analytically, but can use a numerical approximation to get a result. The equation is: $$\frac{2*\sqrt{\pi}*h*s*e^{m^{2}/(2*s^2)}}{\sqrt{2}} = a$$ where $$h, m,$$ and $$a$$ are given constants. This is for a project that I'm writing in Java, and it would be infeasible to grab the solution output from another program such as Matlab, Mathematica, or some other scientific computing language for a couple reasons, one of which is that I will have to solve this equation hundreds of times with different values. Basically I am wondering if anybody knows of a library I can easily import and use for this kind of problem, and to possibly spell out how I would go about doing that. So far I have been looking through math libraries such as Apache Commons Math but cannot find out if they are appropriate. Thanks!

• When a variable (here: m) appears in only one place it can usually be solved directly. And if you looked for s for example: That’s basic numerical mathematics, you don’t need a library for that. Mar 17, 2020 at 7:53

With $$p:=\dfrac ms$$, squaring and collecting all the constants to avoid the mess, the equation reads

$$e^{p^2}=\lambda p^2$$ or $$-p^2e^{-p^2}=-\lambda^{-1}.$$

Hence using the Lambert function,

$$s=\frac m{\sqrt{-W(-\lambda^{-1})}}.$$

The positive square root must be taken, and the two branches of $$W$$ yield two real solutions (provided $$\lambda>e$$).

You can probably find ready-made implementations of that function in Java, or adapt from a C/C++ version.

You are asking how to solve $$e^{ax^2}=b$$ for $$x$$, given constants $$a,b$$. The solution is $$x=\sqrt{\frac{1}{a} \log b}$$, which can be computed (approximately) using standard algorithms for computing the log and the square root.

In particular, your specific equation has the solution

$$m = \sqrt{2s^2 \log (a/(\sqrt{2\pi} hs))}.$$

You can verify the correctness of this solution by plugging into the equation and verifying it works.

• I don't think it's that simple. I am fairly certain that you cannot find a symbolic solution for x with the equation I provided above. Mar 17, 2020 at 22:05
• @JJJJJJJJJJJJJJJJ, I suggest you take another look. See my edited answer. Your certainty seems misplaced to me.
– D.W.
Mar 17, 2020 at 22:15
• Sorry about that D.W., I meant to say m was a constant and s was the variable in question.. Did not mean to waste peoples' time. What I'm doing right now is Regula Falsi and it seems to be working alright. Mar 19, 2020 at 13:40

Quote: "I don't think it's that simple. I am fairly certain that you cannot find a symbolic solution for x with the equation I provided above. "

$$\frac{2*\sqrt{\pi}*h*s*e^{m^{2}/(2*s^2)}}{\sqrt{2}} = a$$

Multiply both sides by sqrt (2).

Divide both sides by 2 sqrt(pi) h s.

Take the natural logarithm.

Multiply by 2 s^2.

Take the square root.

Now m is alone on the left side, and the right side is the solution:

$$m = \sqrt {\ln (\frac {a \cdot \sqrt{2}}{2*\sqrt{\pi}*h*s}) \cdot (2\cdot s^2)}$$

(Of course it would help if you posted the problem you have, and not a different problem).

• See my comment above - s is the variable in question. I already solved the problem anyway. Mar 22, 2020 at 3:46