数值分析
本专栏为西安交通大学的《数值计算》研究生教材里面提供的计算方法撰写Python程序。
leetteel
西安交通大学硕士
展开
-
计算初始分布不规律的两组数据的一次函数关系
% 计算数组c,d之间的关系;[q,w]=sort(c,1);f=d(w);coefficient=polyfit(q,f,1); %用一次函数拟合曲线,想用几次函数拟合,就把第三个参数设成那个数y1=polyval(coefficient,q)plot(q,f,'o',q,y1,'-')原创 2021-04-12 19:23:02 · 250 阅读 · 0 评论 -
MATLAB计算积分的解析解
syms y x t;y=x+t;z=int(y,x,0,10);z=subs(z,t,3);原创 2021-04-12 19:14:20 · 1839 阅读 · 0 评论 -
二阶初值问题常微分方程的求解
import numpy as npfrom matplotlib import pyplot as pltplt.rcParams['font.sans-serif']=['SimHei']plt.rcParams['axes.unicode_minus'] = Falsedef myfun1(x, y1, y2): return y2def myfun2(x,y1, y2): return np.exp(2*x)*np.sin(x)-2*y1+2*y2def od原创 2021-01-04 17:32:13 · 2060 阅读 · 0 评论 -
线性最小二乘问题
import numpy as npdef fgauss(A, b): n = A.shape[0] zengguan = np.hstack((A, b.T)) ra = np.linalg.matrix_rank(A) rz = np.linalg.matrix_rank(zengguan) temp1 = rz - ra if temp1 > 0: print("无一般意义下的解,系数矩阵与增广矩阵的秩不同")原创 2021-01-09 13:32:31 · 153 阅读 · 0 评论 -
三阶初值问题常微分方程的求解
import numpy as npfrom matplotlib import pyplot as pltplt.rcParams['font.sans-serif']=['SimHei']plt.rcParams['axes.unicode_minus'] = Falsedef myfun1(x, y1, y2, y3): return y2def myfun2(x, y1, y2, y3): return y3def myfun3(x,y1, y2, y3):原创 2021-01-09 13:21:31 · 1090 阅读 · 0 评论 -
开方运算
def kaifa(x, n, error): x0 = x err = 1 x = x0/n if x0 == 1: x = 1 elif x == 0: x == 0 elif x < 0: print('输入参数错误!') return 0 else: while err > 10**(-1*error): x = x - (x**原创 2021-01-06 19:31:33 · 249 阅读 · 0 评论 -
复化求积公式计算积分
import numpy as npdef myfun(x): if x == 0: return 1 else: return np.sin(x)/xdef tixing(fun, a, b, h): n = int((b-a)/h)+1 x = np.linspace(a, b, n) sum1 = 0 for i in range(1, n-1): sum1 = sum1 + h*fun(x[i原创 2021-01-05 10:15:00 · 380 阅读 · 0 评论 -
迭代法加速收敛技术
print('-'*22, '不使用加速收敛技术', '-'*22)err = 1x = 1.5k = 0while err > 0.000000001: k = k + 1 x0 = x x = (3*x-1)**(1/3) err = abs(x - x0) if err < 0.000000001: print('第{:<2}次迭代结果为{:.7},误差为{}'.format(k, x, err))print('-'*原创 2021-01-04 21:33:45 · 1109 阅读 · 0 评论 -
4级4阶的龙格-库塔法
def myfun(x, y): return -1*(0.9*y)/(1+2*x)def rk(h, x, y, s, b, myfun): x_r = [] for i in range(int(b/h)): x_r.append(x) k1 = h * myfun(x, y) k2 = h * myfun(x + h/2, y+0.5*k1) k3 = h * myfun(x + h/2, y+0.5*k2)原创 2021-01-04 17:00:25 · 536 阅读 · 0 评论 -
求矩阵全部特征值和特征向量的QR方法
import numpy as npimport mathdef sgn1(x): if x > 0: return 1 elif x == 0: return 0 elif x < 0: return -1ai2 = np.mat([[-1, 2, 1], [2, -4, 1], [1, 1, -6]], dtype=float)n = ai2.shape[0原创 2021-01-03 21:32:36 · 1538 阅读 · 6 评论 -
反幂法
import numpy as npdef lufenjie(a): n = a.shape[0] L = np.mat(np.zeros((n, n), dtype=float)) for i in range(n): L[i, i] = 1 u = np.mat(np.zeros((n, n), dtype=float)) u[0, :] = a[0, :] L[1:n, 0] = a[1:n, 0]/u[0, 0] for原创 2021-01-03 18:12:23 · 615 阅读 · 0 评论 -
艾特肯加速方法加速乘幂法收敛
import numpy as npz0 = np.mat([1, 1, 1])z0 = z0.Terr = 1A = np.mat([[-1, 2, 1], [2, -4, 1], [1, 1, -6]], dtype=float)m_r = []k = 0print('乘幂法:')while err > 0.00000000001: y = A * z0 y1 = y.copy() y1 = abs(y1)原创 2021-01-03 17:36:08 · 1754 阅读 · 0 评论 -
乘幂法
import numpy as npz0 = np.mat([1, 1, 1])z0 = z0.Terr = 1A = np.mat([[-1,2,1], [2,-4,1], [1,1,-6]], dtype=float)m_r = []k = 0while err > 0.0000001: y = A * z0 y1 = y.copy() y1 = abs(y1) a = y1.argmax()原创 2021-01-03 16:57:02 · 1389 阅读 · 1 评论 -
牛顿法解非线性方程组
import numpy as npdef fgauss(A, b): n = A.shape[0] zengguan = np.hstack((A, b.T)) ra = np.linalg.matrix_rank(A) rz = np.linalg.matrix_rank(zengguan) temp1 = rz - ra if temp1 > 0: print("无一般意义下的解,系数矩阵与增广矩阵的秩不同")原创 2021-01-03 15:20:56 · 473 阅读 · 0 评论 -
简单迭代法求解非线性方程组
高斯-塞德尔迭代import numpy as npx = [0, 0]err = 1k = 0while err > 0.0000001: k = k+1 x[0] = 0.25*(1+x[1]-0.1*np.exp(x[0])) x[1] = 0.25*(x[0]-(1/8)*x[0]*x[0]) err = max(abs(4*x[0]-x[1]+0.1*np.exp(x[0])-1), abs(-x[0]+4*x[1]+x[0]*x[0]/8))原创 2021-01-03 14:42:37 · 2130 阅读 · 0 评论 -
牛顿两点线割迭代法求解非线性方程
from matplotlib import pyplot as pltimport numpy as npdef fun(x): return x**3-2*x-5def dfun(x): return 3*x*x-2def newton(fun,dfun,a,b,eps): err = 1 x = b k = 0 lada = 1 x_r = [] x_r.append(x) x = x - fun(x) / d原创 2021-01-03 11:57:16 · 218 阅读 · 0 评论 -
牛顿单点线割迭代法求解非线性方程
from matplotlib import pyplot as pltimport numpy as npdef fun(x): return x**3-2*x-5def dfun(x): return 3*x*x-2def newton(fun,dfun,a,b,eps): err = 1 x = b k = 0 lada = 1 x_r = [] x_r.append(x) x = x - fun(x) / d原创 2021-01-03 11:48:21 · 272 阅读 · 0 评论 -
牛顿下山法求解非线性方程
from matplotlib import pyplot as pltimport numpy as npdef fun(x): return x**3-2*x-5def dfun(x): return 3*x*x-2def newton(fun,dfun,a,b,eps): err = 1 x = b k = 0 lada = 1 x_r = [] while err > eps: x_r.appe原创 2021-01-03 11:35:16 · 427 阅读 · 0 评论 -
简化牛顿法求解非线性方程
import numpy as npdef fun(x): return x**3-2*x-5def dfun(x): return 3*x*x-2def newton(fun,dfun,a,b,eps): err = 1 x = b c = dfun(x) k = 0 while err > eps: x = x - fun(x)/c err = fun(x) k = k+1原创 2021-01-03 11:14:46 · 442 阅读 · 0 评论 -
牛顿迭代法求解非线性方程
from matplotlib import pyplot as pltimport numpy as npdef fun(x): return x**3-2*x-5def dfun(x): return 3*x*x-2def newton(fun,dfun,a,b,eps): err = 1 x = b while err > eps: x = x - fun(x)/dfun(x) err = fun(x)原创 2021-01-03 11:07:01 · 1043 阅读 · 0 评论 -
二分法求解非线性方程
def fun(x): return x**3-2*x-5def erfenfa(fun,a,b,e): err = 1 a = 2 b = 3 while err > e: x = (a+b)/2 err = abs(fun(x)) if fun(a)*fun(x) < 0: b = x else: a = x return x原创 2021-01-03 09:41:01 · 712 阅读 · 0 评论 -
最速下降法
最速下降法算法流程最速下降法的优缺点代码import numpy as npfrom matplotlib import pyplot as pltplt.rcParams['font.sans-serif']=['SimHei']plt.rcParams['axes.unicode_minus'] = Falsedef newton(A, b, error,nmax): n1 = A.shape[0] x = np.mat(np.zeros((n1, 1)))原创 2020-12-29 15:51:16 · 1366 阅读 · 0 评论 -
有限差分求导法
原创 2020-12-19 20:28:55 · 872 阅读 · 1 评论 -
数值积分法(2)————龙格-库塔法
3.龙格-库塔法# 改进欧拉法import numpy as npfrom matplotlib import pyplot as plth = 0.2x = 1x_r = []t = 0for i in np.linspace(h, 1, 10): x_r.append(x) k1 = h * (x - 2*t/x) x1 = x + k1/2 k2 = h * (x1 - 2*(t+h/2)/x1) x2 = x + k2 / 2 k3 =原创 2020-12-19 12:04:54 · 727 阅读 · 1 评论 -
数值积分法(1)————欧拉法
1.欧拉法import numpy as npfrom matplotlib import pyplot as plth = 0.0001x = 1x_r = []for i in np.linspace(0, 1, 100000): x_r.append(x) x = x + x*hx_1 = [h*(i+1) for i in range(len(x_r))]e_1 = [np.exp(i) for i in x_1]plt.plot(x_1, x_r, 'ks')p原创 2020-12-19 11:34:35 · 1792 阅读 · 1 评论 -
Python数值积分与数值微分(1)————龙贝格积分法
#龙贝格积分法函数import numpy as npfrom matplotlib import pyplot as pltfrom mpl_toolkits.axes_grid1.inset_locator import inset_axesdef romberg(fun, a, b): sum = 0 t1 = ((b - a)*(fun(a)+fun(b)))/2 for i in range(1, 2): sum = sum + fun(a+(原创 2020-11-15 09:04:20 · 770 阅读 · 1 评论 -
Python函数最优逼近(1)————最小二乘拟合函数
import numpy as npfrom matplotlib import pyplot as pltdef fgauss(A, b): n = A.shape[0] zengguan = np.hstack((A, b.T)) ra = np.linalg.matrix_rank(A) rz = np.linalg.matrix_rank(zengguan) temp1 = rz - ra if temp1 > 0: pr原创 2020-11-13 23:23:45 · 970 阅读 · 0 评论 -
Python插值法(6)————几种插值方法的比较
这里对三次样条插值函数,拉格朗日插值多项式,牛顿插值多项式这三种插值方法进行比较代码import numpy as npfrom sympy import *from matplotlib import pyplot as pltdef lagrange(x, y): p, la = symbols('p la') n = len(x) s = 0 for k in range(n): la = y[k] for j in ra原创 2020-11-11 23:34:25 · 3079 阅读 · 1 评论 -
Python插值法(5)————三次样条插值函数
代码import numpy as npfrom sympy import *from matplotlib import pyplot as pltdef spline(x, y, t): n = len(x) h = [x[i]-x[i-1] for i in range(1, n)] A = np.eye(n-2) A = A*2 u1 = [h[i]/(h[i]+h[i+1]) for i in range(n-2)] la1 = [1-u原创 2020-11-11 23:09:11 · 2213 阅读 · 1 评论 -
Python插值法(4)————hermite插值多项式(牛顿型)
代码在这里插入代码片结果如下图所示原创 2020-11-11 19:03:46 · 2723 阅读 · 0 评论 -
Python插值法(3)————hermite插值多项式(拉格朗日型)
代码import numpy as npfrom sympy import *from matplotlib import pyplot as pltdef hermite(x, y, dy): p, la = symbols('p la') f = 0 n = len(x) for i in range(n): la = 1 lp = 0 for j in range(n): if j !原创 2020-11-10 23:25:34 · 2094 阅读 · 0 评论 -
Python插值法(2)————牛顿插值多项式
代码import numpy as npfrom sympy import *from matplotlib import pyplot as pltdef newton(x, y): n = len(x) c = np.zeros((n, n)) print(c) for i in range(n): c[i, 0] = y[i] for i in range(1, n): for j in range(i, n):原创 2020-11-10 21:58:49 · 1621 阅读 · 0 评论 -
Python插值法(1)————拉格朗日插值多项式
#代码原创 2020-11-10 19:57:54 · 1821 阅读 · 0 评论 -
sympy符号计算库
引言SymPy是用于符号数学的 Python 库。详细教程见目录引言加载sympy库定义符号,替换代码结果代码结果展开代码结果加载sympy库from sympy import *定义符号,替换代码x = symbols('x ')str_expr = 'x**2 + 2*x + 1'expr = sympify(str_expr)y = expr*expr+1print(expand(y))print(y.subs(x, 1))结果代码x = symbols('x ')原创 2020-11-10 19:24:31 · 186 阅读 · 0 评论 -
Python解线性方程组的迭代法(4)————共轭梯度法
待写原创 2020-10-31 23:28:51 · 1145 阅读 · 1 评论 -
Python解线性方程组的迭代法(3)————逐次超松弛(SOR)迭代法
待写原创 2020-10-31 21:59:22 · 2120 阅读 · 0 评论 -
Python解线性方程组的迭代法(2)————高斯-赛德尔(Gauss-Seidel)迭代法
待写原创 2020-10-26 21:24:29 · 1153 阅读 · 0 评论 -
Python解线性方程组的迭代法(1)————雅克比(Jacobi)迭代法
待写原创 2020-10-26 21:04:22 · 2484 阅读 · 0 评论 -
Python解线性方程组的直接法(8)————基于豪斯霍尔德变换的QR分解
基于豪斯霍尔德变换的QR分解# Householderimport numpy as npdef householder(a): temp = 1 m = a.shape[0] n = a.shape[1] for i in range(0, n-1): x = a[i:n, i] print("x向量为:") print(x) e = np.mat(np.zeros((n-i, 1), dtype原创 2020-10-13 10:25:51 · 1260 阅读 · 0 评论 -
Python解线性方程组的直接法(7)————基于吉文斯变换的QR分解
基于吉文斯变换的QR分解# 基于吉文斯变换的QR分解import numpy as npdef givens(A): m = A.shape[0] n = A.shape[1] P = np.mat(np.eye(n)) p1 = P for i in range(0, n-1): for j in range(i+1, n): if A[i, j] == 0: continue原创 2020-10-12 22:40:34 · 1457 阅读 · 8 评论