Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

smypy not returning solution using solve

I’m attempting to calculate Lagrange multiplier using a question from this youtube video: https://www.youtube.com/watch?v=B0yzLgJ6wn8, however using sympy.solve does not return solutions

I’ve coded this in python

import sympy as sp

x, y = sp.var('x y')

# constraint
g1 = 2 * x + y - 100
# function
f = 2 * x + 2 * x * y + y

# lambda
g1L = sp.Symbol('g1L')

fin_eqs = []
for i in [x, y]:
    fin_eqs.append(
        sp.Eq(
            sp.simplify(f.diff(i)), 
            sp.simplify(
                g1.diff(i) * g1L
            )
        )
    )
fin_eqs.append(sp.Eq(sp.simplify(g1), 0))


for i in fin_eqs:
    print(i)
"""
prints
Eq(2*y + 2, 2*g1L)
Eq(2*x + 1, g1L)
Eq(2*x + y - 100, 0)
which I've confirmed is correct
"""


sp.solve(fin_eqs, x, y) # returns []

However, it does not work out that x = 25 and y = 50

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For some reason, to get result you need to omit symbols you’re solving your system of equations on.

sp.solve(fin_eqs) returns {g1L: 51, x: 25, y: 50}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading