How to find x and y value for following expressions using python expressions 1:2x+1=2 and expressions 2:x-y=3

Question:

2x+1=2,
x-y=3

In python i’m trying to solve above equations to get x and y value using python.

from sympy import symbols, solve, sympify, Eq

def find_variables(eq1, eq2, var1, var2):
    x, y, a, b = symbols('x y {} {}'.format(var1, var2))
    lhs1, rhs1 = eq1.split('=')
    lhs2, rhs2 = eq2.split('=')
    eq1 = Eq(sympify(lhs1.replace(var1, 'a').replace(var2, 'b')),
             sympify(rhs1.replace(var1, 'a').replace(var2, 'b')))
    eq2 = Eq(sympify(lhs2.replace(var1, 'a').replace(var2, 'b')),
             sympify(rhs2.replace(var1, 'a').replace(var2, 'b')))
    soln = solve((eq1, eq2), (a, b))
    xval, yval = None, None
    for s in soln:
        if var1 in str(s):
            xval = s.evalf(subs={b: 0})
        elif var2 in str(s):
            yval = s.evalf(subs={a: 0})
        if xval is not None and yval is not None:
            break
    return xval, yval 

The above function take four inputs ex: find_variables(‘2x+4=2’, ‘x-y=3’, ‘x’, ‘y’) but it is not working the function has to standardise so it can work different formats of expressions

Example 1: find_variables('a^2 + b^2 = 25', 'a - b = 1', 'a', 'b')Example 2:find_variables('2x+4=2', 'x-y=3', 'x', 'y')Example 3:find_variables('3s+1=2', '2s-t=3', 's', 't')

Can anyone solve my problem or anyone having alternative solution to solve my problem.if any alternate packages or functions that solves my problem is appreciated

I tried everything but didn’t got any solution if you find any please help me

Answers:

Something like the following should work. Instead of defining the functions as strings you could define functions that return sympy equations.

import sympy as sp


# Define equations
def eq1(x, y):
    return sp.Eq(2*x + 1, y)

def eq2(x, y):
    return sp.Eq(3*y - x, 5)

# Solve the system of equations

def find_variables(eq1, eq2, v1, v2):
    x, y = sp.symbols(f"{v1} {v2}")
    sol = sp.solve((eq1(x,y), eq2(x,y)), (x, y))
    return sol[x], sol[y]

print(find_variables(eq1, eq2, "x", "y"))

Edit:

If you really want to pass in the functions as strings, you could do something like the following.

def find_variables(eq1, eq2, v1, v2):
    def eq1fn(x, y):
        lhs,rhs = eq1.replace(f"{v1}", "x").replace(f"{v2}", "y").split("=")
        return eval(f"sp.Eq({lhs}, {rhs})")
    def eq2fn(x, y):
        lhs,rhs = eq2.replace(f"{v1}", "x").replace(f"{v2}", "y").split("=")
        return eval(f"sp.Eq({lhs}, {rhs})")
    x, y = sp.symbols(f"{v1} {v2}")
    sol = sp.solve((eq1fn(x, y), eq2fn(x, y)), (x, y))
    return sol



print(find_variables("2*a + 1 = 2", "a-b=3", "a", "b"))

I would personally advise against using eval since defining functions is about as difficult in my opinion.

Answered By: KCQs

I’m not sure, but I think that you’re replacing var1 and var2 in the equations and then trying to solve for a and bwhich are variables containing var1 and var2 values. I’d try changing:

soln = solve((eq1, eq2), ('a', 'b'))
Answered By: user118799

This can be achieved using nonlinsolve from the sympy package:

from sympy import symbols, solve, sympify, Eq, nonlinsolve

def find_variables(eq1, eq2, var1, var2):
    x, y = symbols('x y')
    lhs1, rhs1 = eq1.split('=')
    lhs2, rhs2 = eq2.split('=')
    eq1 = Eq(sympify(lhs1.replace(var1, 'x').replace(var2, 'y')),
             sympify(rhs1.replace(var1, 'y').replace(var2, 'y')))
    eq2 = Eq(sympify(lhs2.replace(var1, 'x').replace(var2, 'y')),
             sympify(rhs2.replace(var1, 'x').replace(var2, 'y')))
    soln = list(nonlinsolve([eq1, eq2], (x, y)))
    xval = soln[0][0]
    yval = soln[0][0]
    return xval, yval 

Here are the results for your test cases:

find_variables('a^2 + b^2 = 25', 'a - b = 1', 'a', 'b') >> (-3, -3)  
find_variables('3*s+1=2', '2*s-t=3', 's', 't') >> (1/3, 1/3)  
find_variables('2*x+4=2', 'x-y=3', 'x', 'y') >> (-1, -1)
Answered By: darth baba
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.