Computergestützte Mathematik zur Analysis¶

Vorlesung vom 08.12.2022

© 2022 Prof. Dr. Rüdiger W. Braun

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

nutzergesteuerte trigonometrische Vereinfachungen¶

In [2]:
x = S('x')
y = S('y')
b = sin(x) + sin(y)
b
Out[2]:
$\displaystyle \sin{\left(x \right)} + \sin{\left(y \right)}$
In [3]:
b.trigsimp()
Out[3]:
$\displaystyle \sin{\left(x \right)} + \sin{\left(y \right)}$

Wir hätten gerne eine Darstellung als Produkt

In [4]:
b.trigsimp(method='fu')
Out[4]:
$\displaystyle \sin{\left(x \right)} + \sin{\left(y \right)}$

Wie kompliziert ist dieser Ausdruck?

In [6]:
b.count_ops()
Out[6]:
$\displaystyle 3$
In [7]:
b.count_ops(visual=True)
Out[7]:
$\displaystyle ADD + 2 SIN$
In [8]:
def my_measure(expr):
    opc = expr.count_ops(visual=True)
    print(opc)  # zur Fehlersuche
    strafe = {}
    strafe['ADD'] = 1
    strafe['SIN'] = 1
    strafe['MUL'] = -100 # negative Strafe = Belohnung
    return opc.subs(strafe) 
In [9]:
my_measure(b)
ADD + 2*SIN
Out[9]:
$\displaystyle 3$
In [11]:
# b.trigsimp(method='fu', measure=my_measure)  # TypeError
In [12]:
def my_measure(expr):
    opc = expr.count_ops(visual=True)
    # print(opc)  # zur Fehlersuche
    strafe = {}
    strafe['ADD'] = 1
    strafe['SIN'] = 1
    strafe['MUL'] = -100
    strafe['COS'] = 1
    strafe['DIV'] = 1
    strafe['SUB'] = 1
    return opc.subs(strafe)
In [13]:
c = b.trigsimp(method='fu', measure=my_measure)
c
Out[13]:
$\displaystyle 2 \sin{\left(\frac{x}{2} + \frac{y}{2} \right)} \cos{\left(\frac{x}{2} - \frac{y}{2} \right)}$
In [15]:
my_measure(c)
Out[15]:
$\displaystyle -192$
In [16]:
c.trigsimp()
Out[16]:
$\displaystyle \sin{\left(x \right)} + \sin{\left(y \right)}$
In [17]:
d = sin(x)**8
In [18]:
d.trigsimp()
Out[18]:
$\displaystyle \sin^{8}{\left(x \right)}$
In [19]:
def my_measure(expr):
    opc = expr.count_ops(visual=True)
    # print(opc)  # zur Fehlersuche
    strafe = {}
    strafe['ADD'] = 1
    strafe['SIN'] = 100
    strafe['MUL'] = 1
    strafe['COS'] = 1
    strafe['DIV'] = 1
    strafe['SUB'] = 1
    strafe['POW'] = 1
    return opc.subs(strafe)
In [20]:
d.trigsimp(method='fu', measure=my_measure)
Out[20]:
$\displaystyle \cos^{8}{\left(x \right)} - 4 \cos^{6}{\left(x \right)} + 6 \cos^{4}{\left(x \right)} - 4 \cos^{2}{\left(x \right)} + 1$

Reihenentwicklungen¶

In [21]:
x = S('x')
f = cos(2*x)
f.series(x, n=12)
Out[21]:
$\displaystyle 1 - 2 x^{2} + \frac{2 x^{4}}{3} - \frac{4 x^{6}}{45} + \frac{2 x^{8}}{315} - \frac{4 x^{10}}{14175} + O\left(x^{12}\right)$
In [22]:
f.series(x, pi/4)
Out[22]:
$\displaystyle \frac{\pi}{2} + \frac{4 \left(x - \frac{\pi}{4}\right)^{3}}{3} - \frac{4 \left(x - \frac{\pi}{4}\right)^{5}}{15} - 2 x + O\left(\left(x - \frac{\pi}{4}\right)^{6}; x\rightarrow \frac{\pi}{4}\right)$
In [23]:
f.subs(x, pi/4)
Out[23]:
$\displaystyle 0$

Die Reihe ist richtig, sie ist nur dämlich hingeschrieben

In [24]:
g = 1/(x**2+x+1)
g
Out[24]:
$\displaystyle \frac{1}{x^{2} + x + 1}$
In [25]:
gs = g.series(x, oo)
gs
Out[25]:
$\displaystyle \frac{1}{x^{5}} - \frac{1}{x^{3}} + \frac{1}{x^{2}} + O\left(\frac{1}{x^{6}}; x\rightarrow \infty\right)$
In [28]:
h = exp(x)
h.series(x, -oo, n=25)
Out[28]:
$\displaystyle O\left(- \frac{1}{x^{25}}; x\rightarrow -\infty\right)$
In [29]:
h.series(x, oo)
Out[29]:
$\displaystyle e^{x}$
In [30]:
exp(sqrt(x+1)).series(x, oo)
Out[30]:
$\displaystyle e^{\sqrt{x + 1}}$
In [33]:
r = exp(sqrt(x+1)-sqrt(x))
r
Out[33]:
$\displaystyle e^{- \sqrt{x} + \sqrt{x + 1}}$
In [34]:
r_ser = r.series(x, oo, n=3)
r_ser
Out[34]:
$\displaystyle - \frac{23}{384 x^{2}} + \frac{1}{8 x} + 1 + \frac{\sqrt{\frac{1}{x}}}{2} - \frac{5 \left(\frac{1}{x}\right)^{\frac{3}{2}}}{48} + \frac{181 \left(\frac{1}{x}\right)^{\frac{5}{2}}}{3840} + O\left(\frac{1}{x^{3}}; x\rightarrow \infty\right)$
In [35]:
(sqrt(x)*(r-1)).limit(x, oo)
Out[35]:
$\displaystyle \frac{1}{2}$
In [36]:
a = S('a')
b_ser = log(sqrt(x+a)).series(x, oo)
b_ser
Out[36]:
$\displaystyle - \frac{\log{\left(\frac{1}{x} \right)}}{2} + \frac{a}{2 x} - \frac{a^{2}}{4 x^{2}} + \frac{a^{3}}{6 x^{3}} - \frac{a^{4}}{8 x^{4}} + \frac{a^{5}}{10 x^{5}} + O\left(\frac{1}{x^{6}}; x\rightarrow \infty\right)$
In [38]:
b_ser.removeO()
Out[38]:
$\displaystyle \frac{a^{5}}{10 x^{5}} - \frac{a^{4}}{8 x^{4}} + \frac{a^{3}}{6 x^{3}} - \frac{a^{2}}{4 x^{2}} + \frac{a}{2 x} - \frac{\log{\left(\frac{1}{x} \right)}}{2}$
In [39]:
reihe = x + 9 + 100/x + O(1/x**2, (x,oo))
reihe
Out[39]:
$\displaystyle \frac{100}{x} + 9 + x + O\left(\frac{1}{x^{2}}; x\rightarrow \infty\right)$
In [40]:
(reihe**2).expand()
Out[40]:
$\displaystyle 281 + 18 x + x^{2} + O\left(\frac{1}{x}; x\rightarrow \infty\right)$

Beispiel mit der Lambert-Funktion¶

In [41]:
glg = Eq(log(x), x + log(y))
glg
Out[41]:
$\displaystyle \log{\left(x \right)} = x + \log{\left(y \right)}$
In [42]:
lsg = solve(glg, x)
lsg
Out[42]:
$\displaystyle \left[ - W\left(- y\right)\right]$
In [43]:
f = lsg[0]
f_ser = f.series(y, 0)
f_ser
Out[43]:
$\displaystyle y + y^{2} + \frac{3 y^{3}}{2} + \frac{8 y^{4}}{3} + \frac{125 y^{5}}{24} + O\left(y^{6}\right)$
In [44]:
g1 = glg.subs(x, f_ser)
g1
Out[44]:
$\displaystyle \log{\left(y + y^{2} + \frac{3 y^{3}}{2} + \frac{8 y^{4}}{3} + \frac{125 y^{5}}{24} + O\left(y^{6}\right) \right)} = \log{\left(y \right)} + y + y^{2} + \frac{3 y^{3}}{2} + \frac{8 y^{4}}{3} + \frac{125 y^{5}}{24} + O\left(y^{6}\right)$
In [45]:
l_ser = g1.lhs.series(y, 0)
l_ser
Out[45]:
$\displaystyle \log{\left(y \right)} + y + y^{2} + \frac{3 y^{3}}{2} + \frac{8 y^{4}}{3} - \frac{671 y^{5}}{120} + O\left(y^{6}\right)$
In [46]:
r_ser = g1.rhs.series(y, 0)
r_ser
Out[46]:
$\displaystyle \log{\left(y \right)} + y + y^{2} + \frac{3 y^{3}}{2} + \frac{8 y^{4}}{3} + \frac{125 y^{5}}{24} + O\left(y^{6}\right)$
In [47]:
series(log(1+y))
Out[47]:
$\displaystyle y - \frac{y^{2}}{2} + \frac{y^{3}}{3} - \frac{y^{4}}{4} + \frac{y^{5}}{5} + O\left(y^{6}\right)$
In [48]:
l_ser - r_ser
Out[48]:
$\displaystyle - \frac{54 y^{5}}{5} + O\left(y^{6}\right)$

Vektoren und Matrizen¶

In [49]:
v = Matrix([1,2,3])
v
Out[49]:
$\displaystyle \left[\begin{matrix}1\\2\\3\end{matrix}\right]$
In [50]:
w = Matrix(1,3,[4,5,6])  # 1x3-Matrix
w
Out[50]:
$\displaystyle \left[\begin{matrix}4 & 5 & 6\end{matrix}\right]$
In [51]:
v[1]
Out[51]:
$\displaystyle 2$
In [52]:
w[1] 
Out[52]:
$\displaystyle 5$
In [54]:
v*w
Out[54]:
$\displaystyle \left[\begin{matrix}4 & 5 & 6\\8 & 10 & 12\\12 & 15 & 18\end{matrix}\right]$
In [55]:
w * v
Out[55]:
$\displaystyle \left[\begin{matrix}32\end{matrix}\right]$
In [57]:
A = Matrix(3,3,range(1,10))
A
Out[57]:
$\displaystyle \left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$
In [58]:
A[1,0]
Out[58]:
$\displaystyle 4$
In [59]:
A[8]
Out[59]:
$\displaystyle 9$

???

In [60]:
A.row(1)
Out[60]:
$\displaystyle \left[\begin{matrix}4 & 5 & 6\end{matrix}\right]$
In [61]:
A.col(2)
Out[61]:
$\displaystyle \left[\begin{matrix}3\\6\\9\end{matrix}\right]$
In [62]:
A * v
Out[62]:
$\displaystyle \left[\begin{matrix}14\\32\\50\end{matrix}\right]$
In [65]:
# v * A # ShapeError
In [68]:
x = S('x')
y = S('y')
B = Matrix([[x,1], [1,y]])
B
Out[68]:
$\displaystyle \left[\begin{matrix}x & 1\\1 & y\end{matrix}\right]$

variable Vektoren¶

In [69]:
x = symbols('x0:10')
x
Out[69]:
$\displaystyle \left( x_{0}, \ x_{1}, \ x_{2}, \ x_{3}, \ x_{4}, \ x_{5}, \ x_{6}, \ x_{7}, \ x_{8}, \ x_{9}\right)$
In [70]:
x[7]
Out[70]:
$\displaystyle x_{7}$
In [71]:
M = Matrix(2, 5, x)
M
Out[71]:
$\displaystyle \left[\begin{matrix}x_{0} & x_{1} & x_{2} & x_{3} & x_{4}\\x_{5} & x_{6} & x_{7} & x_{8} & x_{9}\end{matrix}\right]$
In [72]:
a = symbols('a0:2_0:5')
A = Matrix(2, 5, a)
In [73]:
A
Out[73]:
$\displaystyle \left[\begin{matrix}a_{0 0} & a_{0 1} & a_{0 2} & a_{0 3} & a_{0 4}\\a_{1 0} & a_{1 1} & a_{1 2} & a_{1 3} & a_{1 4}\end{matrix}\right]$