此函数为“直线-三次抛物线拟合”算法( 见西安交通大学《电机测试技术》)

2019-07-17 22:51发布

这段多上的带码如何实现,求大神
Public Sub LinearAndCubicFit(X() As Single, Y() As Single, n As Integer, n1 As Integer, n2 As Integer)
   
'**** 此函数为“直线-三次抛物线拟合”算法( 见西安交通大学《电机测试技术》)****
    Dim n∑Xi As Single
    Dim n∑XiE2 As Single
    Dim n∑Yi As Single
    Dim n∑XiYi As Single
   
   
    Dim n1∑Xi As Single
    Dim n1∑Yi As Single
    Dim n1∑XiE2 As Single
    Dim n1∑XiYi As Single
   
    Dim n2∑XiE2 As Single
    Dim n2∑XiE3 As Single
    Dim n2∑XiE4 As Single
    Dim n2∑XiE5 As Single
    Dim n2∑XiE6 As Single
    Dim n2∑XiE2Yi As Single
    Dim n2∑XiE3Yi As Single
   
    Dim A(1 To 3, 1 To 3) As Single     '方程组“系数矩阵”
    Dim C(1 To 3) As Single             '方程组“常数项向量”
    Dim B() As Single
    ReDim B(1 To 3) As Single           '方程组“解向量”
   
    Dim Q_13(1 To 20) As Single         '直线-三次抛物线拟合之“残差平方和”
    Dim n1∑ViE2 As Single
    Dim n2∑ViE2 As Single
   
    Dim i, j As Integer
    Dim X0 As Single
   
    For i = 1 To n
        n∑Xi = n∑Xi + X(i)
        n∑Yi = n∑Yi + Y(i)
        n∑XiYi = n∑XiYi + X(i) * Y(i)
        n∑XiE2 = n∑XiE2 + X(i) ^ 2
    Next i
   
    DoEvents
    For i = n2 + 1 To n
        n1∑Xi = n1∑Xi + X(i)
        n1∑Yi = n1∑Yi + Y(i)
        n1∑XiE2 = n1∑XiE2 + X(i) ^ 2
        n1∑XiYi = n1∑XiYi + X(i) * Y(i)
    Next i
   
    DoEvents
    For i = 1 To n2
        n2∑XiE2 = n2∑XiE2 + X(i) ^ 2
        n2∑XiE3 = n2∑XiE3 + X(i) ^ 3
        n2∑XiE4 = n2∑XiE4 + X(i) ^ 4
        n2∑XiE5 = n2∑XiE5 + X(i) ^ 5
        n2∑XiE6 = n2∑XiE6 + X(i) ^ 6
        n2∑XiE2Yi = n2∑XiE2Yi + X(i) ^ 2 * Y(i)
        n2∑XiE3Yi = n2∑XiE3Yi + X(i) ^ 3 * Y(i)
    Next i
   
    DoEvents
    j = 1
    For X0 = 0.3 To 1.1 Step 0.05
        A(1, 1) = n
        A(1, 2) = n∑Xi: A(2, 1) = A(1, 2)
        A(1, 3) = n2∑XiE3 - 3 * X0 * n2∑XiE2 - 3 * X0 ^ 2 * n1∑Xi + n1 * X0 ^ 3: A(3, 1) = A(1, 3)
        A(2, 2) = n∑XiE2
        A(2, 3) = n2∑XiE4 - 3 * X0 * n2∑XiE3 - 3 * X0 ^ 2 * n1∑XiE2 + X0 ^ 3 * n1∑Xi: A(3, 2) = A(2, 3)
        A(3, 3) = n2∑XiE6 - 6 * X0 * n2∑XiE5 + 9 * X0 ^ 2 * n2∑XiE4 + 9 * X0 ^ 4 * n1∑XiE2 - 6 * X0 ^ 5 * n1∑Xi + n1 * X0 ^ 6
        C(1) = n∑Yi
        C(2) = n∑XiYi
        C(3) = n2∑XiE3Yi - 3 * X0 * n2∑XiE2Yi - 3 * X0 ^ 2 * n1∑XiYi + X0 ^ 3 * n1∑Yi
            
        B = SolvingEquations(A(), C(), 3)     '调解方程组函数
        
        Dim ValueTemp As Single
        For i = n2 + 1 To n
             ValueTemp = Y(i) - (B(1) + B(2) * X(i) + B(3) * (X0 ^ 3 - 3 * X0 ^ 2 * X(i)))
             ValueTemp = ValueTemp ^ 2
             n1∑ViE2 = n1∑ViE2 + ValueTemp
        Next i
        
        For i = 1 To n2
            ValueTemp = Y(i) - (B(1) + B(2) * X(i) + B(3) * (X(i) ^ 3 - 3 * X0 * X(i) ^ 2))
            ValueTemp = ValueTemp ^ 2
            n2∑ViE2 = n2∑ViE2 + ValueTemp
        Next i
        
        Q_13(j) = n1∑ViE2 + n2∑ViE2
        If Q_13(1) >= Q_13(j) Then
            Q_13(1) = Q_13(j)
            P_X0 = X0
            AA(0) = B(1) + B(3) * P_X0 ^ 3
            AA(1) = B(2) - 3 * B(3) * P_X0 ^ 2
            BB(0) = B(1)
            BB(1) = B(2)
            BB(2) = -3 * B(3) * P_X0
            BB(3) = B(3)
       End If
      
       j = j + 1
       n1∑ViE2 = 0: n2∑ViE2 = 0
    Next X0
   
    P_QLineCurve = Q_13(1)
   
End Sub

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。