-1

I need math library to solve simple system of 3 equations like (ax2 + bx + c = y) and get a, b, c, where I know 3 couples (x,y).

I searching for something but I didn't find anything useful for me.

Denis
  • 503
  • 8
  • 32
  • 6
    How would you do this on a sheet of paper? – Martijn Courteaux Nov 26 '13 at 18:46
  • what exactly are you asking? How to solve a 2nd order equation? that's pretty easy: look here http://en.wikipedia.org/wiki/Quadratic_equation#Discriminant – Andersnk Nov 26 '13 at 18:47
  • 2
    If by ready programs you mean that java.lang.Math has a method for this the answer is no, http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html – Culyx Nov 26 '13 at 18:48
  • 1
    @AndersNK It's solving for the curve, not the parameters. See [curve fitting](http://en.wikipedia.org/wiki/Curve_fitting). – Zong Nov 26 '13 at 18:49
  • I think OP is asking how to solve for `a,b,c` given three points on the parabola, which is even easier than solving for the roots. What have you tried? – Reinstate Monica -- notmaynard Nov 26 '13 at 18:52
  • @iamnotmaynard I'm not sure it'd be easier to code, though. How would you solve a general linear system without using matrices? – Zong Nov 26 '13 at 18:58
  • @MartijnCourteaux At first I will get rid of coefficient A, having made subtraction of one equation of another. Then sequentially I will calculate B and C, then I will calculate A. But on paper we see directly that it is better to add/substract with what. And in a code it is necessary to consider many conditions and a code there can be a quite big. Or I am wrong? – Denis Nov 26 '13 at 18:59
  • @AndersNK I want to compute coefficients of equation, when I know X and Y (3 couples of value X and Y), not discriminant – Denis Nov 26 '13 at 19:04
  • @iamnotmaynard it is not easier. Solving for the roots have a simple alghoritm for computation, but solving a, b and c haven't a simple alghoritm – Denis Nov 26 '13 at 19:06
  • You could solve the general equation with x1, x2, x3, y1, y2, y3 but you might have problems with division by zero with some values, etc. Using a linear algebra library seems a bit overkill for this but look into [Commons Math](http://commons.apache.org/proper/commons-math/userguide/linear.html) by Apache. – Zong Nov 26 '13 at 19:08
  • I'll retract my statement that it's easier (as far as coding, anyway). Given that it will be limited to 2nd-degree equations, it should be still pretty doable without matrix libraries (not excluding arrays). However, this question (as it currently is) is probably off-topic because (1) it's asking for libraries, and (2) OP has not shown any attempt to solve the problem. – Reinstate Monica -- notmaynard Nov 26 '13 at 19:19
  • @ZongZhengLi In paper there are a lot of computations, if I will do paper alghoritm, there will be a big code. Thanks for link, I will try it! – Denis Nov 26 '13 at 19:39
  • @iamnotmaynard you are wrong, I just have written a paper alghotitm, but there are a lot of computations, I am afraid of mistakes. – Denis Nov 26 '13 at 19:41
  • After spending some time fleshing out some code, I'll admit I was hasty in my comments. Even using linear algebra, it's more complex than I first thought. – Reinstate Monica -- notmaynard Nov 26 '13 at 19:49

2 Answers2

4

This is a linear system of 3 equations, not a 2nd order equation! x and y are values are known! To solve a linear system see : https://code.google.com/p/efficient-java-matrix-library/wiki/SolvingLinearSystems

0

Question solved, thanks to Zong Zheng Li and Pavlos Fragkiadoulakis

RealMatrix coefficients = new Array2DRowRealMatrix(new double[][] { { px1*px1, px1, 1 }, { px2*px2, px2, 1 },
            { x*x, x, 1 } }, false);
DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();
RealVector constants = new ArrayRealVector(new double[] { py1, py2, y }, false);
RealVector solution = solver.solve(constants);
double a = solution.getEntry(0);
double b = solution.getEntry(1);
double c = solution.getEntry(2);
Denis
  • 503
  • 8
  • 32