Lektion 11

Extrema unter Nebenbedingungen


In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sympy import *
init_printing()

In [2]:
x = Symbol('x')
y = Symbol('y')
lam = Symbol('lambda')

In [3]:
g = x**4 + y**4 - 1

In [4]:
f = x**2 + y**2

Gesucht: Maximum von $f$ unter der Nebenbedingung $g=0$


In [5]:
grf = Matrix([f]).jacobian([x,y])
grf


Out[5]:
$$\left[\begin{matrix}2 x & 2 y\end{matrix}\right]$$

In [6]:
grg = Matrix([g]).jacobian([x,y])
grg


Out[6]:
$$\left[\begin{matrix}4 x^{3} & 4 y^{3}\end{matrix}\right]$$

In [7]:
gls = grf - lam*grg
gls


Out[7]:
$$\left[\begin{matrix}- 4 \lambda x^{3} + 2 x & - 4 \lambda y^{3} + 2 y\end{matrix}\right]$$

In [8]:
glsmenge = set(gls)

In [9]:
glsmenge.add(g)
glsmenge


Out[9]:
$$\left\{- 4 \lambda x^{3} + 2 x, - 4 \lambda y^{3} + 2 y, x^{4} + y^{4} - 1\right\}$$

In [10]:
Lsg = solve(glsmenge)
Lsg


Out[10]:
$$\left [ \left \{ \lambda : - \frac{1}{2}, \quad x : 0, \quad y : - i\right \}, \quad \left \{ \lambda : - \frac{1}{2}, \quad x : 0, \quad y : i\right \}, \quad \left \{ \lambda : - \frac{1}{2}, \quad x : - i, \quad y : 0\right \}, \quad \left \{ \lambda : - \frac{1}{2}, \quad x : i, \quad y : 0\right \}, \quad \left \{ \lambda : \frac{1}{2}, \quad x : -1, \quad y : 0\right \}, \quad \left \{ \lambda : \frac{1}{2}, \quad x : 0, \quad y : -1\right \}, \quad \left \{ \lambda : \frac{1}{2}, \quad x : 0, \quad y : 1\right \}, \quad \left \{ \lambda : \frac{1}{2}, \quad x : 1, \quad y : 0\right \}, \quad \left \{ \lambda : - \frac{\sqrt{2}}{2}, \quad x : - \frac{2^{\frac{3}{4}} i}{2}, \quad y : - \frac{2^{\frac{3}{4}} i}{2}\right \}, \quad \left \{ \lambda : - \frac{\sqrt{2}}{2}, \quad x : - \frac{2^{\frac{3}{4}} i}{2}, \quad y : \frac{2^{\frac{3}{4}} i}{2}\right \}, \quad \left \{ \lambda : - \frac{\sqrt{2}}{2}, \quad x : \frac{2^{\frac{3}{4}} i}{2}, \quad y : - \frac{2^{\frac{3}{4}} i}{2}\right \}, \quad \left \{ \lambda : - \frac{\sqrt{2}}{2}, \quad x : \frac{2^{\frac{3}{4}} i}{2}, \quad y : \frac{2^{\frac{3}{4}} i}{2}\right \}, \quad \left \{ \lambda : \frac{\sqrt{2}}{2}, \quad x : - \frac{2^{\frac{3}{4}}}{2}, \quad y : - \frac{2^{\frac{3}{4}}}{2}\right \}, \quad \left \{ \lambda : \frac{\sqrt{2}}{2}, \quad x : - \frac{2^{\frac{3}{4}}}{2}, \quad y : \frac{2^{\frac{3}{4}}}{2}\right \}, \quad \left \{ \lambda : \frac{\sqrt{2}}{2}, \quad x : \frac{2^{\frac{3}{4}}}{2}, \quad y : - \frac{2^{\frac{3}{4}}}{2}\right \}, \quad \left \{ \lambda : \frac{\sqrt{2}}{2}, \quad x : \frac{2^{\frac{3}{4}}}{2}, \quad y : \frac{2^{\frac{3}{4}}}{2}\right \}\right ]$$

In [11]:
def test_real(l):
    x0 = x.subs(l)
    y0 = y.subs(l)
    return x0.is_real and y0.is_real

In [12]:
reelle_lsg = [l for l in Lsg if test_real(l)]
reelle_lsg


Out[12]:
$$\left [ \left \{ \lambda : \frac{1}{2}, \quad x : -1, \quad y : 0\right \}, \quad \left \{ \lambda : \frac{1}{2}, \quad x : 0, \quad y : -1\right \}, \quad \left \{ \lambda : \frac{1}{2}, \quad x : 0, \quad y : 1\right \}, \quad \left \{ \lambda : \frac{1}{2}, \quad x : 1, \quad y : 0\right \}, \quad \left \{ \lambda : \frac{\sqrt{2}}{2}, \quad x : - \frac{2^{\frac{3}{4}}}{2}, \quad y : - \frac{2^{\frac{3}{4}}}{2}\right \}, \quad \left \{ \lambda : \frac{\sqrt{2}}{2}, \quad x : - \frac{2^{\frac{3}{4}}}{2}, \quad y : \frac{2^{\frac{3}{4}}}{2}\right \}, \quad \left \{ \lambda : \frac{\sqrt{2}}{2}, \quad x : \frac{2^{\frac{3}{4}}}{2}, \quad y : - \frac{2^{\frac{3}{4}}}{2}\right \}, \quad \left \{ \lambda : \frac{\sqrt{2}}{2}, \quad x : \frac{2^{\frac{3}{4}}}{2}, \quad y : \frac{2^{\frac{3}{4}}}{2}\right \}\right ]$$

In [13]:
werte = [f.subs(l) for l in reelle_lsg]
werte


Out[13]:
$$\left [ 1, \quad 1, \quad 1, \quad 1, \quad \sqrt{2}, \quad \sqrt{2}, \quad \sqrt{2}, \quad \sqrt{2}\right ]$$

In [14]:
fn = lambdify((x,y), f, 'numpy')
gn = lambdify((x,y), g, 'numpy')

In [15]:
x = np.linspace(-1.5, 1.5)
y = np.linspace(-1.5, 1.5)
X, Y = np.meshgrid(x, y)
Wf = fn(X, Y)
Wg = gn(X, Y)

In [16]:
plt.contour(X, Y, Wg, levels=[0])
plt.contour(X, Y, Wf, levels=[1, np.sqrt(2)], colors=['red', 'green'])
plt.axis('image');


Plotverschönerung


In [17]:
x = Symbol('x')
f = 1/(1+x**2)
f


Out[17]:
$$\frac{1}{x^{2} + 1}$$

In [18]:
wendestellen = solve(f.diff(x,2))
wendestellen


Out[18]:
$$\left [ - \frac{\sqrt{3}}{3}, \quad \frac{\sqrt{3}}{3}\right ]$$

In [19]:
werte = [f.subs(x, w) for w in wendestellen]
werte


Out[19]:
$$\left [ \frac{3}{4}, \quad \frac{3}{4}\right ]$$

In [20]:
xn = np.linspace(-3.5, 3.5, 100)
fn = lambdify(x, f)
wn = fn(xn)

In [21]:
props = {}
props['arrowstyle'] = '-|>'

In [22]:
plt.plot(xn, wn)
wx = wendestellen[0].n()
wy = fn(wx)
plt.annotate("Wendepunkt", (wx, wy), (wx-2.5, wy+.03),
            arrowprops=props);


Differentialgleichungen


In [23]:
x = Symbol('x')
y = Function('y')
dgl = Eq(y(x).diff(x), 2*y(x)+1)
dgl


Out[23]:
$$\frac{d}{d x} y{\left (x \right )} = 2 y{\left (x \right )} + 1$$

In [24]:
Lsg = dsolve(dgl)
Lsg


Out[24]:
$$y{\left (x \right )} = \frac{C_{1}}{2} e^{2 x} - \frac{1}{2}$$

In [25]:
print(Lsg)


Eq(y(x), C1*exp(2*x)/2 - 1/2)

In [26]:
Lsg.subs(C1, 3)   # NameError


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-26-40e6ce7cd214> in <module>()
----> 1 Lsg.subs(C1, 3)   # NameError

NameError: name 'C1' is not defined

In [27]:
C1 = Symbol('C1')

In [28]:
Lsg.subs(C1, 3)


Out[28]:
$$y{\left (x \right )} = \frac{3}{2} e^{2 x} - \frac{1}{2}$$

In [29]:
f = Lsg.rhs

In [30]:
y0 = 0

In [31]:
ab = Eq(f.subs(x,0), y0)
ab


Out[31]:
$$\frac{C_{1}}{2} - \frac{1}{2} = 0$$

In [32]:
Lsg_AWA = solve(ab)
f.subs(C1, Lsg_AWA[0])


Out[32]:
$$\frac{e^{2 x}}{2} - \frac{1}{2}$$

In [33]:
dgl = Eq(y(x).diff(x), y(x)-x**3+3*x-2)
dgl


Out[33]:
$$\frac{d}{d x} y{\left (x \right )} = - x^{3} + 3 x + y{\left (x \right )} - 2$$

In [34]:
Lsg = dsolve(dgl).expand()
Lsg


Out[34]:
$$y{\left (x \right )} = C_{1} e^{x} + x^{3} + 3 x^{2} + 3 x + 5$$

In [35]:
f = Lsg.rhs

In [36]:
xn = np.linspace(-1.5,2)
for y0 in [S(1)/2, 1, S(3)/2]:
    ab = Eq(f.subs(x, 0), y0)
    Lsg_AWA = solve(ab)
    fn = lambdify(x, f.subs(C1, Lsg_AWA[0]), 'numpy')
    plt.plot(xn, fn(xn), label=y0)
plt.legend(loc='lower left');



In [37]:
xq = np.linspace(-1.4, 1.8, 13)
yq = np.linspace(-2.5, 5.5, 11)
X, Y = np.meshgrid(xq, yq)
vf = np.array([dgl.rhs.subs({x: xx, y(x): yy})
               for yy in yq for xx in xq]).reshape(11, 13).astype(float)
X.shape, vf.shape


Out[37]:
$$\left ( \left ( 11, \quad 13\right ), \quad \left ( 11, \quad 13\right )\right )$$

In [38]:
plt.quiver(X, Y, np.ones_like(X), vf, angles='xy')
for y0 in [S(1)/2, 1, S(3)/2]:
    ab = Eq(f.subs(x, 0), y0)
    Lsg_AWA = solve(ab)
    fn = lambdify(x, f.subs(C1, Lsg_AWA[0]), 'numpy')
    plt.plot(xn, fn(xn), label=y0)
plt.legend(loc='lower left');


Definitionsbereiche


In [39]:
y = Function('y')
y0 = Symbol('y_0', real=True)
x = Symbol('x')

In [40]:
dgl = Eq(y(x).diff(x), exp(y(x))*sin(x))
dgl


Out[40]:
$$\frac{d}{d x} y{\left (x \right )} = e^{y{\left (x \right )}} \sin{\left (x \right )}$$

In [41]:
Lsg = dsolve(dgl)
f = Lsg.rhs
f


Out[41]:
$$\log{\left (- \frac{1}{C_{1} - \cos{\left (x \right )}} \right )}$$

In [42]:
xq = np.linspace(-3.5, 9.5, 13)
yq = np.linspace(-.8, 1.95, 12)
X, Y = np.meshgrid(xq, yq)
vf = np.array([dgl.rhs.subs({x: xx, y(x): yy})
               for yy in yq for xx in xq]).reshape(12, 13).astype(float)

In [43]:
xn = np.linspace(-2.2, 9, 200)
ab = Eq(f.subs(x, 0), y0)
Lsg_AWA = solve(ab, C1)
Lsg_AWA


Out[43]:
$$\left [ 1 - e^{- y_{0}}\right ]$$

In [44]:
f_y0 = f.subs(C1, Lsg_AWA[0])
f_y0


Out[44]:
$$\log{\left (- \frac{1}{- \cos{\left (x \right )} + 1 - e^{- y_{0}}} \right )}$$

Probe


In [45]:
dgl.subs(y(x), f_y0).doit().simplify()


Out[45]:
$$\mathrm{True}$$

In [46]:
f_y0.subs(x, 0) == y0


Out[46]:
True

In [47]:
f_y0.subs(x, 0).simplify()


Out[47]:
$$y_{0}$$

In [48]:
fn = lambdify(x, f_y0.subs(y0, -.5), 'numpy')
wn = fn(xn)
wn[wn>2] = np.nan
plt.plot(xn, wn)
plt.quiver(X, Y, np.ones_like(X), vf, angles='xy');


/home/braun/miniconda3/envs/WS1617/lib/python3.5/site-packages/numpy/__init__.py:1: RuntimeWarning: invalid value encountered in log
  """

Wo ist die Lösung definiert?


In [49]:
f


Out[49]:
$$\log{\left (- \frac{1}{C_{1} - \cos{\left (x \right )}} \right )}$$

In [50]:
denom(exp(f_y0))


Out[50]:
$$- \cos{\left (x \right )} + 1 - e^{- y_{0}}$$

In [51]:
solve(denom(exp(f_y0)), x)


Out[51]:
$$\left [ - \operatorname{acos}{\left (1 - e^{- y_{0}} \right )} + 2 \pi, \quad \operatorname{acos}{\left (1 - e^{- y_{0}} \right )}\right ]$$

In [52]:
solve(Eq(1-exp(-y0), -1))


Out[52]:
$$\left [ - \log{\left (2 \right )}\right ]$$

In [53]:
plt.quiver(X, Y, np.ones_like(X), vf, angles='xy')
for yy in [-1, -log(2), -S(1)/2]:
    fn = lambdify(x, f_y0.subs(y0, yy), 'numpy')
    wn = fn(xn)
    wn[wn>2] = np.nan
    plt.plot(xn, wn, label=yy)
plt.legend(loc='lower left');


/home/braun/miniconda3/envs/WS1617/lib/python3.5/site-packages/numpy/__init__.py:1: RuntimeWarning: invalid value encountered in log
  """

Die Lösung der Anfangswertaufgabe $$ y' = e^y \sin(x), y(0) = y_0 $$ ist für $y_0 < -\ln(2)$ auf ganz $\mathbb R$ definiert, sonst nur auf dem Intervall $]-\arccos(1-e^{-y_0}), \arccos(1-e^{-y_0})[ $.

Höhere Ordnung


In [54]:
a = Symbol('a')
lam = Symbol('lambda')

In [55]:
dgl = Eq(y(x).diff(x,2) + a*diff(y(x),x) + y(x), 0)
dgl


Out[55]:
$$a \frac{d}{d x} y{\left (x \right )} + y{\left (x \right )} + \frac{d^{2}}{d x^{2}} y{\left (x \right )} = 0$$

In [56]:
dsolve(dgl)


Out[56]:
$$y{\left (x \right )} = C_{1} e^{\frac{x}{2} \left(- a - \sqrt{a^{2} - 4}\right)} + C_{2} e^{\frac{x}{2} \left(- a + \sqrt{a^{2} - 4}\right)}$$

In [57]:
tmp = dgl.lhs.subs(y(x), exp(lam*x)).doit()
tmp


Out[57]:
$$a \lambda e^{\lambda x} + \lambda^{2} e^{\lambda x} + e^{\lambda x}$$

In [58]:
chi = (tmp * exp(-lam*x)).expand()
chi


Out[58]:
$$a \lambda + \lambda^{2} + 1$$

$\chi$ ist das charakteristische Polynom


In [59]:
Lsg = solve(chi, lam)
Lsg


Out[59]:
$$\left [ - \frac{a}{2} - \frac{1}{2} \sqrt{a^{2} - 4}, \quad - \frac{a}{2} + \frac{1}{2} \sqrt{a^{2} - 4}\right ]$$

In [ ]:


In [ ]: