Lektion 6

In [1]:
from sympy import *
init_printing()
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
#%matplotlib inline
x,y,z,a,b,c,d = symbols('x y z a b c d')

Polynome

In [2]:
p = 2*x**2 + 3
q = x+1
d,r = div(p,q)
d,r
Out[2]:
$$\left ( 2 x - 2, \quad 5\right )$$
In [3]:
q = x+y**2
p = 1
degree(q,x), degree(q,y), degree(q) #, degree(p)
Out[3]:
$$\left ( 1, \quad 2, \quad 1\right )$$

besser

In [4]:
p = poly(2*x+y**2,domain=QQ)
p
Out[4]:
$$\operatorname{Poly}{\left( 2 x + y^{2}, x, y, domain=\mathbb{Q} \right)}$$
In [5]:
q = poly(0,x,domain=QQ)
#q = poly(1,x,domain=QQ)
q
Out[5]:
$$\operatorname{Poly}{\left( 0, x, domain=\mathbb{Q} \right)}$$
In [6]:
degree(q) # Achtung
Out[6]:
$$-\infty$$

Loesen von Gleichungen (solve)

In [7]:
gl = Eq((x-1)**2,4-x)
gl
Out[7]:
$$\left(x - 1\right)^{2} = - x + 4$$
In [8]:
lsg = solve(gl,x)
lsg
Out[8]:
$$\left [ \frac{1}{2} + \frac{\sqrt{13}}{2}, \quad - \frac{\sqrt{13}}{2} + \frac{1}{2}\right ]$$
In [9]:
fig = plt.figure()
ax = fig.gca()
xn = np.linspace(-2,3)
ax.plot(xn,lambdify(x,gl.lhs)(xn))
ax.plot(xn,lambdify(x,gl.rhs)(xn))
plt.show()
In [10]:
gl.subs(x,lsg[0])
Out[10]:
$$\mathrm{True}$$
In [11]:
lsg = solve({gl})
lsg
Out[11]:
$$\left [ \left \{ x : \frac{1}{2} + \frac{\sqrt{13}}{2}\right \}, \quad \left \{ x : - \frac{\sqrt{13}}{2} + \frac{1}{2}\right \}\right ]$$
In [12]:
gl.subs(x,lsg[0][x])
Out[12]:
$$\mathrm{True}$$
In [13]:
gl.subs(lsg[0])
Out[13]:
$$\mathrm{True}$$
In [14]:
solve(a*x**2+b*x+c,x)
Out[14]:
$$\left [ \frac{1}{2 a} \left(- b + \sqrt{- 4 a c + b^{2}}\right), \quad - \frac{1}{2 a} \left(b + \sqrt{- 4 a c + b^{2}}\right)\right ]$$
In [15]:
sol = solve(a*x**3+b*x**2+c*x+d,x) #Cardano
sol
Out[15]:
$$\left [ - \frac{- \frac{3}{a} \left(c + 2\right) + \frac{b^{2}}{a^{2}}}{3 \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3}{a} \left(c + 2\right) + \frac{b^{2}}{a^{2}}\right)^{3} + \left(- \frac{54}{a} - \frac{9 b}{a^{2}} \left(c + 2\right) + \frac{2 b^{3}}{a^{3}}\right)^{2}} - \frac{27}{a} - \frac{9 b}{2 a^{2}} \left(c + 2\right) + \frac{b^{3}}{a^{3}}}} - \frac{1}{3} \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3}{a} \left(c + 2\right) + \frac{b^{2}}{a^{2}}\right)^{3} + \left(- \frac{54}{a} - \frac{9 b}{a^{2}} \left(c + 2\right) + \frac{2 b^{3}}{a^{3}}\right)^{2}} - \frac{27}{a} - \frac{9 b}{2 a^{2}} \left(c + 2\right) + \frac{b^{3}}{a^{3}}} - \frac{b}{3 a}, \quad - \frac{- \frac{3}{a} \left(c + 2\right) + \frac{b^{2}}{a^{2}}}{3 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3}{a} \left(c + 2\right) + \frac{b^{2}}{a^{2}}\right)^{3} + \left(- \frac{54}{a} - \frac{9 b}{a^{2}} \left(c + 2\right) + \frac{2 b^{3}}{a^{3}}\right)^{2}} - \frac{27}{a} - \frac{9 b}{2 a^{2}} \left(c + 2\right) + \frac{b^{3}}{a^{3}}}} - \frac{1}{3} \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3}{a} \left(c + 2\right) + \frac{b^{2}}{a^{2}}\right)^{3} + \left(- \frac{54}{a} - \frac{9 b}{a^{2}} \left(c + 2\right) + \frac{2 b^{3}}{a^{3}}\right)^{2}} - \frac{27}{a} - \frac{9 b}{2 a^{2}} \left(c + 2\right) + \frac{b^{3}}{a^{3}}} - \frac{b}{3 a}, \quad - \frac{- \frac{3}{a} \left(c + 2\right) + \frac{b^{2}}{a^{2}}}{3 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3}{a} \left(c + 2\right) + \frac{b^{2}}{a^{2}}\right)^{3} + \left(- \frac{54}{a} - \frac{9 b}{a^{2}} \left(c + 2\right) + \frac{2 b^{3}}{a^{3}}\right)^{2}} - \frac{27}{a} - \frac{9 b}{2 a^{2}} \left(c + 2\right) + \frac{b^{3}}{a^{3}}}} - \frac{1}{3} \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{1}{2} \sqrt{- 4 \left(- \frac{3}{a} \left(c + 2\right) + \frac{b^{2}}{a^{2}}\right)^{3} + \left(- \frac{54}{a} - \frac{9 b}{a^{2}} \left(c + 2\right) + \frac{2 b^{3}}{a^{3}}\right)^{2}} - \frac{27}{a} - \frac{9 b}{2 a^{2}} \left(c + 2\right) + \frac{b^{3}}{a^{3}}} - \frac{b}{3 a}\right ]$$
In [16]:
p = Eq(x**3-5*x**2+3,0)
Lsg = solve(p)
Lsg
Out[16]:
$$\left [ \frac{5}{3} + \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{169}{54} + \frac{\sqrt{419} i}{6}} + \frac{25}{9 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{169}{54} + \frac{\sqrt{419} i}{6}}}, \quad \frac{5}{3} + \frac{25}{9 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{169}{54} + \frac{\sqrt{419} i}{6}}} + \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{169}{54} + \frac{\sqrt{419} i}{6}}, \quad \frac{5}{3} + \frac{25}{9 \sqrt[3]{\frac{169}{54} + \frac{\sqrt{419} i}{6}}} + \sqrt[3]{\frac{169}{54} + \frac{\sqrt{419} i}{6}}\right ]$$
In [17]:
[l.n() for l in Lsg]
Out[17]:
$$\left [ 0.850256587242986 - 1.0 \cdot 10^{-22} i, \quad -0.723956489491132 + 5.0 \cdot 10^{-23} i, \quad 4.87369990224815 - 3.0 \cdot 10^{-21} i\right ]$$
In [18]:
fig = plt.figure()
ax = fig.gca()
xn = np.linspace(-2,6,100)
ax.plot(xn,lambdify(x,p.lhs)(xn))
ax.grid()

Wurzeln von Polynomen

In [19]:
wurzeln = roots(p,trig=True)
wurzeln
Out[19]:
$$\left \{ \frac{5}{3} + \frac{10}{3} \cos{\left (\frac{1}{3} \operatorname{acos}{\left (\frac{169}{250} \right )} \right )} : 1, \quad - \frac{10}{3} \sin{\left (- \frac{1}{3} \operatorname{acos}{\left (\frac{169}{250} \right )} + \frac{\pi}{6} \right )} + \frac{5}{3} : 1, \quad - \frac{10}{3} \cos{\left (- \frac{1}{3} \operatorname{acos}{\left (\frac{169}{250} \right )} + \frac{\pi}{3} \right )} + \frac{5}{3} : 1\right \}$$
In [20]:
[l.n() for l in Lsg], [w.n() for w in wurzeln ]
Out[20]:
$$\left ( \left [ 0.850256587242986 - 1.0 \cdot 10^{-22} i, \quad -0.723956489491132 + 5.0 \cdot 10^{-23} i, \quad 4.87369990224815 - 3.0 \cdot 10^{-21} i\right ], \quad \left [ 4.87369990224815, \quad 0.850256587242986, \quad -0.723956489491132\right ]\right )$$
In [21]:
[p.subs(x,l).simplify() for l in Lsg]
Out[21]:
$$\left [ \mathrm{True}, \quad \mathrm{True}, \quad \mathrm{True}\right ]$$
In [22]:
[p.subs(x,w).expand(trig=True).trigsimp() for w in wurzeln]
Out[22]:
$$\left [ \mathrm{True}, \quad \mathrm{True}, \quad \mathrm{True}\right ]$$
In [23]:
q = Eq(1*x**4-2*x**3-3*x**2+5*x+1,0) 
sol = solve(q,x)
print([l.n() for l in sol])
[w.simplify() for w in roots(q)]
[-1.59615467600863 + 5.48401685356915e-31*I, 1.51572158929134 + 2.67884293265673e-30*I, 2.26307741031324 - 1.54330069972334e-30*I, -0.182644323595948 - 1.6839439182903e-30*I]
Out[23]:
$$\left [ \frac{1}{2} - \frac{1}{12} \sqrt{108 + \frac{204 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}} + 6 \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i}} + \frac{1}{12} \sqrt{216 - 6 \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i} + \frac{72 \sqrt{6}}{\sqrt{18 + \frac{34 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}} + \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i}}} - \frac{204 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}}}, \quad \frac{1}{2} - \frac{1}{12} \sqrt{108 + \frac{204 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}} + 6 \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i}} - \frac{1}{12} \sqrt{216 - 6 \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i} + \frac{72 \sqrt{6}}{\sqrt{18 + \frac{34 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}} + \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i}}} - \frac{204 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}}}, \quad \frac{1}{2} + \frac{1}{12} \sqrt{216 - 6 \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i} - \frac{72 \sqrt{6}}{\sqrt{18 + \frac{34 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}} + \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i}}} - \frac{204 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}}} + \frac{1}{12} \sqrt{108 + \frac{204 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}} + 6 \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i}}, \quad \frac{1}{2} - \frac{1}{12} \sqrt{216 - 6 \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i} - \frac{72 \sqrt{6}}{\sqrt{18 + \frac{34 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}} + \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i}}} - \frac{204 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}}} + \frac{1}{12} \sqrt{108 + \frac{204 \sqrt[3]{18}}{\sqrt[3]{225 + \sqrt{8331} i}} + 6 \sqrt[3]{12} \sqrt[3]{225 + \sqrt{8331} i}}\right ]$$
In [24]:
fig = plt.figure()
ax = fig.gca()
xn = np.linspace(-2,3,100)
ax.plot(xn,lambdify(x,q.lhs)(xn))
Out[24]:
[<matplotlib.lines.Line2D at 0x7fcf1e029358>]
In [25]:
lsg = solve(x**5-x-11) # -> Algebra
lsg
Out[25]:
$$\left [ \operatorname{CRootOf} {\left(x^{5} - x - 11, 0\right)}, \quad \operatorname{CRootOf} {\left(x^{5} - x - 11, 1\right)}, \quad \operatorname{CRootOf} {\left(x^{5} - x - 11, 2\right)}, \quad \operatorname{CRootOf} {\left(x^{5} - x - 11, 3\right)}, \quad \operatorname{CRootOf} {\left(x^{5} - x - 11, 4\right)}\right ]$$
In [26]:
[l.n() for l in lsg]
Out[26]:
$$\left [ 1.66148698080144, \quad -1.2926835755914 - 0.903032173152019 i, \quad -1.2926835755914 + 0.903032173152019 i, \quad 0.461940085190682 - 1.5649989957979 i, \quad 0.461940085190682 + 1.5649989957979 i\right ]$$
In [27]:
n= int(3)
a=Rational(-1,2)
b=3
f = 1/(2**n*factorial(n)) *(1-x)**(-a) *(1+x)**(-b) * diff((1-x)**a * (1+x)**b * (1-x**2)**n,x,n)
f
#f.simplify()
#plot(f,(x,-1/2,3/2))
Out[27]:
$$- \frac{1}{16 \left(x + 1\right)^{3}} \left(16 x^{3} \left(x + 1\right)^{3} + 72 x^{2} \left(x + 1\right)^{2} \left(x^{2} - 1\right) + \frac{12 x^{2} \left(x + 1\right)^{3} \left(x^{2} - 1\right)}{- x + 1} + 24 x \left(x + 1\right)^{3} \left(x^{2} - 1\right) + 36 x \left(x + 1\right) \left(x^{2} - 1\right)^{2} + \frac{18 x \left(x + 1\right)^{2} \left(x^{2} - 1\right)^{2}}{- x + 1} + \frac{9 x \left(x + 1\right)^{3} \left(x^{2} - 1\right)^{2}}{2 \left(- x + 1\right)^{2}} + 18 \left(x + 1\right)^{2} \left(x^{2} - 1\right)^{2} + 2 \left(x^{2} - 1\right)^{3} + \frac{3 \left(x + 1\right)^{3} \left(x^{2} - 1\right)^{2}}{- x + 1} + \frac{3 \left(x + 1\right) \left(x^{2} - 1\right)^{3}}{- x + 1} + \frac{9 \left(x + 1\right)^{2} \left(x^{2} - 1\right)^{3}}{4 \left(- x + 1\right)^{2}} + \frac{5 \left(x + 1\right)^{3} \left(x^{2} - 1\right)^{3}}{8 \left(- x + 1\right)^{3}}\right)$$
In [28]:
sols = solve(f.simplify())
sols
Out[28]:
$$\left [ \frac{7}{17} - \frac{96}{289 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{24192}{63869} + \frac{3456 i}{3757}}} - \frac{1}{3} \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{24192}{63869} + \frac{3456 i}{3757}}, \quad \frac{7}{17} - \frac{1}{3} \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{24192}{63869} + \frac{3456 i}{3757}} - \frac{96}{289 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{24192}{63869} + \frac{3456 i}{3757}}}, \quad \frac{7}{17} - \frac{1}{3} \sqrt[3]{\frac{24192}{63869} + \frac{3456 i}{3757}} - \frac{96}{289 \sqrt[3]{\frac{24192}{63869} + \frac{3456 i}{3757}}}\right ]$$
In [29]:
[im(sol).simplify() for sol in sols]
Out[29]:
$$\left [ 0, \quad 0, \quad 0\right ]$$
In [30]:
[re(sol).simplify() for sol in sols]
Out[30]:
$$\left [ \frac{8 \sqrt{2}}{17} \sin{\left (\frac{1}{6} \left(- 2 \operatorname{atan}{\left (\frac{17}{7} \right )} + \pi\right) \right )} + \frac{7}{17}, \quad \frac{7}{17} + \frac{8 \sqrt{2}}{17} \sin{\left (\frac{1}{6} \left(2 \operatorname{atan}{\left (\frac{17}{7} \right )} + \pi\right) \right )}, \quad - \frac{8 \sqrt{2}}{17} \cos{\left (\frac{1}{3} \operatorname{atan}{\left (\frac{17}{7} \right )} \right )} + \frac{7}{17}\right ]$$
In [31]:
gl = Eq(sin(x),cos(x))
gl
Out[31]:
$$\sin{\left (x \right )} = \cos{\left (x \right )}$$
In [32]:
def glp(lb,ub,gl,nn=500):
    fig = plt.figure()
    ax = fig.gca()
    xn = np.linspace(lb,ub,nn)
    ax.plot(xn,lambdify(x,gl.lhs)(xn))
    ax.plot(xn,lambdify(x,gl.rhs)(xn))
    #plt.show()
    return fig, ax
fig, ax = glp(-5,5,gl)
ax.set_ylim((-1,1));
In [33]:
solve(gl,x)
Out[33]:
$$\left [ - \frac{3 \pi}{4}, \quad \frac{\pi}{4}\right ]$$
In [34]:
solveset(gl,x)
Out[34]:
$$\left\{2 n \pi + \frac{5 \pi}{4}\; |\; n \in \mathbb{Z}\right\} \cup \left\{2 n \pi + \frac{\pi}{4}\; |\; n \in \mathbb{Z}\right\}$$
In [35]:
solveset(exp(x),x) # exp(x) == 0
Out[35]:
$$\emptyset$$
In [36]:
gl = Eq(tan(x),x)
fig, ax = glp(-10,10,gl)
ax.set_ylim([-10,10])
ax.axis([-10,10,-10,10])
Out[36]:
$$\left [ -10, \quad 10, \quad -10, \quad 10\right ]$$
In [37]:
solveset(gl)
Out[37]:
$$\left\{x\; |\; x \in \mathbb{C} \wedge - x + \tan{\left (x \right )} = 0 \right\}$$

Numerische Loesung nichtlinearer Gleichungen

In [38]:
nsolve(gl,1)
Out[38]:
$$0.000348227174421857$$
In [39]:
nsolve(gl,(np.pi, 1.499*np.pi),solver='bisect')
Out[39]:
$$4.49340945790906$$