I am trying to solve a simple set of equations in sympy. Finding the solution manually is simple, but I want to do it with sympy to learn the tool.
from sympy import symbols,solve,Le,Eq
l,x = symbols('lamda x')
f0 = x**2+1
f1 = (x-2)*(x-4); feasible_set = Le(f1,0);
lagrange = f0 + l*f1
stationary_lagrangian = Eq(lagrange.diff(x),0)
solve([feasible_set,stationary_lagrangian])
The code above gives me the error NotImplementedError:
inequality has more than one symbol of interest.
.
Question 1: Why is this? The inequality only contains x
and not lamda
.
Question 2: Is it possible to solve the same problem in another way, using sympy?
The background of the problem, if you are interested
minimize (over x \in R)
x^2 + 1
subject to
(x-2)(x-4) <= 0
.. and then applying stationarity and primal feasibility from KKT conditions