1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| import numpy as np import math from numpy.linalg import (solve)
m = int(input("输入节点的数量: ")) n = m-1 X = np.zeros((m)) Y = np.zeros((m)) H = np.zeros((m-1)) Mu = np.zeros((m)) Lambda = np.zeros((m)) D = np.zeros((m, 1)) A = np.zeros((m, m)) S = np.zeros((n, 4))
def Deviation(i, j): return (Y[j]-Y[i])/(X[j]-X[i])
opt = input("输入边界条件种类(1~3): ")
for i in range(n+1): x, y = input("输入节点x_%d的值及对应的函数值: " % i).split() X[i] = float(x) Y[i] = float(y) for j in range(n): H[j] = X[j+1]-X[j]
if opt == "1" or opt == "2": diff_0, diff_n = input("输入两个边界点的"+opt+"阶导的值: ").split() diff_0 = float(diff_0) diff_n = float(diff_n) for j in range(1, n): Mu[j] = H[j-1]/(H[j-1]+H[j]) Lambda[j] = 1.0-Mu[j] D[j] = 6*(Deviation(j, j+1)-Deviation(j-1, j))/(H[j-1]+H[j]) if opt == "1": Lambda[0] = Mu[n] = 1 D[0] = 6*(Deviation(0, 1)-diff_0)/H[0] D[n] = 6*(diff_n-Deviation(n-1, n))/H[n-1] else: Lambda[0] = Mu[n] = 0 D[0] = 2*diff_0 D[n] = 2*diff_n for j in range(n): A[j][j] = 2 A[j][j+1] = Lambda[j] A[j+1][j] = Mu[j+1] A[n][n] = 2 M = solve(A, D) for j in range(n): S[j][3] = (M[j+1]-M[j])/(6*H[j]) S[j][2] = (M[j]*X[j+1]-M[j+1]*X[j])/(2*H[j]) S[j][1] = (M[j+1]*math.pow(X[j], 2)-M[j]*math.pow(X[j+1], 2) + 2*(Y[j+1]-Y[j]))/(2*H[j])+(M[j]-M[j+1])*H[j]/6 S[j][0] = (M[j]*math.pow(X[j+1], 3)-M[j+1]*math.pow(X[j], 3)+6 * (Y[j]*X[j+1]-Y[j+1]*X[j]))/(6*H[j])+(M[j+1]*X[j]-M[j]*X[j+1])*H[j]/6 print("S(x)") for j in range(n): print("= (%f*x^3) + (%f*x^2) + (%f*x) + (%f), %f <= x <= %f" % (S[j][3], S[j][2], S[j][1], S[j][0], X[j], X[j+1]))
elif opt == "3": D_temp = np.zeros((m-1, 1)) A_temp = np.zeros((m-1, m-1)) for j in range(1, n): Mu[j] = H[j-1]/(H[j-1]+H[j]) Lambda[j] = 1.0-Mu[j] D[j] = 6*(Deviation(j, j+1)-Deviation(j-1, j))/(H[j-1]+H[j]) Lambda[n] = H[0]/(H[n-1]+H[0]) Mu[n] = 1.0-Lambda[n] D[n] = 6*(Deviation(0, 1)-Deviation(n-1, n))/(H[n-1]+H[0]) for j in range(n): D_temp[j] = D[j+1] for j in range(n-1): A_temp[j][j] = 2 A_temp[j][j+1] = Lambda[j+1] A_temp[j+1][j] = Mu[j+2] A_temp[n-1][n-1] = 2 A_temp[0][n-1] = Mu[1] A_temp[n-1][0] = Lambda[n] M_temp = solve(A_temp, D_temp) M = np.zeros((m, 1)) for j in range(n): M[j+1] = M_temp[j] M[0] = M[n] for j in range(n): S[j][3] = (M[j+1]-M[j])/(6*H[j]) S[j][2] = (M[j]*X[j+1]-M[j+1]*X[j])/(2*H[j]) S[j][1] = (M[j+1]*math.pow(X[j], 2)-M[j]*math.pow(X[j+1], 2) + 2*(Y[j+1]-Y[j]))/(2*H[j])+(M[j]-M[j+1])*H[j]/6 S[j][0] = (M[j]*math.pow(X[j+1], 3)-M[j+1]*math.pow(X[j], 3)+6 * (Y[j]*X[j+1]-Y[j+1]*X[j]))/(6*H[j])+(M[j+1]*X[j]-M[j]*X[j+1])*H[j]/6 difference = float(X[n]-X[0]) print("S(x)") for j in range(n): print("= (%f*x^3) + (%f*x^2) + (%f*x) + (%f), %f+k*%f <= x <= %f+k*%f, k为整数" % (S[j][3], S[j][2], S[j][1], S[j][0], X[j], difference, X[j+1], difference))
|