from sympy import *
init_printing()

Solve for the variable

image.png

t=symbols("t")
expr=2/(t-4)+3/(t+4) + 8/((t-4)*(t+4))
tsol=solve(expr,t,dict=True)
tsol
$\displaystyle \left[ \left\{ t : - \frac{4}{5}\right\}\right]$

And just 'cause we can, let's verify the solution by plugging it into the original expression:

expr.subs(tsol[0])
$\displaystyle 0$

Solve for $y$ in terms of the other variables

image.png

x,y = symbols("x,y")
ysol=solve(Eq(1/2*x*y+y,3*x-2*y),y,dict=True)
ysol
$\displaystyle \left[ \left\{ y : \frac{6.0 x}{x + 6.0}\right\}\right]$
(1/2*x*y+y).subs(ysol[0]) - (3*x-2*y).subs(ysol[0])
$\displaystyle \frac{3.0 x^{2}}{x + 6.0} - 3 x + \frac{18.0 x}{x + 6.0}$
factor(_)
$\displaystyle 0$

Find the $x$ intercepts

image.png

ee=Eq(y,x**2+3*x+2)
ee
$\displaystyle y = x^{2} + 3 x + 2$
ee.subs(y,0)
$\displaystyle 0 = x^{2} + 3 x + 2$
solve(_,x)
$\displaystyle \left[ -2, \ -1\right]$

Multivariable optimization: Lagrange Multipliers

Minimize the function $f(x)=2x^2+y^2$ subject to the constraint $x^2+y^2=1$

x,y = symbols("x y")
h = symbols("h") # h is the lagrange multipler
f=2*x**2 + y**2
g=x**2+y**2
s=solve([Eq(f.diff(x),h*g.diff(x)),
         Eq(f.diff(y),h*g.diff(y)), 
         Eq(g,1)],[x,y,h],
            dict=True)
s
$\displaystyle \left[ \left\{ h : 1, \ x : 0, \ y : -1\right\}, \ \left\{ h : 1, \ x : 0, \ y : 1\right\}, \ \left\{ h : 2, \ x : -1, \ y : 0\right\}, \ \left\{ h : 2, \ x : 1, \ y : 0\right\}\right]$

We evaluate the objective function $f$ at each of the points identified by the Lagrang multiplier procedure and see that minimum value of $f$ occurs at $(0,1)$ and $(0,-1)$.

for i in range(len(s)):
  print(f.subs(s[i]))
1
1
2
2

Minimize the volume of a rectangular prism subject to the suface area constraint $2yz+2xz+2xy=12$

x,y,z,h = symbols("x y z h")
A=2*y*z+2*x*z+2*x*y
V=x*y*z
ss=solve( [Eq(V.diff(x),h*diff(A,x)),
           Eq(V.diff(y),h*diff(A,y)),
           Eq(V.diff(z),h*diff(A,z)),
           Eq(A,12)],
             dict=True)
ss
$\displaystyle \left[ \left\{ h : - \frac{\sqrt{2}}{4}, \ x : - \sqrt{2}, \ y : - \sqrt{2}, \ z : - \sqrt{2}\right\}, \ \left\{ h : \frac{\sqrt{2}}{4}, \ x : \sqrt{2}, \ y : \sqrt{2}, \ z : \sqrt{2}\right\}\right]$

Ignoring the solution with negative side lengths, we arrive at the minimum volume of the cube with sidelength $\sqrt{2}$:

V.subs(ss[1])
$\displaystyle 2 \sqrt{2}$

Minimize the surface area of a rectangular prism with fixed volume $V=xyz=1000$.

x,y,z,h = symbols("x y z h")
A=2*y*z+2*x*z+2*x*y
V=x*y*z
sss=solve( [Eq(V.diff(x),h*diff(A,x)),
           Eq(V.diff(y),h*diff(A,y)),
           Eq(V.diff(z),h*diff(A,z)),
           Eq(V,1000)],
             dict=True)
sss
$\displaystyle \left[ \left\{ h : \frac{5}{2}, \ x : 10, \ y : 10, \ z : 10\right\}, \ \left\{ h : - \frac{5}{4} - \frac{5 \sqrt{3} i}{4}, \ x : -5 - 5 \sqrt{3} i, \ y : -5 - 5 \sqrt{3} i, \ z : -5 - 5 \sqrt{3} i\right\}, \ \left\{ h : - \frac{5}{4} + \frac{5 \sqrt{3} i}{4}, \ x : -5 + 5 \sqrt{3} i, \ y : -5 + 5 \sqrt{3} i, \ z : -5 + 5 \sqrt{3} i\right\}\right]$

Ignoring the complex solutions, we find the minimum surface area 600 occurs for the cube of sidelength 10

A.subs(sss[0])
$\displaystyle 600$