LabVIEW论坛
直播中

loge2012

9年用户 49经验值
擅长:测量仪表
私信 关注
[问答]

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

这段多上的带码如何实现,求大神
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

已退回5积分

回帖(2)

chenwei6991627

2016-7-17 09:40:35
使用VB写的吗?
举报

loge2012

2016-7-18 09:55:49
本帖最后由 loge2012 于 2016-7-18 09:56 编辑
引用: chenwei6991627 发表于 2016-7-17 09:40
使用VB写的吗?

对,VB写的 网上也可以搜到的,准备用labview来做软件,好多这样的代码无从下手
举报

更多回帖

发帖
×
20
完善资料,
赚取积分