Search code examples
sympynonlinear-functionsexpression-evaluation

Evaluating the solution pairs of the sympy function


I have a non linear function with two variables, I want to solve the equation . But the solution itself is an equation. How do i evaluate the function at a certain point

CODE:

import sympy as sp
sp.init_printing()
x1,x2,y1,y2 = sp.symbols('x1,x2,y1,y2')
x1,y1=-2,3
f = sp.Eq((x1-x2)**2 + (y1-y2)**2,1)
a = sp.solve([f],(x2,y2))

now i want a a few solution pairs of the function 'a' .

Thanks in advance :)


Solution

  • You have a single equation in two unknowns: pick a value for one and solve for the other. Here, we pick values for y2 and solve for x2 and pair each solution with the value of i. There is one solution for y2 = 2 and 4 and 2 solutions when it is 3

    >>> [(j,i) for i in range(2,5) for j in sp.solve(f.subs(y2,i),x2)]
    [(-2, 2), (-3, 3), (-1, 3), (-2, 4)]
    

    Realizing that the equation represents a circle centered at (-2, 3) also permits one to use SymPy's Circle to give you an arbitrary Point in terms of a parameter:

    >>> from sympy import Circle
    >>> from sympy.abc import t
    >>> Circle((-2,3),1).arbitrary_point(t)
    Point2D(cos(t) - 2, sin(t) + 3)
    

    Substitute a value for t to get the corresponding point

    >>> _.subs(t,pi)
    Point2D(-3, 3)