# Solvers Python or Apache

Trying to solve a 3rd degree polynomial and feed those coefficients into a solver to determine x given y (at eft 6 hrs what density should I expect). Im using 7.9 so is there any good solvers that I could use from Apache Commons or a good way to roll my own?

``````from org.apache.commons.math3.fitting import PolynomialCurveFitter as pcf
from org.apache.commons.math3.fitting import WeightedObservedPoints as wop

eft = [0.0, 7, 15, 23]
dcw = [0.583,0.72, 2.82, 7.29]
fitter = pcf.create(2)
tstPoints = wop()

for i in range(4):
time = eft[i]
density = dcw[i]

coeff = fitter.fit(tstPoints.toList())
for i in range(len(coeff)):
print coeff[i]
``````

I wrote this before we access to Apache math. May be useful for you. The polyfit() function is what I think you’re looking for.

I don’t know of any built in ones off hand.

@JordanCClark 's library seems to have the functionality you need (and up to 5th degree polynomials it looks like).

But if finding roots of a 3rd degree polynomial is all you need, there is a formula for it you could hardcode by hand. The hard part I imagine would be handling the potential imaginary numbers that can arise The Cubic Formula

If you’re not comfortable using someone else’s library and or handling the imaginary numbers, you can always try implementing your own version of Newton’s method or some other numerical approximation method for finding roots for which you can find the process online.

Thanks @JordanCClark and @bkarabinchak.psi for the libraries and recommendations. I figured since I was using the Apache Commons library to get the nth degree polynomial I could then put that into a solver from the same library to get the roots maybe something like these [solvers]. (LaguerreSolver (Apache Commons Math 3.3 API))

Since we converting an Excel sheet to Ignition were just using the goal seek option in Excel to solve for x given y using the coefficients. Is there a good way to implement this in Ignition using some existing library?

I haven’t verified this personally but this post in stackoverflow -

uses the brent Method in java for finding roots. Read the entire post as there’s some caveats - only finds roots within your interval range, and only that are more than an interval size apart (in his example, more than .01 apart).

You can try replicating his code. You can start off with

``````import org.apache.commons.math3.analysis.UnivariateFunction
import org.apache.commons.math3.analysis.solvers.BrentSolver
``````

and go from there.

I don’t know how excel’s goal seek works behind the scenes but you are just trying to find roots essentially so any root finding algorithm should fit your needs.

Thats a great example. How would you instantiate the UnivariateFunction from java?

``````UnivariateFunction f = new UnivariateFunction() {

@Override
public double value(double x) {
return 103*Math.pow(x, 5.0)-5*Math.pow(x, 4.0)-5*Math.pow(x, 3.0)-5*Math.pow(x, 2.0)-5*x - 105;
}
};
``````

You would just make a Python subclass, e.g.:

``````from org.apache.commons.math3.analysis import UnivariateFunction

class myFunction(UnivariateFunction):
def value(self, x):
return 0.0 # do math here

``````
2 Likes