Computergestützte Mathematik zur Analysis¶

Vorlesung vom 01.12.2022

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

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

Don't Repeat Yourself! (DRY)¶

Wenn identische Daten an mehreren Stellen gehalten werden, dann

  • hat man unnötige Schreibarbeit geleistet
  • besteht Unsicherheit, dass die Daten initial identisch waren
  • besteht die Gefahr, dass Updates nicht alle Speicherorte erreichen

Copy-and-Paste¶

ist eine Form von DRY. Die besondere Gefahr ist, dass Änderungen am Input zu Änderungen am Output führen, die eine neuerliche Kopieroperation erorderlich machen, welche aber vergessen wird

Bei der Klausur ist copy-and-paste unzulässig. Wir löschen alle Ausgaben und lassen das Skript laufen. Eingaben, die sich nur durch Kopieren einer Ausgabe erklären lassen, werden nicht berücksichtigt.

weitere Integrale¶

In [2]:
x = Symbol('x')
In [3]:
f = 1/(1-x**2)**Rational(3,2)
I1 = Integral(f, x)
I1
Out[3]:
$\displaystyle \int \frac{1}{\left(1 - x^{2}\right)^{\frac{3}{2}}}\, dx$
In [4]:
F = I1.doit()
F
Out[4]:
$\displaystyle \begin{cases} - \frac{i x}{\sqrt{x^{2} - 1}} & \text{for}\: \left|{x^{2}}\right| > 1 \\\frac{x}{\sqrt{1 - x^{2}}} & \text{otherwise} \end{cases}$
In [5]:
F.diff(x).simplify()
Out[5]:
$\displaystyle \begin{cases} \frac{i}{\left(x^{2} - 1\right)^{\frac{3}{2}}} & \text{for}\: x > 1 \vee x < -1 \\\frac{1}{\left(1 - x^{2}\right)^{\frac{3}{2}}} & \text{otherwise} \end{cases}$

Nur der zweite Fall interessiert uns

In [6]:
print(F)
Piecewise((-I*x/sqrt(x**2 - 1), Abs(x**2) > 1), (x/sqrt(1 - x**2), True))
In [7]:
F.subs(Abs(x**2)>1, False)
Out[7]:
$\displaystyle \frac{x}{\sqrt{1 - x^{2}}}$
In [8]:
g = (x-2.4)**2*sqrt(x)*0.1875
g
Out[8]:
$\displaystyle 1.08 \sqrt{x} \left(0.416666666666667 x - 1\right)^{2}$
In [10]:
intervall  = (x, 0, 4)
plot(g, intervall);
In [11]:
Integral(g, intervall).doit()
Out[11]:
$\displaystyle 0.485255751108899$
In [12]:
Integral(g, intervall).n()
Out[12]:
$\displaystyle 1.09714285714286$

Moral: Wenn ich symbolisch integrieren will, darf ich keine Fließkommazahlen verwenden

In [15]:
Rational(pi.n())
Out[15]:
$\displaystyle \frac{884279719003555}{281474976710656}$
In [16]:
Rational(0.1875)
Out[16]:
$\displaystyle \frac{3}{16}$
In [17]:
g = 3*(x-Rational(12,5))**2*sqrt(x)/16
In [18]:
Integral(g, intervall).doit()
Out[18]:
$\displaystyle \frac{192}{175}$
In [19]:
Integral(g, intervall).doit().n()
Out[19]:
$\displaystyle 1.09714285714286$

Ist natürlich trotzdem ein Bug

Summen¶

In [23]:
n, m = symbols('n m')
In [24]:
S1 = Sum(n, (n,1,m))
S1
Out[24]:
$\displaystyle \sum_{n=1}^{m} n$

summit kleinem s ist nicht symbolisch

In [25]:
S1.doit()
Out[25]:
$\displaystyle \frac{m^{2}}{2} + \frac{m}{2}$
In [26]:
a = (-1)**n * x**(2*n+1) / factorial(2*n+1)
S2 = Sum(a, (n,0,oo))
S2
Out[26]:
$\displaystyle \sum_{n=0}^{\infty} \frac{\left(-1\right)^{n} x^{2 n + 1}}{\left(2 n + 1\right)!}$
In [27]:
S2.doit()
Out[27]:
$\displaystyle \sin{\left(x \right)}$
In [28]:
q = S('q')
S3 = Sum(q**n, (n,0,oo))
S3
Out[28]:
$\displaystyle \sum_{n=0}^{\infty} q^{n}$
In [29]:
S3.doit()
Out[29]:
$\displaystyle \begin{cases} \frac{1}{1 - q} & \text{for}\: \left|{q}\right| < 1 \\\sum_{n=0}^{\infty} q^{n} & \text{otherwise} \end{cases}$
In [30]:
S3.doit().subs(Abs(q)<1, True)
Out[30]:
$\displaystyle \frac{1}{1 - q}$
In [31]:
S4 = Sum(1/n**2, (n, 1, m))
S4
Out[31]:
$\displaystyle \sum_{n=1}^{m} \frac{1}{n^{2}}$
In [32]:
S4.doit()
Out[32]:
$\displaystyle \operatorname{harmonic}{\left(m,2 \right)}$
In [33]:
S4.subs(m, oo).doit()
Out[33]:
$\displaystyle \frac{\pi^{2}}{6}$
In [34]:
Sum(1/n**54, (n, 1, oo)).doit()
Out[34]:
$\displaystyle \frac{116599854539539449685672495250764 \pi^{54}}{81807125729900063867074959072425603825198823017351806640625}$
In [35]:
Sum(1/n**3, (n, 1, oo)).doit()
Out[35]:
$\displaystyle \zeta\left(3\right)$
In [36]:
f = zeta(Rational(1,2) + I*x)
f
Out[36]:
$\displaystyle \zeta\left(i x + \frac{1}{2}\right)$
In [37]:
plot(abs(f), (x, -1, 26));
In [38]:
s = Rational(1,2) + 15*I
S5 = Sum(1/n**s, (n, 1, oo))
S5
Out[38]:
$\displaystyle \sum_{n=1}^{\infty} n^{- \frac{1}{2} - 15 i}$
In [39]:
S5.doit()  # TypeError, die komplexe Reihe divergiert 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [39], in <cell line: 1>()
----> 1 S5.doit()

File ~\anaconda3\lib\site-packages\sympy\concrete\summations.py:234, in Sum.doit(self, **hints)
    232 if reps:
    233     undo = {v: k for k, v in reps.items()}
--> 234     did = self.xreplace(reps).doit(**hints)
    235     if isinstance(did, tuple):  # when separate=True
    236         did = tuple([i.xreplace(undo) for i in did])

File ~\anaconda3\lib\site-packages\sympy\concrete\summations.py:263, in Sum.doit(self, **hints)
    261 if newf is None:
    262     if f == self.function:
--> 263         zeta_function = self.eval_zeta_function(f, (i, a, b))
    264         if zeta_function is not None:
    265             return zeta_function

File ~\anaconda3\lib\site-packages\sympy\concrete\summations.py:293, in Sum.eval_zeta_function(self, f, limits)
    291 s = result[z]
    292 q = result[y] / result[w] + a
--> 293 return Piecewise((coeff * zeta(s, q), And(q > 0, s > 1)), (self, True))

File ~\anaconda3\lib\site-packages\sympy\core\decorators.py:236, in _SympifyWrapper.make_wrapped.<locals>._func(self, other)
    234 if not isinstance(other, expectedcls):
    235     return retval
--> 236 return func(self, other)

File ~\anaconda3\lib\site-packages\sympy\core\expr.py:365, in Expr.__gt__(self, other)
    362 @sympify_return([('other', 'Expr')], NotImplemented)
    363 def __gt__(self, other):
    364     from .relational import StrictGreaterThan
--> 365     return StrictGreaterThan(self, other)

File ~\anaconda3\lib\site-packages\sympy\core\relational.py:834, in _Inequality.__new__(cls, lhs, rhs, **options)
    832 for me in (lhs, rhs):
    833     if me.is_extended_real is False:
--> 834         raise TypeError("Invalid comparison of non-real %s" % me)
    835     if me is S.NaN:
    836         raise TypeError("Invalid NaN comparison")

TypeError: Invalid comparison of non-real 1/2 + 15*I

Zeichenketten¶

Strings

In [40]:
"Text" == 'Text'
Out[40]:
True
In [41]:
netto = 2.72
f"Bruttopreis = {netto}€ plus {netto*0.16}€ Mehrwertsteuer"
Out[41]:
'Bruttopreis = 2.72€ plus 0.43520000000000003€ Mehrwertsteuer'
In [44]:
f"Bruttopreis = {netto}€ plus {netto*0.16:.2f}€ Mehrwertsteuer"
Out[44]:
'Bruttopreis = 2.72€ plus 0.44€ Mehrwertsteuer'
 .2f     (2 Nachkommastellen)
8.2f     (2 Nachkommastellen, insgesamt 8 Zeichen inkl. eventueller Leerzeichen)

Polarkoordinaten¶

In [45]:
r = Symbol('r', positive=True)
phi = Symbol('phi', real=True)
x = r*cos(phi)
y = r*sin(phi)
x, y
Out[45]:
$\displaystyle \left( r \cos{\left(\phi \right)}, \ r \sin{\left(\phi \right)}\right)$
In [46]:
r == simplify(sqrt(x**2+y**2))
Out[46]:
True
In [47]:
phi == atan(y/x)
Out[47]:
False
In [48]:
ers1 = {r:1, phi: pi/3}
x1 = x.subs(ers1)
y1 = y.subs(ers1)
x1, y1
Out[48]:
$\displaystyle \left( \frac{1}{2}, \ \frac{\sqrt{3}}{2}\right)$
In [49]:
atan(y1/x1)
Out[49]:
$\displaystyle \frac{\pi}{3}$
In [50]:
ers2 = {r:1, phi: 4*pi/3}
x2 = x.subs(ers2)
y2 = y.subs(ers2)
x2, y2
Out[50]:
$\displaystyle \left( - \frac{1}{2}, \ - \frac{\sqrt{3}}{2}\right)$
In [51]:
atan(y2/x2)
Out[51]:
$\displaystyle \frac{\pi}{3}$
In [52]:
atan2(y1, x1)
Out[52]:
$\displaystyle \frac{\pi}{3}$
In [53]:
atan2(y2, x2)
Out[53]:
$\displaystyle - \frac{2 \pi}{3}$

Potenzfunktionen¶

In [54]:
x, y = symbols('x y')
I1 = Integral(x**Rational(2,3), (x,-8,8))
I1
Out[54]:
$\displaystyle \int\limits_{-8}^{8} x^{\frac{2}{3}}\, dx$
In [55]:
I1.doit()
Out[55]:
$\displaystyle \frac{96}{5} + \frac{96 \left(-1\right)^{\frac{2}{3}}}{5}$

Das Problem geschickt zurückgespielt 😉

Was ist $(-8)^\frac23$ ?

In [56]:
(-8)**Rational(2,3)
Out[56]:
$\displaystyle 4 \left(-1\right)^{\frac{2}{3}}$
In [57]:
((-8)**Rational(2,3)).expand(complex=True)
Out[57]:
$\displaystyle -2 + 2 \sqrt{3} i$
In [58]:
(((-8)**Rational(1,3))**2).expand(complex=True)
Out[58]:
$\displaystyle -2 + 2 \sqrt{3} i$
In [59]:
((-8)**2)**Rational(1,3)
Out[59]:
$\displaystyle 4$
In [61]:
glg = Eq(x**3, (-8)**2)
solveset(glg, x)
Out[61]:
$\displaystyle \left\{4, -2 - 2 \sqrt{3} i, -2 + 2 \sqrt{3} i\right\}$
In [62]:
p = exp(log(-8)*Rational(2,3))
p
Out[62]:
$\displaystyle 4 e^{\frac{2 i \pi}{3}}$
In [63]:
expand(p, complex=True)
Out[63]:
$\displaystyle -2 + 2 \sqrt{3} i$
In [64]:
s = Symbol('s')
In [65]:
q = exp(log(-8+I*s)*Rational(2,3))
q
Out[65]:
$\displaystyle \left(i s - 8\right)^{\frac{2}{3}}$
In [66]:
q.limit(s, 0, dir='+')
Out[66]:
$\displaystyle 4 \left(-1\right)^{\frac{2}{3}}$
In [67]:
q.limit(s, 0, dir='+').expand(complex=True)
Out[67]:
$\displaystyle -2 + 2 \sqrt{3} i$
In [68]:
q.limit(s, 0, dir='-')
Out[68]:
$\displaystyle - 4 \sqrt[3]{-1}$
In [69]:
q.limit(s, 0, dir='-').expand(complex=True)
Out[69]:
$\displaystyle -2 - 2 \sqrt{3} i$

Die Funktion $e^{\frac23i\phi}$ ist halt nicht $2\pi$-periodisch

In [70]:
phi = S('phi')
h = exp(2*I*phi/3)
plot(re(h), im(h), (phi, -pi, pi));