In [1]:
from sympy import *
init_printing()
import numpy as np
import matplotlib.pyplot as plt
interaktiv
In [ ]:
%matplotlib notebook
für den Druck
In [2]:
%matplotlib inline
In [3]:
x = Symbol('x')
In [4]:
f = sin(x)/x
In [5]:
I1 = Integral(f, x)
I1
Out[5]:
In [7]:
I1.doit()
Out[7]:
In [8]:
plot(Si(x), (x, -8, 8));
In [9]:
Si(6.)
Out[9]:
In [10]:
Integral(sin(x)/x, (x,0,6)).n()
Out[10]:
In [11]:
I3 = Integral(exp(-x**2), x)
I3
Out[11]:
In [12]:
I3.doit()
Out[12]:
In [13]:
plot(erf(x), (x, -3, 3));
In [14]:
f = (1-x**2)**Rational(-3,2)
In [15]:
I4 = Integral(f, x)
I4
Out[15]:
In [16]:
tmp = I4.doit()
tmp
Out[16]:
In [17]:
F = tmp.args[0][0]
F
Out[17]:
In [18]:
F.diff(x) == f
Out[18]:
In [19]:
F.diff(x)
Out[19]:
In [23]:
(F.diff(x) - f).radsimp().expand().ratsimp()
Out[23]:
In [24]:
xn = np.linspace(-.8, .8)
fn = lambdify(x, f)
Fn = lambdify(x, F, 'numpy')
dFn = lambdify(x, F.diff(x), 'numpy')
plt.plot(xn, fn(xn), label='f')
plt.plot(xn, Fn(xn), label='F')
plt.plot(xn, dFn(xn), label='$\\frac{\\partial F}{\\partial x}$')
plt.legend(loc='lower right');
Die Stammfunktion ist falsch. Wir waren aber gewarnt.
In [25]:
I5 = Integral(f, (x, Rational(1,5), Rational(1,2)))
I5
Out[25]:
In [26]:
I5.doit() # AttribureError
In [27]:
y = Symbol('y', real=True)
In [28]:
g = sin(x) * exp(-x*y)
I6 = Integral(g, x)
I6
Out[28]:
In [29]:
G = I6.doit()
G
Out[29]:
In [31]:
G.diff(x)# == g
Out[31]:
In [32]:
G.diff(x).simplify() == g
Out[32]:
In [33]:
x = Symbol('x')
f = sqrt(1+x)/sqrt(1-x**2)
f
Out[33]:
In [34]:
t = f.series(x, 0, 8)
t
Out[34]:
In [35]:
xn = np.linspace(-.8, .8)
fn = lambdify(x, f, 'numpy')
plt.plot(xn, fn(xn), label='f')
for n in range(2, 5): # n=1 führt auf ValueError
tt = t + O(x**n, (x,0))
tn = lambdify(x, tt.removeO(), 'numpy')
plt.plot(xn, tn(xn), label='n='+str(n))
plt.legend(loc='upper left');
Dasselbe für den Arcustangens
In [36]:
xn = np.linspace(-2, 2, 200)
plt.plot(xn, np.arctan(xn), label='arctan')
for n in [4, 20, 60]:
t = atan(x).series(x, 0, n)
tn = lambdify(x, t.removeO(), 'numpy')
plt.plot(xn, tn(xn), label='n='+str(n))
plt.axis(ymin=-2, ymax=2)
plt.legend(loc='upper center');
In [45]:
t = {}
t[1] = atan(x).series(x, -oo, 12)
t[1]
Out[45]:
In [47]:
t[2] = atan(x).series(x, 0, 12)
t[2]
Out[47]:
In [48]:
t[3] = atan(x).series(x, oo, 12)
t[3]
Out[48]:
In [49]:
xn = np.linspace(-3, 3, 200)
for nr, tt in t.items():
tn = lambdify(x, tt.removeO())
plt.plot(xn, tn(xn))
plt.axis(ymin=-2, ymax=2);
In [50]:
x = Symbol('x')
In [51]:
f = 1 - cos(x**2)
g = x*(x-sin(x))
L = Limit(f/g, x, 0)
L
Out[51]:
In [52]:
L.doit()
Out[52]:
In [53]:
fr = f.series(x, 0, 10)
fr
Out[53]:
In [54]:
gr = g.series(x, 0, 10)
gr
Out[54]:
In [55]:
t1 = fr.removeO() / gr.removeO()
t1
Out[55]:
In [56]:
n, z = numer(t1), denom(t1)
t2 = (n/x**4).expand() / (z/x**4).expand()
t2
Out[56]:
In [57]:
t2.subs(x, 0)
Out[57]:
In [58]:
x = Matrix([1,2,3])
x
Out[58]:
In [59]:
y = Matrix(1,3,[4,5,6])
y
Out[59]:
In [60]:
y*x
Out[60]:
In [61]:
x*y
Out[61]:
In [62]:
A = Matrix(3,3,range(1,10))
A
Out[62]:
In [63]:
A[1,1]
Out[63]:
In [64]:
A*x
Out[64]:
In [65]:
A*y # ShapeError
In [66]:
y*A
Out[66]:
In [67]:
B = Matrix(3,3,[1,0,1,-1,-1,-1,0,1,0])
B
Out[67]:
In [68]:
A * B
Out[68]:
In [70]:
np.array(A) * np.array(B)
Out[70]:
In [71]:
A*B -B*A
Out[71]:
In [72]:
eye(5)
Out[72]:
In [74]:
C = A + eye(3)
In [75]:
A.det()
Out[75]:
In [76]:
C.det()
Out[76]:
In [77]:
C**(-1)
Out[77]:
In [78]:
C*C**(-1)
Out[78]:
In [79]:
C**(-1)*C
Out[79]:
In [80]:
A.T # Transponierte
Out[80]:
In [81]:
Matrix([A,B])
Out[81]:
In [82]:
Matrix([A.T, B.T]).T
Out[82]:
In [85]:
A.reshape(1, len(A))#.reshape(3,3) # flatten a matrix
Out[85]:
In [86]:
A
Out[86]:
In [87]:
A[0:2, 1:3]
Out[87]:
In [88]:
A[[0,2], [0,2]]
Out[88]:
In [89]:
def element(i,j):
return 1 + 3*i + j
In [90]:
E = Matrix(3,3, element)
E
Out[90]:
In [91]:
def hilbert_element(i,j):
return 1/(i+j+1)
In [92]:
hilbert = Matrix(5,5,hilbert_element)
hilbert
Out[92]:
In [93]:
hilbert.det()
Out[93]:
In [94]:
hilbert**(-1)
Out[94]:
In [95]:
hilbert = Matrix(60, 60, hilbert_element)
In [96]:
inv_hilbert = hilbert**(-1)
In [97]:
inv_hilbert[29,40]
Out[97]:
In [98]:
def f_hilbert_element(i,j):
return float(hilbert_element(i,j))
f_hilbert = Matrix(60, 60, f_hilbert_element)
In [99]:
inv_f_hilbert = f_hilbert**(-1)
In [100]:
inv_f_hilbert[29,40]
Out[100]:
In [101]:
float(inv_hilbert[29,40])
Out[101]:
In [102]:
float(hilbert.det())
Out[102]:
In [103]:
hd = hilbert.det()
hd
Out[103]:
In [104]:
mpmath.mpf(hd)
Out[104]:
In [ ]: