Lektion 4


In [1]:
from sympy import *
init_printing()

interaktiv


In [2]:
#%matplotlib notebook

für den Druck


In [3]:
%matplotlib inline

3D Graphen in matplotlib


In [4]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

In [5]:
x = Symbol('x')
y = Symbol('y')
f = cos(sqrt(x**2+y**2))
f


Out[5]:
$$\cos{\left (\sqrt{x^{2} + y^{2}} \right )}$$

In [6]:
fn = lambdify((x,y), f, 'numpy')   # probehalber 'numpy' weglassen         
f.subs(x,1.).subs(y,2.), fn(1,2)


Out[6]:
$$\left ( -0.617272876457167, \quad -0.617272876457\right )$$

In [7]:
xn = np.linspace(-3*np.pi, 3*np.pi)
yn = np.linspace(-3*np.pi, 3*np.pi)
X, Y = np.meshgrid(xn, yn)
X.shape


Out[7]:
$$\left ( 50, \quad 50\right )$$

In [8]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Z = fn(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, 
                cmap=plt.cm.coolwarm, linewidth=0);



In [9]:
xn = np.linspace(-3*np.pi, 3*np.pi, 200)
yn = np.linspace(-3*np.pi, 3*np.pi, 200)
X, Y = np.meshgrid(xn, yn)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Z = fn(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, 
                cmap=plt.cm.coolwarm, linewidth=0, 
                alpha=.35);


Wenn's schnell gehen soll


In [10]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Z = fn(X, Y)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10);



In [11]:
fig = plt.figure()  
ax = fig.add_subplot(111, projection='3d')
Z = fn(X, Y)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
ax.view_init(84, 23)                            # <----------



In [12]:
xn = np.linspace(-3*np.pi, 3*np.pi, 200)
yn = np.linspace(-3*np.pi, 3*np.pi, 200)
X, Y = np.meshgrid(xn, yn)
fig = plt.figure()  
ax = fig.add_subplot(111, projection='3d')
Z = fn(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, 
                cmap=plt.cm.coolwarm, linewidth=0)
ax.view_init(80, 23)
ax.set_zticks([]);


Beschränkung der Höhe


In [13]:
g = abs(tan(x+I*y))
gn = lambdify((x,y), g, 'numpy')
gn(1, 1)


Out[13]:
$$1.11747002071$$

In [14]:
xn = np.linspace(-3*np.pi, 3*np.pi, 300)
yn = xn
X, Y = np.meshgrid(xn, yn)
Z = np.minimum(gn(X, Y), 5)
Z[Z>5] = np.nan

In [15]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X, Y, Z)
ax.view_init(15,-120)


Der np.nan-Trick stört die automatische Farbgebung


In [16]:
from matplotlib.colors import Normalize

In [17]:
norm = Normalize(0, 2)

In [18]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap=plt.cm.viridis, linewidth=0,   # Dark2 ist besonders schlecht 
                rstride=1, cstride=1, norm=norm)
ax.view_init(15,-120)


Listen, Tupel und Mengen


In [19]:
a, b, c, A, B, C, x, y, z = symbols('a b c A B C x y z')

In [20]:
liste = [a, b, c, a]
liste


Out[20]:
$$\left [ a, \quad b, \quad c, \quad a\right ]$$

In [21]:
menge = {A, B, A, C}
menge


Out[21]:
$$\left\{A, B, C\right\}$$

In [22]:
tupel = (x, y, z, y)
tupel


Out[22]:
$$\left ( x, \quad y, \quad z, \quad y\right )$$

In [23]:
tupel[0]


Out[23]:
$$x$$

In [24]:
liste[0]


Out[24]:
$$a$$

In [25]:
liste[0] = c

In [26]:
menge[0]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-5e5f369d8550> in <module>()
----> 1 menge[0]

TypeError: 'set' object does not support indexing

In [27]:
tupel[0] = y


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-7af56ebfe5f4> in <module>()
----> 1 tupel[0] = y

TypeError: 'tuple' object does not support item assignment

Umwandlungen (cast-Operatoren)


In [28]:
set(liste)


Out[28]:
$$\left\{a, b, c\right\}$$

In [29]:
list(menge)


Out[29]:
$$\left [ B, \quad A, \quad C\right ]$$

In [30]:
tuple(liste)


Out[30]:
$$\left ( c, \quad b, \quad c, \quad a\right )$$

In [31]:
list(tupel)


Out[31]:
$$\left [ x, \quad y, \quad z, \quad y\right ]$$

In [32]:
tuple(menge)


Out[32]:
$$\left ( B, \quad A, \quad C\right )$$

In [33]:
set(tupel)


Out[33]:
$$\left\{x, y, z\right\}$$

Dictionaries


In [34]:
d = {}
d[1] = 'eins'
d[2] = 'zwei'
d[3] = 'drei'
d


Out[34]:
{1: 'eins', 2: 'zwei', 3: 'drei'}

In [35]:
d[3]


Out[35]:
'drei'

In [36]:
d[4]


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-36-4923c2483f0b> in <module>()
----> 1 d[4]

KeyError: 4

List and dictionary comprehension

Listenerzeugung und Erzeugung von Dictionaries (Abbildungstypen)


In [37]:
eins_bis_fuenf = np.arange(1, 6)
eins_bis_fuenf**2


Out[37]:
array([ 1,  4,  9, 16, 25])

In [38]:
liste_quadrate = [j**2 for j in np.arange(1,6)]
liste_quadrate


Out[38]:
[1, 4, 9, 16, 25]

In [39]:
quadrate = { d[j]: j**2 for j in np.arange(1, 4)}
quadrate['zwei']


Out[39]:
4

Arrays


In [40]:
import numpy as np

In [41]:
np.array(liste)


Out[41]:
array([c, b, c, a], dtype=object)

In [42]:
np.array([1,2,3])


Out[42]:
array([1, 2, 3])

In [43]:
np.array([1,2,3.])


Out[43]:
array([ 1.,  2.,  3.])

In [44]:
np.array([1, 2, 1j])


Out[44]:
array([ 1.+0.j,  2.+0.j,  0.+1.j])

In [45]:
eins_bis_fuenf**2


Out[45]:
array([ 1,  4,  9, 16, 25])

In [46]:
np.maximum(eins_bis_fuenf+10, eins_bis_fuenf**2)


Out[46]:
array([11, 12, 13, 16, 25])

Lösen von Gleichungen


In [47]:
Glg = (x-1)**2 == 4
solve(Glg)   # sytaktisch richtig, aber inhaltlich sinnlos


Out[47]:
$$\mathrm{False}$$

In [48]:
Glg = Eq((x-1)**2, 4)
Glg


Out[48]:
$$\left(x - 1\right)^{2} = 4$$

In [49]:
solve(Glg)


Out[49]:
$$\left [ -1, \quad 3\right ]$$

In [50]:
Lsg = solve({Glg})
Lsg


Out[50]:
$$\left [ \left \{ x : -1\right \}, \quad \left \{ x : 3\right \}\right ]$$

In [51]:
Lsg[0]


Out[51]:
$$\left \{ x : -1\right \}$$

In [52]:
Lsg[0][x]


Out[52]:
$$-1$$

In [53]:
Glg.subs(x, Lsg[0][x])


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

In [54]:
Glg.subs(Lsg[0])


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

In [55]:
Glg1 = Eq(x**2+y**2, 1)
Glg2 = Eq(x, y)
Gls = {Glg1, Glg2}
Gls


Out[55]:
$$\left\{x = y, x^{2} + y^{2} = 1\right\}$$

In [56]:
Lsg = solve(Gls)
Lsg


Out[56]:
$$\left [ \left \{ x : - \frac{\sqrt{2}}{2}, \quad y : - \frac{\sqrt{2}}{2}\right \}, \quad \left \{ x : \frac{\sqrt{2}}{2}, \quad y : \frac{\sqrt{2}}{2}\right \}\right ]$$

In [57]:
[Glg.subs(l) for l in Lsg for Glg in Gls]


Out[57]:
$$\left [ \mathrm{True}, \quad \mathrm{True}, \quad \mathrm{True}, \quad \mathrm{True}\right ]$$

In [58]:
Glg = Eq(cos(x/2), sin(x/2))
Glg


Out[58]:
$$\cos{\left (\frac{x}{2} \right )} = \sin{\left (\frac{x}{2} \right )}$$

In [59]:
solve(Glg)


Out[59]:
$$\left [ - \frac{3 \pi}{2}, \quad \frac{\pi}{2}\right ]$$

In [60]:
Glg = Eq(cos(x), sin(2*x))
Glg


Out[60]:
$$\cos{\left (x \right )} = \sin{\left (2 x \right )}$$

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


Out[61]:
$$\left [ - \frac{\pi}{2}, \quad \frac{\pi}{2}, \quad - i \log{\left (- \frac{\sqrt{3}}{2} + \frac{i}{2} \right )}, \quad - i \log{\left (\frac{\sqrt{3}}{2} + \frac{i}{2} \right )}\right ]$$

In [62]:
sin(Lsg[2]).n()


Out[62]:
$$0.5 - 5.68149547717736 \cdot 10^{-142} i$$

Ungleichungen


In [63]:
f = x**2 + x + 1
g = 2*x**2
f, g


Out[63]:
$$\left ( x^{2} + x + 1, \quad 2 x^{2}\right )$$

In [64]:
solve(f > g)


Out[64]:
$$x < \frac{1}{2} + \frac{\sqrt{5}}{2} \wedge - \frac{\sqrt{5}}{2} + \frac{1}{2} < x$$

In [65]:
fn = lambdify(x, f, 'numpy')
gn = lambdify(x, g, 'numpy')
xn = np.linspace(-3, 3)
plt.plot(xn, fn(xn), label='f')
plt.plot(xn, gn(xn), label='g')
plt.legend();



In [66]:
solve(sin(x)>cos(x), x)


Out[66]:
$$\left(-\infty < x \wedge x < - \frac{3 \pi}{4}\right) \vee \left(\frac{\pi}{4} < x \wedge x < \infty\right)$$

bekannter Fehler


In [67]:
xn = np.linspace(-10, 10, 200)
plt.plot(xn, np.sin(xn), label='sin')
plt.plot(xn, np.cos(xn), label='cos')
plt.legend();



In [ ]: