三、假定正态分布,求解1倍标准差和0.5倍标准差的概率?
二、求解多元线性或非线性方程组解
一、求解3元一次方程
1、学习资料 https://github.com/lijin-THU/notes-python/tree/master/04-scipy
2、子模块,即功能
3、学习笔记
二、help(linalg)
dir(linalg) ['absolute_import', 'basic', 'blas', 'block_diag', 'cdf2rdf', 'cho_factor', 'cho_solve', 'cho_solve_banded', 'cholesky', 'cholesky_banded', 'circulant', 'clarkson_woodruff_transform', 'companion', 'coshm', 'cosm', 'cython_blas', 'cython_lapack', 'decomp', 'decomp_cholesky', 'decomp_lu', 'decomp_qr', 'decomp_schur', 'decomp_svd', 'det', 'dft', 'diagsvd', 'division', 'eig', 'eig_banded', 'eigh', 'eigh_tridiagonal', 'eigvals', 'eigvals_banded', 'eigvalsh', 'eigvalsh_tridiagonal', 'expm', 'expm_cond', 'expm_frechet', 'find_best_blas_type', 'flinalg', 'fractional_matrix_power', 'funm', 'get_blas_funcs', 'get_lapack_funcs', 'hadamard', 'hankel', 'helmert', 'hessenberg', 'hilbert', 'inv', 'invhilbert', 'invpascal', 'kron', 'lapack', 'ldl', 'leslie', 'linalg_version', 'logm', 'lstsq', 'lu', 'lu_factor', 'lu_solve', 'matfuncs', 'matrix_balance', 'misc', 'norm', 'null_space', 'ordqz', 'orth', 'orthogonal_procrustes', 'pascal', 'pinv', 'pinv2', 'pinvh', 'polar', 'print_function', 'qr', 'qr_delete', 'qr_insert', 'qr_multiply', 'qr_update', 'qz', 'rq', 'rsf2csf', 'schur', 'signm', 'sinhm', 'sinm', 'solve', 'solve_banded', 'solve_circulant', 'solve_continuous_are', 'solve_continuous_lyapunov', 'solve_discrete_are', 'solve_discrete_lyapunov', 'solve_lyapunov', 'solve_sylvester', 'solve_toeplitz', 'solve_triangular', 'solveh_banded', 'special_matrices', 'sqrtm', 'subspace_angles', 'svd', 'svdvals', 'tanhm', 'tanm', 'test', 'toeplitz', 'tri', 'tril', 'triu']
dir(optimize)
[ 'absolute_import',
'anderson',
'approx_fprime',
'basinhopping',
'bisect',
'bracket',
'brent',
'brenth',
'brentq',
'broyden1',
'broyden2',
'brute',
'check_grad',
'cobyla',
'curve_fit',
'diagbroyden',
'differential_evolution',
'division',
'excitingmixing',
'fixed_point',
'fmin',
'fmin_bfgs',
'fmin_cg',
'fmin_cobyla',
'fmin_l_bfgs_b',
'fmin_ncg',
'fmin_powell',
'fmin_slsqp',
'fmin_tnc',
'fminbound',
'fsolve',
'golden',
'lbfgsb',
'least_squares',
'leastsq',
'line_search',
'linear_sum_assignment',
'linearmixing',
'linesearch',
'linprog',
'linprog_verbose_callback',
'lsq_linear',
'minimize',
'minimize_scalar',
'minpack',
'minpack2',
'moduleTNC',
'newton',
'newton_krylov',
'nnls',
'nonlin',
'optimize',
'print_function',
'ridder',
'root',
'rosen',
'rosen_der',
'rosen_hess',
'rosen_hess_prod',
'show_options',
'slsqp',
'test',
'tnc',
'zeros']
问题:
1、scipy scipy-ref-1.1.0.pdf 中 Unconstrained minimization of multivariate scalar functions 下面
Nelder-Mead Simplex algorithm (method='Nelder-Mead')
不明白这个函数是如何求解的?为啥要这样写?
def rosen(x): return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0+(1-x[:-1])**2.0)
2、如何处理非线性约束问题?
1、如果全部为线性约束问题,所有的线性约束问题都可以转换为矩阵来求解
例如:求解 x-y>0 y>2 线性约束下的 min(x)
2、仅存在一个非线性约束
3、存在多个非线性约束问题
4、同时存在多个非线性约束和线性约束问题
3、 在多元非线性约束下,为什么加入Jacobian和Hessians ?jacobian为非线性约束的导数,Hessians这个函数是如何得出的?
三、假定正态分布,求解1倍标准差和0.5倍标准差的概率?
https://baike.baidu.com/item/%E6%A0%87%E5%87%86%E5%B7%AE/1415772?fr=aladdin
import scipy.stats 1-scipy.stats.norm(0,1).cdf(1) Out[3]: 0.15865525393145707 scipy.stats.norm(0,1).cdf(1) Out[4]: 0.8413447460685429 1-(1-scipy.stats.norm(0,1).cdf(1))*2 Out[5]: 0.6826894921370859 1-(1-scipy.stats.norm(0,1).cdf(2))*2 Out[6]: 0.9544997361036416
一、求解3元一次方程
from scipy import linalg A=np.array([[1,3,5],[2,5,1],[2,3,8]]) b=np.array([10,8,3]) for i in range(1000): x=linalg.solve(A,b) x Out[19]: array([-9.28, 5.16, 0.76])
二、求解多元线性或非线性方程组解
问题:只能得到一个解,不能得到全部解
from scipy.integrate import odeint import numpy as np import matplotlib.pyplot as plt from scipy.optimize import root,fsolve #plt.rc('text', usetex=True) #使用latex ## 使用scipy.optimize模块的root和fsolve函数进行数值求解方程 ## 1、求解f(x)=2*sin(x)-x+1 # rangex1 = np.linspace(-2,8) # rangey1_1,rangey1_2 = 2*np.sin(rangex1),rangex1-1 # plt.figure(1) # plt.plot(rangex1,rangey1_1,'r',rangex1,rangey1_2,'b--') # plt.title('$2sin(x)$ and $x-1$') f1=lambda x:np.sin(x)*2-x+1 sol1_root = root(f1,[0]) print('sol1_root:',sol1_root) print('sol1_root.x',sol1_root.x) sol1_fsolve = fsolve(f1,[0]) print('sol1_fsolve:',sol1_fsolve) print('----------------') # 2、求解线性方程组{3X1+2X2=3;X1-2X2=5} def f2(x): return np.array([3*x[0]+2*x[1]-3,x[0]-2*x[1]-5]) f2=lambda x:np.array([3*x[0]+2*x[1]-3,x[0]-2*x[1]-5]) sol2_root = root(f2,[0,0]) sol2_fsolve = fsolve(f2,[0,0]) print('sol2_fsolve:',sol2_fsolve) # [2. -1.5] a = np.array([[3,2],[1,-2]]) b = np.array([3,5]) x = np.linalg.solve(a,b) print('x:',x) # [2. -1.5] ## 3、求解非线性方程组 def f3(x): return np.array([2*x[0]**2+3*x[1]-3*x[2]**3-7, x[0]+4*x[1]**2+8*x[2]-10, x[0]-2*x[1]**3-2*x[2]**2+1]) sol3_root = root(f3,[0,0,0]) sol3_fsolve = fsolve(f3,[0,0,0]) print('sol3_fsolve:',sol3_fsolve) Backend Qt5Agg is interactive backend. Turning interactive mode on. sol1_root: fjac: array([[-1.]]) fun: array([0.31514905]) message: 'The iteration is not making good progress, as measured by the improvement from the last ten iterations.' nfev: 24 qtf: array([-0.31514905]) r: array([0.00451924]) status: 5 success: False x: array([-1.04882813]) sol1_root.x [-1.04882813] sol1_fsolve: [-1.04882813] sol2_fsolve: [ 2. -1.5] x: [ 2. -1.5] sol3_fsolve: [1.52964909 0.973546 0.58489796]