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
>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}