In [1]:
%autosave 20
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
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
Out[12]:
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]:
In [2]:
from scipy import optimize
def f(x):
return x**2 - 2
print(optimize.brentq(f, 0, 2))
print(optimize.newton(f, 0))
In [4]:
def f(x):
return x**2 - 2
optimize.minimize(f, [1])
# optimize.least_squares
# optimize.curve_fit
Out[4]:
In [ ]: