In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

In [2]:
x = np.linspace(0,7,10)
x


Out[2]:
array([ 0.        ,  0.77777778,  1.55555556,  2.33333333,  3.11111111,
        3.88888889,  4.66666667,  5.44444444,  6.22222222,  7.        ])

In [3]:
f = np.sin(x)

In [5]:
plt.plot(x,f)


Out[5]:
[<matplotlib.lines.Line2D at 0x7f1bedd65910>]

In [8]:
for i in xrange(x.shape[0]-1):
    #print f[i], f[i+1]
    if f[i]*f[i+1]<=0:
        #print x[i], x[i+1]
        a=(f[i+1]-f[i])/(x[i+1]-x[i])
        b=f[i]-a*x[i]
        x_0=-b/a
        print x_0


-0.0
3.14449096517
6.28822804578

$$F(x) = \int_c^x f(x')dx'$$


In [9]:
timeit('np.zeros(1000000,dtype=np.float64)')


100000000 loops, best of 3: 7.18 ns per loop

In [10]:
a = np.empty(1000000,dtype=np.float64)

In [11]:
timeit('a.fill(0.0)')


100000000 loops, best of 3: 7.18 ns per loop

Maximum


In [12]:
np.max(f)


Out[12]:
0.99988386169410237

In [13]:
f[np.argmax(f)]


Out[13]:
0.99988386169410237

In [14]:
x[np.argmax(f)]


Out[14]:
1.5555555555555556

In [15]:
x[np.argmin(f)]


Out[15]:
4.666666666666667

pochodna

array slicing


In [16]:
x[1:4]


Out[16]:
array([ 0.77777778,  1.55555556,  2.33333333])

In [17]:
x[1:6:2]


Out[17]:
array([ 0.77777778,  2.33333333,  3.88888889])

In [18]:
x[::2]


Out[18]:
array([ 0.        ,  1.55555556,  3.11111111,  4.66666667,  6.22222222])

In [19]:
x[:-1],x[-1]


Out[19]:
(array([ 0.        ,  0.77777778,  1.55555556,  2.33333333,  3.11111111,
         3.88888889,  4.66666667,  5.44444444,  6.22222222]), 7.0)

In [20]:
x.shape


Out[20]:
(10,)

In [21]:
x[1:].shape


Out[21]:
(9,)

In [22]:
x[:-1].shape


Out[22]:
(9,)

In [34]:
x = np.linspace(0,7,10)
f = np.sin(x)

In [35]:
h = x[1]-x[0]
fdiff = (f[1:]-f[:-1])/h

In [36]:
fdiff.shape, (x[:-1]+x[1:]).shape


Out[36]:
((9,), (9,))

In [38]:
y = np.linspace(0,7,230)
plt.plot((x[:-1]+x[1:])/2.0,fdiff, 'o-') 
plt.plot( y,np.cos(y))


Out[38]:
[<matplotlib.lines.Line2D at 0x7f1bed1180d0>]

In [39]:
f[1:]-f[:-1]


Out[39]:
array([ 0.70169788,  0.29818599, -0.27679798, -0.69260906, -0.71013478,
       -0.31929696,  0.25515281,  0.68287677,  0.71791193])

In [40]:
np.diff(f)


Out[40]:
array([ 0.70169788,  0.29818599, -0.27679798, -0.69260906, -0.71013478,
       -0.31929696,  0.25515281,  0.68287677,  0.71791193])

pierwotna

całka


In [41]:
h = x[1]-x[0]
w  = h*np.ones_like(f)
w[0] = w[0]/2.0
w[-1] = w[-1]/2.0
np.sum(f*w)


Out[41]:
0.23356467160546901

In [43]:
# sage integrate(sin(y),(y,0,7)).n()

In [46]:
x = np.linspace(0,7,41)
h = x[1]-x[0]
f = np.sin(x)
plt.plot(x,np.cumsum(f*h))
plt.plot(y,-np.cos(y)+1)


Out[46]:
[<matplotlib.lines.Line2D at 0x7f1becfc79d0>]

In [33]:



Out[33]: