Lektion 12

In [1]:
from IPython.display import display
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
init_printing()
%matplotlib notebook
x,y,z = symbols('x y z')

Daten exportieren und importieren

In [2]:
H  = Matrix(5,5,[Rational(1,j+i+1) for i in range(5) for j in range(5)])
H
Out[2]:
$$\left[\begin{matrix}1 & \frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \frac{1}{5}\\\frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6}\\\frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6} & \frac{1}{7}\\\frac{1}{4} & \frac{1}{5} & \frac{1}{6} & \frac{1}{7} & \frac{1}{8}\\\frac{1}{5} & \frac{1}{6} & \frac{1}{7} & \frac{1}{8} & \frac{1}{9}\end{matrix}\right]$$
In [3]:
str(H)
Out[3]:
'Matrix([[1, 1/2, 1/3, 1/4, 1/5], [1/2, 1/3, 1/4, 1/5, 1/6], [1/3, 1/4, 1/5, 1/6, 1/7], [1/4, 1/5, 1/6, 1/7, 1/8], [1/5, 1/6, 1/7, 1/8, 1/9]])'
In [4]:
srepr(H) # erzeugt ausfuehrbaren Python code
Out[4]:
'MutableDenseMatrix([[Integer(1), Rational(1, 2), Rational(1, 3), Rational(1, 4), Rational(1, 5)], [Rational(1, 2), Rational(1, 3), Rational(1, 4), Rational(1, 5), Rational(1, 6)], [Rational(1, 3), Rational(1, 4), Rational(1, 5), Rational(1, 6), Rational(1, 7)], [Rational(1, 4), Rational(1, 5), Rational(1, 6), Rational(1, 7), Rational(1, 8)], [Rational(1, 5), Rational(1, 6), Rational(1, 7), Rational(1, 8), Rational(1, 9)]])'
In [5]:
preview(H,output='dvi')
In [6]:
with open('output.txt','w') as f:  # w: write
    f.write(srepr(H) + '\n')
In [7]:
%less output.txt
In [8]:
x,y = symbols('x y')
f = log(x)*sqrt(x**2/3+y**2)
df = f.diff(x,y).simplify()
df
f = Function('f')
lhs = f(x,y).diff(x,y)
eq = Eq(lhs, df)
eq
Out[8]:
$$\frac{\partial^{2}}{\partial x\partial y} f{\left (x,y \right )} = \frac{\sqrt{3} y}{x \left(x^{2} + 3 y^{2}\right)^{\frac{3}{2}}} \left(- x^{2} \log{\left (x \right )} + x^{2} + 3 y^{2}\right)$$
In [9]:
with open('diff.tex','w') as f:
    f.write(latex(eq) + '\n')
In [10]:
%less diff.tex 
In [11]:
z = symbols('z:5')
V = Matrix(5,5,[z[j]**i for j in range(5) for i in range(5)])
V
Out[11]:
$$\left[\begin{matrix}1 & z_{0} & z_{0}^{2} & z_{0}^{3} & z_{0}^{4}\\1 & z_{1} & z_{1}^{2} & z_{1}^{3} & z_{1}^{4}\\1 & z_{2} & z_{2}^{2} & z_{2}^{3} & z_{2}^{4}\\1 & z_{3} & z_{3}^{2} & z_{3}^{3} & z_{3}^{4}\\1 & z_{4} & z_{4}^{2} & z_{4}^{3} & z_{4}^{4}\end{matrix}\right]$$
In [12]:
with open('output.txt','a') as f:  # 'a' append / anhaengen
    f.write(srepr(2*H) + '\n')
    f.write(srepr(V))
In [13]:
with open('output.txt') as f:  # read (default)
    HH = S(f.readline())
    H2 = S(f.readline())
    VV = S(f.readline())
In [14]:
HH, H2, VV
Out[14]:
$$\left ( \left[\begin{matrix}1 & \frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \frac{1}{5}\\\frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6}\\\frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6} & \frac{1}{7}\\\frac{1}{4} & \frac{1}{5} & \frac{1}{6} & \frac{1}{7} & \frac{1}{8}\\\frac{1}{5} & \frac{1}{6} & \frac{1}{7} & \frac{1}{8} & \frac{1}{9}\end{matrix}\right], \quad \left[\begin{matrix}2 & 1 & \frac{2}{3} & \frac{1}{2} & \frac{2}{5}\\1 & \frac{2}{3} & \frac{1}{2} & \frac{2}{5} & \frac{1}{3}\\\frac{2}{3} & \frac{1}{2} & \frac{2}{5} & \frac{1}{3} & \frac{2}{7}\\\frac{1}{2} & \frac{2}{5} & \frac{1}{3} & \frac{2}{7} & \frac{1}{4}\\\frac{2}{5} & \frac{1}{3} & \frac{2}{7} & \frac{1}{4} & \frac{2}{9}\end{matrix}\right], \quad \left[\begin{matrix}1 & z_{0} & z_{0}^{2} & z_{0}^{3} & z_{0}^{4}\\1 & z_{1} & z_{1}^{2} & z_{1}^{3} & z_{1}^{4}\\1 & z_{2} & z_{2}^{2} & z_{2}^{3} & z_{2}^{4}\\1 & z_{3} & z_{3}^{2} & z_{3}^{3} & z_{3}^{4}\\1 & z_{4} & z_{4}^{2} & z_{4}^{3} & z_{4}^{4}\end{matrix}\right]\right )$$
In [15]:
%less output.txt 
In [16]:
# '%' IPython Magie (shell Befehl 'less')
# Aendere output.txt in Editor
In [17]:
with open('output.txt') as f:  # read (default)
    for zeile in f:
        print(zeile)
    else:
        print('Ende erreicht')
MutableDenseMatrix([[Integer(1), Rational(1, 2), Rational(1, 3), Rational(1, 4), Rational(1, 5)], [Rational(1, 2), Rational(1, 3), Rational(1, 4), Rational(1, 5), Rational(1, 6)], [Rational(1, 3), Rational(1, 4), Rational(1, 5), Rational(1, 6), Rational(1, 7)], [Rational(1, 4), Rational(1, 5), Rational(1, 6), Rational(1, 7), Rational(1, 8)], [Rational(1, 5), Rational(1, 6), Rational(1, 7), Rational(1, 8), Rational(1, 9)]])

MutableDenseMatrix([[Integer(2), Integer(1), Rational(2, 3), Rational(1, 2), Rational(2, 5)], [Integer(1), Rational(2, 3), Rational(1, 2), Rational(2, 5), Rational(1, 3)], [Rational(2, 3), Rational(1, 2), Rational(2, 5), Rational(1, 3), Rational(2, 7)], [Rational(1, 2), Rational(2, 5), Rational(1, 3), Rational(2, 7), Rational(1, 4)], [Rational(2, 5), Rational(1, 3), Rational(2, 7), Rational(1, 4), Rational(2, 9)]])

MutableDenseMatrix([[Integer(1), Symbol('z0'), Pow(Symbol('z0'), Integer(2)), Pow(Symbol('z0'), Integer(3)), Pow(Symbol('z0'), Integer(4))], [Integer(1), Symbol('z1'), Pow(Symbol('z1'), Integer(2)), Pow(Symbol('z1'), Integer(3)), Pow(Symbol('z1'), Integer(4))], [Integer(1), Symbol('z2'), Pow(Symbol('z2'), Integer(2)), Pow(Symbol('z2'), Integer(3)), Pow(Symbol('z2'), Integer(4))], [Integer(1), Symbol('z3'), Pow(Symbol('z3'), Integer(2)), Pow(Symbol('z3'), Integer(3)), Pow(Symbol('z3'), Integer(4))], [Integer(1), Symbol('z4'), Pow(Symbol('z4'), Integer(2)), Pow(Symbol('z4'), Integer(3)), Pow(Symbol('z4'), Integer(4))]])
Ende erreicht
In [18]:
%less output.txt

Bilder exportieren / speichern

In [19]:
p = plot_implicit(x**2+y**2-1)
In [20]:
p.save('kreis.png')
In [21]:
fig = plt.figure()
plt.plot([1,2],[1,2],'r');
In [22]:
fig.savefig('rotelinie.png',format = 'png')
fig.savefig('rotelinie.pdf',format = 'pdf')
fig.savefig('rotelinie.eps',format = 'eps')
fig.savefig('rotelinie.svg',format = 'svg')

Differentialgleichungen

erstes Beispiel

$\dot y(t) = y(t)$

In [23]:
y = Function('y')
t,tau = symbols('t tau', real = True)
dgl = Eq(y(t).diff(t)-y(t),0)
sol = dsolve(dgl,y(t))
sol
Out[23]:
$$y{\left (t \right )} = C_{1} e^{t}$$
In [24]:
C1 = sol.atoms(Symbol).difference(dgl.atoms(Symbol)).pop()
C1
Out[24]:
$$C_{1}$$
In [25]:
C1 = solve(sol,C1).pop().subs(t,0)
C1
Out[25]:
$$y{\left (0 \right )}$$
In [26]:
checkodesol(dgl,sol)
Out[26]:
(True, 0)

inhomogene lineare DGL

$$ \dot{u}(t) = u(t) + \sin(t) $$

In [27]:
u = Function('u')
t = symbols('t', real = True)
dgl = Eq(u(t).diff(t)-u(t)-sin(t))
sol = dsolve(dgl,u(t))
sol
Out[27]:
$$u{\left (t \right )} = \left(C_{1} - \frac{e^{- t}}{2} \sin{\left (t \right )} - \frac{e^{- t}}{2} \cos{\left (t \right )}\right) e^{t}$$
In [28]:
C1 = sol.atoms(Symbol).difference(dgl.atoms(Symbol)).pop()
C1 = solve(sol,C1).pop().subs(t,0)
C1
Out[28]:
$$u{\left (0 \right )} + \frac{1}{2}$$
In [29]:
w = (sol.subs(sol.atoms(Symbol).difference(dgl.atoms(Symbol)).pop(),C1)).rhs
w
Out[29]:
$$\left(u{\left (0 \right )} + \frac{1}{2} - \frac{e^{- t}}{2} \sin{\left (t \right )} - \frac{e^{- t}}{2} \cos{\left (t \right )}\right) e^{t}$$

Variation der Konstanten Formel

In [30]:
v = u(0)*exp(t) + integrate(exp(t-tau)*(sin(tau)),(tau,0,t))
v
Out[30]:
$$u{\left (0 \right )} e^{t} + \frac{e^{t}}{2} - \frac{1}{2} \sin{\left (t \right )} - \frac{1}{2} \cos{\left (t \right )}$$
In [31]:
simplify(v-w)
Out[31]:
$$0$$

Loesung mit einen Reihenansatz

$$ \dot{y}(t)=y(t),y(0)=1 $$ mit einem Reihenansatz

In [32]:
y0, t, C = symbols('y0 t C')
a = symbols('a:8')
y = Function('y')
In [33]:
dgl = Eq(y(t).diff(t),y(t))
dgl
Out[33]:
$$\frac{d}{d t} y{\left (t \right )} = y{\left (t \right )}$$
In [34]:
ys = sum([a[i]*t**i for i in range(8)])
ys = ys.subs(a[0],1)  # y(0) = 1
ys
Out[34]:
$$a_{1} t + a_{2} t^{2} + a_{3} t^{3} + a_{4} t^{4} + a_{5} t^{5} + a_{6} t^{6} + a_{7} t^{7} + 1$$
In [35]:
gl = dgl.subs(y(t),ys).doit()
gl
Out[35]:
$$a_{1} + 2 a_{2} t + 3 a_{3} t^{2} + 4 a_{4} t^{3} + 5 a_{5} t^{4} + 6 a_{6} t^{5} + 7 a_{7} t^{6} = a_{1} t + a_{2} t^{2} + a_{3} t^{3} + a_{4} t^{4} + a_{5} t^{5} + a_{6} t^{6} + a_{7} t^{7} + 1$$
In [36]:
gl.coeff(t)
Out[36]:
$$0$$
In [37]:
gl1 = (gl.lhs - gl.rhs).expand()
gl1
Out[37]:
$$- a_{1} t + a_{1} - a_{2} t^{2} + 2 a_{2} t - a_{3} t^{3} + 3 a_{3} t^{2} - a_{4} t^{4} + 4 a_{4} t^{3} - a_{5} t^{5} + 5 a_{5} t^{4} - a_{6} t^{6} + 6 a_{6} t^{5} - a_{7} t^{7} + 7 a_{7} t^{6} - 1$$
In [38]:
gls = gl1.as_poly(t).all_coeffs()
gls[1:]
Out[38]:
$$\left [ - a_{6} + 7 a_{7}, \quad - a_{5} + 6 a_{6}, \quad - a_{4} + 5 a_{5}, \quad - a_{3} + 4 a_{4}, \quad - a_{2} + 3 a_{3}, \quad - a_{1} + 2 a_{2}, \quad a_{1} - 1\right ]$$
In [39]:
ac = solve(gls[1:])
ac
Out[39]:
$$\left \{ a_{1} : 1, \quad a_{2} : \frac{1}{2}, \quad a_{3} : \frac{1}{6}, \quad a_{4} : \frac{1}{24}, \quad a_{5} : \frac{1}{120}, \quad a_{6} : \frac{1}{720}, \quad a_{7} : \frac{1}{5040}\right \}$$
In [40]:
ac[a[0]] = 1
acc = [ac[j]for j in a]
acc
Out[40]:
$$\left [ 1, \quad 1, \quad \frac{1}{2}, \quad \frac{1}{6}, \quad \frac{1}{24}, \quad \frac{1}{120}, \quad \frac{1}{720}, \quad \frac{1}{5040}\right ]$$
In [41]:
[ acc[j]/acc[j+1] for j in range(7)]
Out[41]:
$$\left [ 1, \quad 2, \quad 3, \quad 4, \quad 5, \quad 6, \quad 7\right ]$$
In [42]:
[ acc[j] - 1/factorial(j) for j in range(8)]
Out[42]:
$$\left [ 0, \quad 0, \quad 0, \quad 0, \quad 0, \quad 0, \quad 0, \quad 0\right ]$$
In [43]:
n = symbols('n')
yr = Sum(t**n/factorial(n) ,(n,0,oo))
yr
Out[43]:
$$\sum_{n=0}^{\infty} \frac{t^{n}}{n!}$$
In [44]:
yr.doit()
Out[44]:
$$e^{t}$$

Logistische Gleichung

$$ \dot{y}(t) = (1-y(t))y(t)$$

In [45]:
y = Function('y')
t = symbols('t', real = True)
dgl = Eq(y(t).diff(t)-(1-y(t))*y(t))
sol = dsolve(dgl,y(t))
sol
Out[45]:
$$y{\left (t \right )} = - \frac{1}{C_{1} e^{- t} - 1}$$
In [46]:
K1 = sol.atoms(Symbol).difference(dgl.atoms(Symbol)).pop()
K1
Out[46]:
$$C_{1}$$
In [47]:
C1 = solve(sol,K1)
C1
Out[47]:
$$\left [ e^{t} - \frac{e^{t}}{y{\left (t \right )}}\right ]$$
In [48]:
C1 = solve(sol,K1).pop().subs(t,0)
C1
Out[48]:
$$1 - \frac{1}{y{\left (0 \right )}}$$
In [49]:
sol.subs(sol.atoms(Symbol).difference(dgl.atoms(Symbol)).pop(),C1)
Out[49]:
$$y{\left (t \right )} = - \frac{1}{\left(1 - \frac{1}{y{\left (0 \right )}}\right) e^{- t} - 1}$$

Zweite Ordnung DGL

$$\ddot{y}(t) = -2 \dot{y}(t) + y(t) - 100\cos(t)$$

In [50]:
dgl2 = Eq(y(t).diff(t,2) + 2*y(t).diff(t) -y(t)+100*cos(t),0)
dgl2
Out[50]:
$$- y{\left (t \right )} + 100 \cos{\left (t \right )} + 2 \frac{d}{d t} y{\left (t \right )} + \frac{d^{2}}{d t^{2}} y{\left (t \right )} = 0$$
In [51]:
sol = dsolve(dgl2,y(t))
sol
Out[51]:
$$y{\left (t \right )} = C_{1} e^{t \left(-1 + \sqrt{2}\right)} + C_{2} e^{t \left(- \sqrt{2} - 1\right)} - 25 \sin{\left (t \right )} + 25 \cos{\left (t \right )}$$

Anfangswerte

$$y(0)=10,\ \dot{y}(0)=0$$

In [52]:
[Eq(sol.rhs.subs(t,0),10), Eq(sol.rhs.diff(t).subs(t,0),0)]
Out[52]:
$$\left [ C_{1} + C_{2} + 25 = 10, \quad C_{1} \left(-1 + \sqrt{2}\right) + C_{2} \left(- \sqrt{2} - 1\right) - 25 = 0\right ]$$
In [53]:
Constants = solve([Eq(sol.rhs.subs(t,0),10), Eq(sol.rhs.diff(t).subs(t,0),0)])
Constants # Loesung per Hand
Out[53]:
$$\left \{ C_{1} : - \frac{15}{2} + \frac{5 \sqrt{2}}{2}, \quad C_{2} : - \frac{15}{2} - \frac{5 \sqrt{2}}{2}\right \}$$
In [54]:
# kuerzer
Constants = solve([sol.rhs.subs(t,0)-10, sol.rhs.diff(t).subs(t,0)])
Constants
Out[54]:
$$\left \{ C_{1} : - \frac{15}{2} + \frac{5 \sqrt{2}}{2}, \quad C_{2} : - \frac{15}{2} - \frac{5 \sqrt{2}}{2}\right \}$$
In [55]:
sol = sol.subs(Constants)
sol
Out[55]:
$$y{\left (t \right )} = \left(- \frac{15}{2} + \frac{5 \sqrt{2}}{2}\right) e^{t \left(-1 + \sqrt{2}\right)} + \left(- \frac{15}{2} - \frac{5 \sqrt{2}}{2}\right) e^{t \left(- \sqrt{2} - 1\right)} - 25 \sin{\left (t \right )} + 25 \cos{\left (t \right )}$$
In [56]:
tn = np.linspace(0,5,500)
yn = lambdify(t,sol.rhs)
fig = plt.figure()
plt.plot(tn,yn(tn))
Out[56]:
[<matplotlib.lines.Line2D at 0x7f7eadbb3240>]

Matrixexponentialfunktion

In [57]:
t = symbols('t',real=True)
In [58]:
A = Matrix(3,3,[2,1,0,0,2,1,0,0,2])
A
Out[58]:
$$\left[\begin{matrix}2 & 1 & 0\\0 & 2 & 1\\0 & 0 & 2\end{matrix}\right]$$
In [59]:
(t*A).exp()
Out[59]:
$$\left[\begin{matrix}e^{2 t} & t e^{2 t} & \frac{t^{2}}{2} e^{2 t}\\0 & e^{2 t} & t e^{2 t}\\0 & 0 & e^{2 t}\end{matrix}\right]$$
In [60]:
u0 = Matrix(3,1,[1,2,3])
u0
Out[60]:
$$\left[\begin{matrix}1\\2\\3\end{matrix}\right]$$
In [61]:
u = (t*A).exp()*u0
u
Out[61]:
$$\left[\begin{matrix}\frac{3 t^{2}}{2} e^{2 t} + 2 t e^{2 t} + e^{2 t}\\3 t e^{2 t} + 2 e^{2 t}\\3 e^{2 t}\end{matrix}\right]$$
In [62]:
diff(u,t) - A*u
Out[62]:
$$\left[\begin{matrix}0\\0\\0\end{matrix}\right]$$

Gekoppeltes Pendel (Kleinwinkelnaeherung)

\begin{align} \ddot{y}(t) &= w(t) -y(t) + \cos(2t)\\ \ddot{w}(t) &= y(t)-w(t) \end{align} aequivalent zum System erster Ordnung $$x_0 = y, x_1 = \dot{y}, x_2 = w, x_3 = \dot{w}$$ ergibt \begin{align} \dot{x}_0(t) &= x_1(t) \\ \dot{x}_1(t) &= x_2(t) - x_0(t) + \cos(2t) \\ \dot{x}_2(t) &= x_3(t) \\ \dot{x}_3(t) &= x_0(t)-x_2(t) \end{align}

In [63]:
y = Function('y')
w = Function('w')
t, tau = symbols('t tau',real=True)
In [64]:
dgl = (Eq( y(t).diff(t,2), w(t)-y(t)+cos(t)), \
Eq( w(t).diff(t,2), y(t)-w(t)))
dgl
Out[64]:
$$\left ( \frac{d^{2}}{d t^{2}} y{\left (t \right )} = w{\left (t \right )} - y{\left (t \right )} + \cos{\left (t \right )}, \quad \frac{d^{2}}{d t^{2}} w{\left (t \right )} = - w{\left (t \right )} + y{\left (t \right )}\right )$$
In [65]:
#dsolve(dgl,(y(t),w(t)))
In [66]:
A = Matrix(4,4,[0,1,0,0, -1,0,1,0, 0,0,0,1, 1,0,-1,0])
A
Out[66]:
$$\left[\begin{matrix}0 & 1 & 0 & 0\\-1 & 0 & 1 & 0\\0 & 0 & 0 & 1\\1 & 0 & -1 & 0\end{matrix}\right]$$
In [67]:
Tt = (t*A).exp()
Tt
Out[67]:
$$\left[\begin{matrix}\frac{1}{4} e^{\sqrt{2} i t} + \frac{1}{2} + \frac{1}{4} e^{- \sqrt{2} i t} & \frac{t}{2} - \frac{\sqrt{2} i}{8} e^{\sqrt{2} i t} + \frac{\sqrt{2} i}{8} e^{- \sqrt{2} i t} & - \frac{1}{4} e^{\sqrt{2} i t} + \frac{1}{2} - \frac{1}{4} e^{- \sqrt{2} i t} & \frac{t}{2} + \frac{\sqrt{2} i}{8} e^{\sqrt{2} i t} - \frac{\sqrt{2} i}{8} e^{- \sqrt{2} i t}\\\frac{\sqrt{2} i}{4} e^{\sqrt{2} i t} - \frac{\sqrt{2} i}{4} e^{- \sqrt{2} i t} & \frac{1}{4} e^{\sqrt{2} i t} + \frac{1}{2} + \frac{1}{4} e^{- \sqrt{2} i t} & - \frac{\sqrt{2} i}{4} e^{\sqrt{2} i t} + \frac{\sqrt{2} i}{4} e^{- \sqrt{2} i t} & - \frac{1}{4} e^{\sqrt{2} i t} + \frac{1}{2} - \frac{1}{4} e^{- \sqrt{2} i t}\\- \frac{1}{4} e^{\sqrt{2} i t} + \frac{1}{2} - \frac{1}{4} e^{- \sqrt{2} i t} & \frac{t}{2} + \frac{\sqrt{2} i}{8} e^{\sqrt{2} i t} - \frac{\sqrt{2} i}{8} e^{- \sqrt{2} i t} & \frac{1}{4} e^{\sqrt{2} i t} + \frac{1}{2} + \frac{1}{4} e^{- \sqrt{2} i t} & \frac{t}{2} - \frac{\sqrt{2} i}{8} e^{\sqrt{2} i t} + \frac{\sqrt{2} i}{8} e^{- \sqrt{2} i t}\\- \frac{\sqrt{2} i}{4} e^{\sqrt{2} i t} + \frac{\sqrt{2} i}{4} e^{- \sqrt{2} i t} & - \frac{1}{4} e^{\sqrt{2} i t} + \frac{1}{2} - \frac{1}{4} e^{- \sqrt{2} i t} & \frac{\sqrt{2} i}{4} e^{\sqrt{2} i t} - \frac{\sqrt{2} i}{4} e^{- \sqrt{2} i t} & \frac{1}{4} e^{\sqrt{2} i t} + \frac{1}{2} + \frac{1}{4} e^{- \sqrt{2} i t}\end{matrix}\right]$$
In [68]:
Tttau = Matrix(4,4,[x.rewrite(sin).expand().subs(t,t-tau) for x in Tt])
Tttau
Out[68]:
$$\left[\begin{matrix}\frac{1}{2} \cos{\left (\sqrt{2} \left(t - \tau\right) \right )} + \frac{1}{2} & \frac{t}{2} - \frac{\tau}{2} + \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} \left(t - \tau\right) \right )} & - \frac{1}{2} \cos{\left (\sqrt{2} \left(t - \tau\right) \right )} + \frac{1}{2} & \frac{t}{2} - \frac{\tau}{2} - \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} \left(t - \tau\right) \right )}\\- \frac{\sqrt{2}}{2} \sin{\left (\sqrt{2} \left(t - \tau\right) \right )} & \frac{1}{2} \cos{\left (\sqrt{2} \left(t - \tau\right) \right )} + \frac{1}{2} & \frac{\sqrt{2}}{2} \sin{\left (\sqrt{2} \left(t - \tau\right) \right )} & - \frac{1}{2} \cos{\left (\sqrt{2} \left(t - \tau\right) \right )} + \frac{1}{2}\\- \frac{1}{2} \cos{\left (\sqrt{2} \left(t - \tau\right) \right )} + \frac{1}{2} & \frac{t}{2} - \frac{\tau}{2} - \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} \left(t - \tau\right) \right )} & \frac{1}{2} \cos{\left (\sqrt{2} \left(t - \tau\right) \right )} + \frac{1}{2} & \frac{t}{2} - \frac{\tau}{2} + \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} \left(t - \tau\right) \right )}\\\frac{\sqrt{2}}{2} \sin{\left (\sqrt{2} \left(t - \tau\right) \right )} & - \frac{1}{2} \cos{\left (\sqrt{2} \left(t - \tau\right) \right )} + \frac{1}{2} & - \frac{\sqrt{2}}{2} \sin{\left (\sqrt{2} \left(t - \tau\right) \right )} & \frac{1}{2} \cos{\left (\sqrt{2} \left(t - \tau\right) \right )} + \frac{1}{2}\end{matrix}\right]$$
In [69]:
Ttt = Matrix(4,4,[x.rewrite(sin).expand() for x in Tt])
Ttt
Out[69]:
$$\left[\begin{matrix}\frac{1}{2} \cos{\left (\sqrt{2} t \right )} + \frac{1}{2} & \frac{t}{2} + \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} t \right )} & - \frac{1}{2} \cos{\left (\sqrt{2} t \right )} + \frac{1}{2} & \frac{t}{2} - \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} t \right )}\\- \frac{\sqrt{2}}{2} \sin{\left (\sqrt{2} t \right )} & \frac{1}{2} \cos{\left (\sqrt{2} t \right )} + \frac{1}{2} & \frac{\sqrt{2}}{2} \sin{\left (\sqrt{2} t \right )} & - \frac{1}{2} \cos{\left (\sqrt{2} t \right )} + \frac{1}{2}\\- \frac{1}{2} \cos{\left (\sqrt{2} t \right )} + \frac{1}{2} & \frac{t}{2} - \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} t \right )} & \frac{1}{2} \cos{\left (\sqrt{2} t \right )} + \frac{1}{2} & \frac{t}{2} + \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} t \right )}\\\frac{\sqrt{2}}{2} \sin{\left (\sqrt{2} t \right )} & - \frac{1}{2} \cos{\left (\sqrt{2} t \right )} + \frac{1}{2} & - \frac{\sqrt{2}}{2} \sin{\left (\sqrt{2} t \right )} & \frac{1}{2} \cos{\left (\sqrt{2} t \right )} + \frac{1}{2}\end{matrix}\right]$$
In [70]:
u0 = Matrix(4,1,[1,0,1,0])
ftau  = Matrix(4,1,[0,0,cos(2*tau),0])
ft  = Matrix(4,1,[0,0,cos(2*t),0])
u0,ftau, ft
Out[70]:
$$\left ( \left[\begin{matrix}1\\0\\1\\0\end{matrix}\right], \quad \left[\begin{matrix}0\\0\\\cos{\left (2 \tau \right )}\\0\end{matrix}\right], \quad \left[\begin{matrix}0\\0\\\cos{\left (2 t \right )}\\0\end{matrix}\right]\right )$$
In [71]:
u = Ttt*u0 + integrate(Tttau*ftau,(tau,0,t))
In [72]:
u = u.doit()
u
Out[72]:
$$\left[\begin{matrix}- \frac{1}{4} \sin{\left (2 t \right )} + \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} t \right )} + 1\\\frac{\sqrt{2}}{2} \left(- \frac{\sqrt{2}}{2} \cos{\left (2 t \right )} + \frac{\sqrt{2}}{2} \cos{\left (\sqrt{2} t \right )}\right)\\\frac{3}{4} \sin{\left (2 t \right )} - \frac{\sqrt{2}}{4} \sin{\left (\sqrt{2} t \right )} + 1\\- \frac{\sqrt{2}}{2} \left(- \frac{\sqrt{2}}{2} \cos{\left (2 t \right )} + \frac{\sqrt{2}}{2} \cos{\left (\sqrt{2} t \right )}\right)\end{matrix}\right]$$
In [73]:
simplify(u.diff(t) - A*u - ft)
Out[73]:
$$\left[\begin{matrix}0\\0\\0\\0\end{matrix}\right]$$
In [74]:
A = Matrix(2,2,[2,1,0,1])
A
Out[74]:
$$\left[\begin{matrix}2 & 1\\0 & 1\end{matrix}\right]$$
In [75]:
A.exp()
Out[75]:
$$\left[\begin{matrix}e^{2} & - e + e^{2}\\0 & e\end{matrix}\right]$$

Pendelgleichung 2. Versuch

\begin{align} \ddot{y}(t) &= w(t) -y(t) + \cos(2t)\\ \ddot{w}(t) &= y(t) - \mathbf{3}w(t) \end{align}

Die Loesung von $$ \ddot{x} = -B x + g $$ ist gegeben durch $$ x(t) = C_1 \cos(t \sqrt{B}) + C_2 \sqrt{B}^{-1}\sin(t \sqrt{B}) + \int_0^t \sqrt{B}^{-1}\sin((t-\tau) \sqrt{B}) g(\tau) d\tau $$

In [76]:
B = Matrix(2,2,[1,-1,-1,3])
T, D = B.diagonalize()
B,D,T, simplify(B- T*D*T.inv())
Out[76]:
$$\left ( \left[\begin{matrix}1 & -1\\-1 & 3\end{matrix}\right], \quad \left[\begin{matrix}- \sqrt{2} + 2 & 0\\0 & \sqrt{2} + 2\end{matrix}\right], \quad \left[\begin{matrix}1 + \sqrt{2} & - \sqrt{2} + 1\\1 & 1\end{matrix}\right], \quad \left[\begin{matrix}0 & 0\\0 & 0\end{matrix}\right]\right )$$
In [77]:
CosBt = simplify(T*diag(cos(t*sqrt(D[0,0])),cos(t*sqrt(D[1,1])))*T.inv())
SinBttau = simplify(T*diag(sin((t-tau)*sqrt(D[0,0]))/sqrt(D[0,0]),\
                           sin((t-tau)*sqrt(D[1,1]))/sqrt(D[1,1]))*T.inv())

v0 = Matrix(2,1,[1,2])
gtau = Matrix(2,1,[cos(2*tau),0])
gt   = Matrix(2,1,[cos(2*t),0])

display(SinBttau, CosBt, v0, gtau, gt)
$$\left[\begin{matrix}\frac{1}{4} \left(1 + \sqrt{2}\right) \sqrt{\sqrt{2} + 2} \sin{\left (\sqrt{- \sqrt{2} + 2} \left(t - \tau\right) \right )} - \frac{1}{4} \left(- \sqrt{2} + 1\right) \sqrt{- \sqrt{2} + 2} \sin{\left (\sqrt{\sqrt{2} + 2} \left(t - \tau\right) \right )} & \frac{1}{4} \sqrt{\sqrt{2} + 2} \sin{\left (\sqrt{- \sqrt{2} + 2} \left(t - \tau\right) \right )} - \frac{1}{4} \sqrt{- \sqrt{2} + 2} \sin{\left (\sqrt{\sqrt{2} + 2} \left(t - \tau\right) \right )}\\\frac{1}{4} \sqrt{\sqrt{2} + 2} \sin{\left (\sqrt{- \sqrt{2} + 2} \left(t - \tau\right) \right )} - \frac{1}{4} \sqrt{- \sqrt{2} + 2} \sin{\left (\sqrt{\sqrt{2} + 2} \left(t - \tau\right) \right )} & - \frac{1}{4} \left(- \sqrt{2} + 1\right) \sqrt{\sqrt{2} + 2} \sin{\left (\sqrt{- \sqrt{2} + 2} \left(t - \tau\right) \right )} + \frac{1}{4} \left(1 + \sqrt{2}\right) \sqrt{- \sqrt{2} + 2} \sin{\left (\sqrt{\sqrt{2} + 2} \left(t - \tau\right) \right )}\end{matrix}\right]$$
$$\left[\begin{matrix}\frac{\sqrt{2}}{4} \left(\left(1 + \sqrt{2}\right) \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \left(-1 + \sqrt{2}\right) \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\right) & \frac{\sqrt{2}}{4} \left(\cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} - \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\right)\\\frac{\sqrt{2}}{4} \left(\cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} - \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\right) & \frac{\sqrt{2}}{4} \left(\left(-1 + \sqrt{2}\right) \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \left(1 + \sqrt{2}\right) \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\right)\end{matrix}\right]$$
$$\left[\begin{matrix}1\\2\end{matrix}\right]$$
$$\left[\begin{matrix}\cos{\left (2 \tau \right )}\\0\end{matrix}\right]$$
$$\left[\begin{matrix}\cos{\left (2 t \right )}\\0\end{matrix}\right]$$
In [78]:
v = CosBt*v0 + integrate(SinBttau*gtau,(tau,0,t))
v = v.doit()
v
Out[78]:
$$\left[\begin{matrix}\frac{\sqrt{2}}{4} \left(\left(1 + \sqrt{2}\right) \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \left(-1 + \sqrt{2}\right) \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\right) + \frac{\sqrt{2}}{2} \left(\cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} - \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\right) + \frac{3 \sqrt{2} \sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \left(-6 + 4 \sqrt{2}\right)} \cos{\left (2 t \right )} - \frac{3 \sqrt{2} \sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \left(4 \sqrt{2} + 6\right)} \cos{\left (2 t \right )} - \frac{\sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \sqrt{2} + 6} \cos{\left (2 t \right )} - \frac{\sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{-6 + 4 \sqrt{2}} \cos{\left (2 t \right )} + \frac{\sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \sqrt{2} + 6} \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \frac{3 \sqrt{2} \sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \left(4 \sqrt{2} + 6\right)} \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \frac{\sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{-6 + 4 \sqrt{2}} \cos{\left (t \sqrt{\sqrt{2} + 2} \right )} - \frac{3 \sqrt{2} \sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \left(-6 + 4 \sqrt{2}\right)} \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\\\frac{\sqrt{2}}{2} \left(\left(-1 + \sqrt{2}\right) \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \left(1 + \sqrt{2}\right) \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\right) + \frac{\sqrt{2}}{4} \left(\cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} - \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\right) + \frac{\sqrt{2} \sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \left(-6 + 4 \sqrt{2}\right)} \cos{\left (2 t \right )} - \frac{\sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{2 \left(4 \sqrt{2} + 6\right)} \cos{\left (2 t \right )} - \frac{\sqrt{2} \sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \left(4 \sqrt{2} + 6\right)} \cos{\left (2 t \right )} - \frac{\sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{2 \left(-6 + 4 \sqrt{2}\right)} \cos{\left (2 t \right )} + \frac{\sqrt{2} \sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \left(4 \sqrt{2} + 6\right)} \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \frac{\sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{2 \left(4 \sqrt{2} + 6\right)} \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \frac{\sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{2 \left(-6 + 4 \sqrt{2}\right)} \cos{\left (t \sqrt{\sqrt{2} + 2} \right )} - \frac{\sqrt{2} \sqrt{- \sqrt{2} + 2} \sqrt{\sqrt{2} + 2}}{4 \left(-6 + 4 \sqrt{2}\right)} \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\end{matrix}\right]$$
In [79]:
v.simplify()
Out[79]:
$$\left[\begin{matrix}- \frac{1}{2} \cos{\left (2 t \right )} + \frac{3}{4} \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \frac{3 \sqrt{2}}{4} \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} - \frac{3 \sqrt{2}}{4} \cos{\left (t \sqrt{\sqrt{2} + 2} \right )} + \frac{3}{4} \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\\\frac{1}{2} \cos{\left (2 t \right )} + \frac{3}{4} \cos{\left (t \sqrt{- \sqrt{2} + 2} \right )} + \frac{3}{4} \cos{\left (t \sqrt{\sqrt{2} + 2} \right )}\end{matrix}\right]$$
In [80]:
simplify(v.diff(t,2) + B*v - gt ) , v.subs(t,0)-v0
Out[80]:
$$\left ( \left[\begin{matrix}0\\0\end{matrix}\right], \quad \left[\begin{matrix}0\\0\end{matrix}\right]\right )$$