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]:
In [6]:
grg = Matrix([g]).jacobian([x,y])
grg
Out[6]:
In [7]:
gls = grf - lam*grg
gls
Out[7]:
In [8]:
glsmenge = set(gls)
In [9]:
glsmenge.add(g)
glsmenge
Out[9]:
In [10]:
Lsg = solve(glsmenge)
Lsg
Out[10]:
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]:
In [13]:
werte = [f.subs(l) for l in reelle_lsg]
werte
Out[13]:
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');
In [17]:
x = Symbol('x')
f = 1/(1+x**2)
f
Out[17]:
In [18]:
wendestellen = solve(f.diff(x,2))
wendestellen
Out[18]:
In [19]:
werte = [f.subs(x, w) for w in wendestellen]
werte
Out[19]:
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);
In [23]:
x = Symbol('x')
y = Function('y')
dgl = Eq(y(x).diff(x), 2*y(x)+1)
dgl
Out[23]:
In [24]:
Lsg = dsolve(dgl)
Lsg
Out[24]:
In [25]:
print(Lsg)
In [26]:
Lsg.subs(C1, 3) # NameError
In [27]:
C1 = Symbol('C1')
In [28]:
Lsg.subs(C1, 3)
Out[28]:
In [29]:
f = Lsg.rhs
In [30]:
y0 = 0
In [31]:
ab = Eq(f.subs(x,0), y0)
ab
Out[31]:
In [32]:
Lsg_AWA = solve(ab)
f.subs(C1, Lsg_AWA[0])
Out[32]:
In [33]:
dgl = Eq(y(x).diff(x), y(x)-x**3+3*x-2)
dgl
Out[33]:
In [34]:
Lsg = dsolve(dgl).expand()
Lsg
Out[34]:
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]:
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');
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]:
In [41]:
Lsg = dsolve(dgl)
f = Lsg.rhs
f
Out[41]:
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]:
In [44]:
f_y0 = f.subs(C1, Lsg_AWA[0])
f_y0
Out[44]:
Probe
In [45]:
dgl.subs(y(x), f_y0).doit().simplify()
Out[45]:
In [46]:
f_y0.subs(x, 0) == y0
Out[46]:
In [47]:
f_y0.subs(x, 0).simplify()
Out[47]:
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');
Wo ist die Lösung definiert?
In [49]:
f
Out[49]:
In [50]:
denom(exp(f_y0))
Out[50]:
In [51]:
solve(denom(exp(f_y0)), x)
Out[51]:
In [52]:
solve(Eq(1-exp(-y0), -1))
Out[52]:
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');
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})[ $.
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]:
In [56]:
dsolve(dgl)
Out[56]:
In [57]:
tmp = dgl.lhs.subs(y(x), exp(lam*x)).doit()
tmp
Out[57]:
In [58]:
chi = (tmp * exp(-lam*x)).expand()
chi
Out[58]:
$\chi$ ist das charakteristische Polynom
In [59]:
Lsg = solve(chi, lam)
Lsg
Out[59]:
In [ ]:
In [ ]: