linear-algebraequation-solvingquadratic

Solve a Quadratic Equation in two variables using Python


I have got two equations, one linear say,

formula , where m and c are constants and the other quadratic say,

another , where x1, y1 and r are constants.

Is there a way I can solve for x and y using Python ?

I could solve them on pen and paper finding the relation between x and y from the linear equation and substituting it in the other. There would be two roots satisfying the quadratic equation.


Solution

  • Look at SymPy.

    Here is an example of how to solve a simple difference of squares equation, taken from their documentation.

    >>> from sympy.solvers import solve
    >>> from sympy import Symbol
    
    >>> x = Symbol('x')
    >>> solve(x**2 - 1, x)
    
    [-1, 1]
    

    Regarding your specific problem, the solution will look something like this:

    >>> x = Symbol('x')
    >>> y = Symbol('y')
    
    >>> solve( (x-c1)**2 + (y-c2)**2 - c3**2, x, y)
    

    c1, c2 and c3 are the constants declared as variables earlier in your code.