基于VB的灰色模型预测和线性回归预测.doc
《基于VB的灰色模型预测和线性回归预测.doc》由会员分享,可在线阅读,更多相关《基于VB的灰色模型预测和线性回归预测.doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流基于VB的灰色模型预测和线性回归预测.精品文档. 灰色模型预测GM(1,1)与线性回归预测(一元、多元)新建一个工程,添加一个模块(.bas),两个命令按钮:窗体代码:Option ExplicitPrivate Sub Command1_Click() 灰色模型预测 Dim Data As String Data = 2.67,3.13,3.25,3.36,3.56,3.72 GM1_1_Predict DataEnd SubPrivate Sub Command2_Click() 线性回归预测 Dim X1 As String, X2 A
2、s String, X3 As String, X4 As String, Y As String X1 = 100.38,99.7,92.3,87.6,87.17,88.3,92.75,100.6,90.05; 最后要加上分号; X2 = 53.24,51.5,50.5,52.4,59.6,59.7,65.2,62.4,53.68; 最后要加上分号; X3 = 226,250,281,272,194,180,105,115,250; 最后要加上分号; Y = 644,640,517,425,385,401,448,599,462 最后 不要 加上分号;请注意! Linear_Regressi
3、on_Predict X1 & X2 & X3 & YEnd Sub模块代码:Option ExplicitPrivate Sub Calculate_1_AGO(X_0() As Double, X_1() As Double) 做一次累加生成 1-AGO Dim i As Long, TempX As Double, K As Long K = UBound(X_0) ReDim X_1(K) For i = 0 To K TempX = TempX + X_0(i) X_1(i) = TempX Next iEnd SubPrivate Sub Calculate_Matrix_B(X_
4、1() As Double, B() As Double) 计算数据矩阵B Dim i As Long, K As Long K = UBound(X_1) - 1 ReDim B(K, 1) For i = 0 To K B(i, 0) = -0.5 * (X_1(i) + X_1(i + 1) B(i, 1) = 1 Next iEnd SubPrivate Sub Calculate_Matrix_YN(X_0() As Double, YN() As Double) 计算数据矩阵YN Dim i As Long, K As Long K = UBound(X_0) - 1 ReDim
5、YN(K, 0) For i = 0 To K YN(i, 0) = X_0(i + 1) Next iEnd Sub 函数名:Matrix_Transpotation 功能: 计算矩阵的转置transpotation 参数: m - Integer型变量,矩阵的行数 n - Integer型变量,矩阵的列数 mtxA - Double型m x n二维数组,存放原矩阵 mtxAT - Double型n x m二维数组,返回转置矩阵Private Sub Matrix_Transpotation(mtxA() As Double, mtxAT() As Double) Dim i As Inte
6、ger, j As Integer Dim M As Integer, N As Integer M = UBound(mtxA, 2) N = UBound(mtxA, 1) ReDim mtxAT(M, N) For i = 0 To M For j = 0 To N mtxAT(i, j) = mtxA(j, i) Next j Next iEnd Sub 函数名:Matrix_Multiplication 功能: 计算矩阵的乘法multiplication 参数: m - Integer型变量,相乘的左边矩阵的行数 n - Integer型变量,相乘的左边矩阵的列数和右边矩阵的行数 l
7、 - Integer型变量,相乘的右边矩阵的列数 mtxA - Double型m x n二维数组,存放相乘的左边矩阵 mtxB - Double型n x l二维数组,存放相乘的右边矩阵 mtxC - Double型m x l二维数组,返回矩阵乘积矩阵Private Sub Matrix_Multiplication(mtxA() As Double, mtxB() As Double, mtxC() As Double) Dim i As Integer, j As Integer, K As Integer Dim M As Integer, N As Integer, L As Integ
8、er M = UBound(mtxA, 1): N = UBound(mtxB, 1): L = UBound(mtxB, 2) ReDim mtxC(M, L) For i = 0 To M For j = 0 To L mtxC(i, j) = 0# For K = 0 To N mtxC(i, j) = mtxC(i, j) + mtxA(i, K) * mtxB(K, j) Next K Next j Next iEnd Sub 函数名:Matrix_Inversion 功能: 矩阵求逆 参数: n - Integer型变量,矩阵的阶数 mtxA - Double型二维数组,体积为n
9、x n。存放原矩阵A;返回时存放其逆矩阵A-1。 返回值:Boolean型,失败为False,成功为TruePrivate Function Matrix_Inversion(mtxA() As Double) As Boolean 局部变量 Dim N As Integer N = UBound(mtxA) ReDim nIs(N) As Integer, nJs(N) As Integer Dim i As Integer, j As Integer, K As Integer Dim d As Double, P As Double 全选主元,消元 For K = 0 To N d =
10、0# For i = K To N For j = K To N P = Abs(mtxA(i, j) If (P d) Then d = P nIs(K) = i nJs(K) = j End If Next j Next i 求解失败 If (d + 1# = 1#) Then Matrix_Inversion = False Exit Function End If If (nIs(K) K) Then For j = 0 To N P = mtxA(K, j) mtxA(K, j) = mtxA(nIs(K), j) mtxA(nIs(K), j) = P Next j End If
11、If (nJs(K) K) Then For i = 0 To N P = mtxA(i, K) mtxA(i, K) = mtxA(i, nJs(K) mtxA(i, nJs(K) = P Next i End If mtxA(K, K) = 1# / mtxA(K, K) For j = 0 To N If (j K) Then mtxA(K, j) = mtxA(K, j) * mtxA(K, K) Next j For i = 0 To N If (i K) Then For j = 0 To N If (j K) Then mtxA(i, j) = mtxA(i, j) - mtxA
12、(i, K) * mtxA(K, j) Next j End If Next i For i = 0 To N If (i K) Then mtxA(i, K) = -mtxA(i, K) * mtxA(K, K) Next i Next K 调整恢复行列次序 For K = N To 0 Step -1 If (nJs(K) K) Then For j = 0 To N P = mtxA(K, j) mtxA(K, j) = mtxA(nJs(K), j) mtxA(nJs(K), j) = P Next j End If If (nIs(K) K) Then For i = 0 To N
13、P = mtxA(i, K) mtxA(i, K) = mtxA(i, nIs(K) mtxA(i, nIs(K) = P Next i End If Next K 求解成功 Matrix_Inversion = TrueEnd FunctionPrivate Sub Predicted_Value(ByVal X_1_0 As Double, ByVal u_value As Double, ByVal a_value As Double, K As Long, PV() As Double) Dim i As Long ReDim PV(K) For i = 1 To K + 1 PV(i
14、 - 1) = (X_1_0 - u_value / a_value) * Exp(-a_value * (i - 1) * (1 - Exp(a_value) Next i PV(0) = X_1_0End SubPrivate Sub String_to_Array(Data As String, X_0() As Double) Data字符串转为 X_0 数组,X_0 是原始序列 Dim Predict_Data() As String, K As Long, i As Long Predict_Data = Split(Data, ,) K = UBound(Predict_Data
15、) ReDim X_0(K) For i = 0 To K X_0(i) = Predict_Data(i) Next iEnd SubPrivate Sub Print_Array(Arrays() As Double, Title As String) 打印数组 Dim i As Long Form1.Print vbCrLf & String(25, -) & Title & String(25, -) & vbCrLf For i = 0 To UBound(Arrays) Form1.Print Format(Arrays(i), 0.0000) & ; Next i Form1.P
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 VB 灰色 模型 预测 线性 回归
限制150内