Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

nonlinear least square fit problem

Hello,
 
       I have been trying to fit the data given in the attached file. When you click command button1, the routine goes thru the NonLinearFitModelProcedure, calculates some points and gives an error message like "The return parameter in NonLinearFitCallback must be assigned a scalr value". I do not understand this error message. Any ideas? Thank you.
 
Cem
 
--------------------------------------------------------------
 
Option Explicit
Const numpoints = 49
 
Private Sub CWStat1_NonLinearFitModelProcedure(x As Variant, c As Variant, f As Variant)
    ' Enter the equation to fit in this callback.
    ' The fitter will attempt to find the best set of Coefficients that will fit the model result (f)
    ' to the expected result (y) generated by the GenerateData subroutine below.
    ' In this example, we fit the data to an exponential (f = a * exp(b*x) + c).
    'f = Coefficients(0) * Exp(Coefficients(1) * x) + Coefficients(2)
        If x <= c(1) Then
            f = 0
        ElseIf x >= c(1) And x <= (c(1) + c(2)) Then
            f = 2 * (1# - Exp(-c(0) * (x - c(1))))
        ElseIf x >= (c(1) + c(2)) And x <= (2# + c(2)) Then
            f = 2 * Exp(-c(0) * x) * Exp(c(0) * c(1)) * (Exp(c(0) * c(2)) - 1#)
        End If
        Debug.Print x, f
End Sub
 
Private Sub Form_Load()
 
    Open "fldwhite.txt" For Input As #1
   
End Sub
 
Private Sub Command1_Click()

    'Compute the fit now.
    ComputeFit

End Sub
 
Private Sub GenerateData(x As Variant, Y As Variant)

    Dim i
    Dim dum
        
    Input #1, dum, dum
    ReDim x(numpoints)
    ReDim Y(numpoints)
   
    i = 0
    Do While Not EOF(1)
        i = i + 1
        Input #1, x(i), Y(i)
    Loop
   
End Sub
 
Private Sub ComputeFit()

    Dim x, Y, z, ynoisy, MSError, coef
   
    ' generate sample data
    GenerateData x, Y

    a0 = 0.02
    b0 = 2
    c0 = 2
   
    ' initial guess coefficients
    coef = Array(a0, b0, c0)
   
    ' perform the fit
    ' The function returns the results within coef.
    ' The function also returns a gauge of the results as MSError.
    ' A higher value for this number implies a "worse" fit.

    z = CWStat1.NonLinearFit(x, Y, coef, MSError)
 
    ' plot the results:
    '   Plot1 = Original data
    '   Plot2 = Original data plus noise
    '   Plot3 = Fitted curve
    CWGraph1.PlotXvsY x, CWArray1.BuildArray(Array(Y, z))
   
    ' Display computed coefficints and Mean Squared Error
    a1.Caption = Format(coef(0), "0.0##")
    b1.Caption = Format(coef(1), "0.0##")
    c1.Caption = Format(coef(2), "0.0##")
    mse.Caption = Format(MSError, "0.00E+00")

End Sub
0 Kudos
Message 1 of 3
(7,058 Views)

Hey Cem,

Is there an error number?  I haven't had a lot of time to look into this yet but just off the top of my head it seems that one of your parameters is not declared correctly or needs to be typecast.

Regards,

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
0 Kudos
Message 2 of 3
(7,036 Views)

Hello Sam,

     Thank you for the reply. The error number was something like 10024(?) I found out that my problem was to do with the function definition. The function limits were not set right. When I corrected it worked. But you are right that somehow I do not define the type correctly. See the routine below:

      The xData and yData are defined as variant, likewise the yFit variable. The first two are populated in a for loop, therefore they are redimensioned prior to this. On the other hand the yFit variable is filled as an array by the NonLinearFit routine. This typing works for the fit routine, but it doesn't work for the plot routine where two differently typed variables, yData and yFit are used as arguments of the Array function. In order to make this to work I used CWDSP function for the XData, yData arrays which is an overkill. What is the reasonable solution for type casting these variables? Thank you.

 

Cem

 

Private Sub cmdFit_Click()

Dim i As Integer
Dim j As Integer
Dim k As Integer

Dim NCoeff As Integer
Dim Coefficients
Dim mse


Dim yFit

Dim xData
Dim yData

Dim tArray() As Double
   
    With sprResults
   
        For j = 0 To 4
            
'            xData = CWDSP1.Ramp(nPts(j), 0, 1)
'            yData = CWDSP1.Ramp(nPts(j), 0, 1)


            ReDim xData(0 To nPts(j) - 1)
            ReDim yData(0 To nPts(j) - 1)
           
             For i = 0 To nPts(j) - 1
                    xData(i) = CDbl(sTime(i))
                    yData(i) = CDbl(sData(i, j))
            Next
                     
            fitType = j
            NCoeff = 2
            Coefficients = Array(CoefValues(0), CoefValues(1))
             
            yFit = CWStat.NonLinearFit(xData, yData, Coefficients, mse, 25)
                                                    
            cwgPlot(j).PlotXvsY xData, CWArray.BuildArray(Array(yData, yFit))
   
        Next
   
End With

End Sub
 

0 Kudos
Message 3 of 3
(7,027 Views)