Lektion 5


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

interaktiv


In [2]:
#%matplotlib notebook

für den Druck


In [3]:
%matplotlib inline

Graphen von Lösungsmengen


In [4]:
x = Symbol('x')
y = Symbol('y')

In [5]:
f1 = x**2+y**2
f2 = x - y
f1, f2


Out[5]:
$$\left ( x^{2} + y^{2}, \quad x - y\right )$$

Zeichne beide Nullstellenmengen in ein Bild


In [6]:
f1n = lambdify((x,y), f1)
f2n = lambdify((x,y), f2)

In [9]:
xn = np.linspace(-1.1, 1.1)
yn = xn
X, Y = np.meshgrid(xn, yn)
plt.contour(X, Y, f1n(X, Y), [1])
plt.contour(X, Y, f2n(X, Y), [0, .5], colors='green')
plt.axis('equal');



In [11]:
h = (x**2+y**2)**2 + 3*x**2*y - y**3
h


Out[11]:
$$3 x^{2} y - y^{3} + \left(x^{2} + y^{2}\right)^{2}$$

In [14]:
xn = np.linspace(-1, 1, 200)
yn = xn
hn = lambdify((x,y), h)
X, Y = np.meshgrid(xn, yn)
plt.contour(X, Y, hn(X,Y), [0])
plt.axis('equal');


Polynomgleichungen


In [15]:
x = Symbol('x')

In [16]:
Glg = x**5 + x + 7
Glg   # keine rechte Seite bedeutet "=0"


Out[16]:
$$x^{5} + x + 7$$

In [17]:
Lsg = solve(Glg)
Lsg


Out[17]:
$$\left [ \operatorname{CRootOf} {\left(x^{5} + x + 7, 0\right)}, \quad \operatorname{CRootOf} {\left(x^{5} + x + 7, 1\right)}, \quad \operatorname{CRootOf} {\left(x^{5} + x + 7, 2\right)}, \quad \operatorname{CRootOf} {\left(x^{5} + x + 7, 3\right)}, \quad \operatorname{CRootOf} {\left(x^{5} + x + 7, 4\right)}\right ]$$

Die Lösungen können nicht durch Wurzeln ausgedrückt werden. Fragen Sie Dr. Klopsch.

http://www.sagemath.org kann die Galoisgruppe ausrechnen.


In [18]:
[l.n() for l in Lsg]


Out[18]:
$$\left [ -1.41081385105958, \quad -0.508469408973023 - 1.3686164883299 i, \quad -0.508469408973023 + 1.3686164883299 i, \quad 1.21387633450281 - 0.924188110922051 i, \quad 1.21387633450281 + 0.924188110922051 i\right ]$$

sympy kann damit leider nur numerisch weiterarbeiten


In [19]:
f = x**3 - 23*x**2 + 1
Glg = Eq(f, 0)
Lsg = solve(Glg)
Lsg


Out[19]:
$$\left [ \frac{23}{3} + \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{24307}{54} + \frac{\sqrt{145923} i}{18}} + \frac{529}{9 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{24307}{54} + \frac{\sqrt{145923} i}{18}}}, \quad \frac{23}{3} + \frac{529}{9 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{24307}{54} + \frac{\sqrt{145923} i}{18}}} + \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{24307}{54} + \frac{\sqrt{145923} i}{18}}, \quad \frac{23}{3} + \frac{529}{9 \sqrt[3]{\frac{24307}{54} + \frac{\sqrt{145923} i}{18}}} + \sqrt[3]{\frac{24307}{54} + \frac{\sqrt{145923} i}{18}}\right ]$$

In [20]:
[l.n() for l in Lsg]


Out[20]:
$$\left [ 0.209470462659866 + 5.0 \cdot 10^{-20} i, \quad -0.207579792666746 + 2.0 \cdot 10^{-22} i, \quad 22.9981093300069 - 8.0 \cdot 10^{-22} i\right ]$$

In [23]:
fn = lambdify(x, f, 'numpy')
xn = np.linspace(-5, 24, 1200)
plt.plot(xn, fn(xn))
plt.grid()
plt.axis(ymin=-50, ymax=10)


Out[23]:
$$\left ( -5.0, \quad 25.0, \quad -50, \quad 10\right )$$

In [24]:
rlsg = roots(Glg, trig=True)
rlsg


Out[24]:
$$\left \{ \frac{23}{3} + \frac{46}{3} \cos{\left (\frac{1}{3} \operatorname{acos}{\left (\frac{24307}{24334} \right )} \right )} : 1, \quad - \frac{46}{3} \sin{\left (- \frac{1}{3} \operatorname{acos}{\left (\frac{24307}{24334} \right )} + \frac{\pi}{6} \right )} + \frac{23}{3} : 1, \quad - \frac{46}{3} \cos{\left (- \frac{1}{3} \operatorname{acos}{\left (\frac{24307}{24334} \right )} + \frac{\pi}{3} \right )} + \frac{23}{3} : 1\right \}$$

In [25]:
print([r.n() for r in rlsg])
print([l.n() for l in Lsg])


[0.209470462659866, 22.9981093300069, -0.207579792666746]
[0.209470462659866 + 0.e-19*I, -0.207579792666746 + 0.e-22*I, 22.9981093300069 - 0.e-21*I]

In [28]:
f.subs(x, list(rlsg)[0]).expand(trig=True).trigsimp()


Out[28]:
$$0$$

In [29]:
[f.subs(x, x0).expand(trig=True).trigsimp() for x0 in rlsg]


Out[29]:
$$\left [ 0, \quad 0, \quad 0\right ]$$

In [30]:
g = 100*(x-1)*(x**2+1)*(x-2) + 1
Lsg = solve(g)
Lsg[2]


Out[30]:
$$- \frac{1}{2} \sqrt{- 2 \sqrt[3]{\frac{\sqrt{3705593}}{8000} + \frac{401}{1600}} - \frac{17}{50 \sqrt[3]{\frac{\sqrt{3705593}}{8000} + \frac{401}{1600}}} + \frac{1}{2} + \frac{15}{4 \sqrt{\frac{1}{4} + \frac{17}{50 \sqrt[3]{\frac{\sqrt{3705593}}{8000} + \frac{401}{1600}}} + 2 \sqrt[3]{\frac{\sqrt{3705593}}{8000} + \frac{401}{1600}}}}} + \frac{3}{4} + \frac{1}{2} \sqrt{\frac{1}{4} + \frac{17}{50 \sqrt[3]{\frac{\sqrt{3705593}}{8000} + \frac{401}{1600}}} + 2 \sqrt[3]{\frac{\sqrt{3705593}}{8000} + \frac{401}{1600}}}$$

In [31]:
Lsg[2].n()


Out[31]:
$$1.00500006281487$$

In [32]:
solve(g, quartics=False)


Out[32]:
$$\left [ \operatorname{CRootOf} {\left(100 x^{4} - 300 x^{3} + 300 x^{2} - 300 x + 201, 0\right)}, \quad \operatorname{CRootOf} {\left(100 x^{4} - 300 x^{3} + 300 x^{2} - 300 x + 201, 1\right)}, \quad \operatorname{CRootOf} {\left(100 x^{4} - 300 x^{3} + 300 x^{2} - 300 x + 201, 2\right)}, \quad \operatorname{CRootOf} {\left(100 x^{4} - 300 x^{3} + 300 x^{2} - 300 x + 201, 3\right)}\right ]$$

Numerische Lösung von Gleichungen


In [33]:
x = Symbol('x')

In [36]:
xn = np.linspace(0, 4*np.pi, 850)
yn = np.tan(xn)
yn[yn>35] = np.nan
yn[yn<-10] = np.nan
plt.plot(xn, yn)
plt.plot(xn, xn)
plt.axis(ymin=-5, ymax=25);


/home/braun/miniconda3/lib/python3.5/site-packages/ipykernel/__main__.py:4: RuntimeWarning: invalid value encountered in less

In [38]:
solve(tan(x)-x) #gibt NotImplementedError


---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-38-b4a633f59064> in <module>()
----> 1 solve(tan(x)-x) #gibt NotImplementedError

/home/braun/miniconda3/lib/python3.5/site-packages/sympy/solvers/solvers.py in solve(f, *symbols, **flags)
   1051     ###########################################################################
   1052     if bare_f:
-> 1053         solution = _solve(f[0], *symbols, **flags)
   1054     else:
   1055         solution = _solve_system(f, symbols, **flags)

/home/braun/miniconda3/lib/python3.5/site-packages/sympy/solvers/solvers.py in _solve(f, *symbols, **flags)
   1617 
   1618     if result is False:
-> 1619         raise NotImplementedError('\n'.join([msg, not_impl_msg % f]))
   1620 
   1621     if flags.get('simplify', True):

NotImplementedError: multiple generators [x, tan(x)]
No algorithms are implemented to solve equation -x + tan(x)

In [39]:
nsolve(tan(x)-x, 4)


Out[39]:
mpf('-0.00051084676234878935')

In [40]:
x0 = nsolve(tan(x)-x, (np.pi, 1.499*np.pi), solver='anderson')   # ridder und bisect sind auch möglich
x0


Out[40]:
mpf('4.4934094579090642')

In [41]:
float(x0)


Out[41]:
$$4.493409457909064$$

Parameterabhängige Gleichungen


In [42]:
x = Symbol('x')
a = Symbol('a')

In [43]:
Glg = Eq(exp(-a*x), 3*x**a)
Glg


Out[43]:
$$e^{- a x} = 3 x^{a}$$

In [44]:
solve(Glg)


Out[44]:
$$\left [ \left \{ a : - \frac{\log{\left (3 \right )}}{x + \log{\left (x \right )}}\right \}\right ]$$

In [45]:
Lsg = solve(Glg, x)
Lsg


Out[45]:
$$\left [ \operatorname{LambertW}{\left (3^{- \frac{1}{a}} \right )}\right ]$$

LambertW(a) ist die Lösung von $xe^x=a$.


In [46]:
solve(x*exp(x)-a, x)


Out[46]:
$$\left [ \operatorname{LambertW}{\left (a \right )}\right ]$$

In [47]:
f = Lsg[0]
f.subs(a, 1).n()


Out[47]:
$$0.257627653049737$$

Warum kann $f$ nicht lambdifiziert werden?


In [48]:
xn = np.linspace(.01, 5)
yn = [f.subs(a, xx).n() for xx in xn]

In [49]:
plt.plot(xn, yn);


Gibt es einen endlichen Grenzwert für $a\to\infty$?


In [50]:
L = Limit(f, a, oo)
L


Out[50]:
$$\lim_{a \to \infty} \operatorname{LambertW}{\left (3^{- \frac{1}{a}} \right )}$$

In [51]:
L.doit()


Out[51]:
$$\operatorname{LambertW}{\left (1 \right )}$$

In [52]:
LambertW(1.)


Out[52]:
$$0.567143290409784$$

Ist die Differenz zwischen $f$ und dem Grenzwert (uneigentlich Riemann-)integriebar?

Reihenenticklungen


In [53]:
sin(x).series(x, 0, 14)


Out[53]:
$$x - \frac{x^{3}}{6} + \frac{x^{5}}{120} - \frac{x^{7}}{5040} + \frac{x^{9}}{362880} - \frac{x^{11}}{39916800} + \frac{x^{13}}{6227020800} + \mathcal{O}\left(x^{14}\right)$$

In [54]:
reihe = f.series(a, oo, 3)
reihe


Out[54]:
$$\frac{1}{a^{2}} \left(- \frac{\log^{2}{\left (3 \right )} \operatorname{LambertW}^{2}{\left (1 \right )}}{2 \operatorname{LambertW}^{3}{\left (1 \right )} + 6 \operatorname{LambertW}^{2}{\left (1 \right )} + 2 + 6 \operatorname{LambertW}{\left (1 \right )}} + \frac{\log^{2}{\left (3 \right )} \operatorname{LambertW}{\left (1 \right )}}{2 \operatorname{LambertW}^{2}{\left (1 \right )} + 2 + 4 \operatorname{LambertW}{\left (1 \right )}}\right) - \frac{\log{\left (3 \right )} \operatorname{LambertW}{\left (1 \right )}}{a \left(\operatorname{LambertW}{\left (1 \right )} + 1\right)} + \operatorname{LambertW}{\left (1 \right )} + \mathcal{O}\left(\frac{1}{a^{3}}; a\rightarrow\infty\right)$$

Die Differenz ist asymptotisch proportional zu $-\frac1a$, also nicht integrierbar.


In [55]:
reihe + O(a**(-2), (a, oo))


Out[55]:
$$- \frac{\log{\left (3 \right )} \operatorname{LambertW}{\left (1 \right )}}{a \left(\operatorname{LambertW}{\left (1 \right )} + 1\right)} + \operatorname{LambertW}{\left (1 \right )} + \mathcal{O}\left(\frac{1}{a^{2}}; a\rightarrow\infty\right)$$

In [56]:
reihe.removeO()


Out[56]:
$$\operatorname{LambertW}{\left (1 \right )} - \frac{\log{\left (3 \right )} \operatorname{LambertW}{\left (1 \right )}}{a \left(\operatorname{LambertW}{\left (1 \right )} + 1\right)} + \frac{1}{a^{2}} \left(- \frac{\log^{2}{\left (3 \right )} \operatorname{LambertW}^{2}{\left (1 \right )}}{2 \operatorname{LambertW}^{3}{\left (1 \right )} + 6 \operatorname{LambertW}^{2}{\left (1 \right )} + 2 + 6 \operatorname{LambertW}{\left (1 \right )}} + \frac{\log^{2}{\left (3 \right )} \operatorname{LambertW}{\left (1 \right )}}{2 \operatorname{LambertW}^{2}{\left (1 \right )} + 2 + 4 \operatorname{LambertW}{\left (1 \right )}}\right)$$

In [ ]: