In [1]:
%autosave 20
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
Autosaving every 20 seconds
In [12]:
from scipy import integrate

def f(x):
    return np.sin(x) / np.sqrt(1-x)

integrate.quad(f, 0, 0.5)[0]
print(integrate.quad(f, 0, 0.9))

x = np.r_[0:0.9:10j]  # np.linspace(0, 1, 11)
y = f(x)
np.trapz(y, x)
# integrate.romberg
(0.6667058512510808, 4.0109229736274246e-10)
Out[12]:
0.6772872862120448
In [24]:
# integrate.odeint
def dydt(t, y):
    k = 1
    a = 0.5
    dxdt = y[1]
    dvdt = -k * y[0] - a * y[1] + 0.05*np.sin(t)
    return [dxdt, dvdt]

result = integrate.solve_ivp(dydt, [0, 20], [0, 1],
                             t_eval=np.r_[0:20:101j])
assert result.success
t = result.t
y = result.y
plt.plot(t, y[0], label='x')
plt.plot(t, y[1], label='v')
plt.legend()

# integrate.solve_bvp
Out[24]:
<matplotlib.legend.Legend at 0x7f5397c69eb8>
In [2]:
from scipy import optimize

def f(x):
    return x**2 - 2

print(optimize.brentq(f, 0, 2))
print(optimize.newton(f, 0))
1.4142135623731364
1.414213562373095
In [4]:
def f(x):
    return x**2 - 2

optimize.minimize(f, [1])
# optimize.least_squares
# optimize.curve_fit
Out[4]:
      fun: -2.0
 hess_inv: array([[0.5]])
      jac: array([0.])
  message: 'Optimization terminated successfully.'
     nfev: 9
      nit: 2
     njev: 3
   status: 0
  success: True
        x: array([-9.51608812e-09])