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
Attachments:
fldwhite.txt ‏1 KB

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

Similar Messages

  • Linear Least-Squares Fit

    Im farely new to programming and I got stuck with a linear least-squares fit problem. I am supposed to develop a method that will calculate slope and intercept of the least-squares line that best fits an input data set.
    Ive been trying to figure out just how to write this program for a couple of days, but with no luck.
    Can anyone give me some tips on how to write this program and what the finished program should look like?
    Any help would be greatly appreciated

    Atleast I believe that I am needed to return a graph. The assignment says
    "Test your method usin a test driver program that calculates the least-squares fit to the following 20-point input data set, and plots both the original input data and the resulting least-squares fit line"
    And then itgives me a chart of the x and y values

  • Problems about nonlinear least square( waiting online)

    This time I met a problem again,as you see when I set the parameters Amplitude=10000 or greater ,then the output will not turn out .
    Another problem is the derivative of the output on the waveform wobble after I click run for a while
    As a man you should be ....
    Attachments:
    ZQH_BYSJ.llb ‏330 KB

    Can you explain what you mean by wobble? (e.g. attach a picture)
    Your toplevel VI only runs once because there is no while loop. How do you run it "for a while" then? I pressed the run button 50 times, but nothing changed....
    If your amplitude is set to 10000, the function has a range of over 10^17, mostly because you take the fourth power. On that same scale, the influence of the lower order terms(a , b) now only contribute an infinitesimal small fraction to the signal (way beyond the scope of DBL or EXT) and thus can no longer be determined. Your problme is out of balance because the C and D completely dominate the result. As explained elsewhere, you need to keep your amplitude in a reasonable range and instead scale the fitting parameters.
    LabVIEW Champion . Do more with less code and in less time .

  • Multivariable least square fitting

    Can Labview, specifically General LS linear Fit VI do the following fitting?
    I have x, y, P as independent variables, and fit a curve B to a function of :
    B = b0 +b1*x +b2* y +b3 *P + b4 *x^2 + b5*y^2 + b6 * P^2 + b7* x*y + b8*x*P + b9 * y*P;
    There are 12 data points to get 10 coefficients, b0-b9.
    I'd appreciate any comments. Thanks,

    You have three independent variables (x,y,P), one dependent variable (B), 10 linear coefficients, and 12 datapoints B(x,y,P) in 3D.
    All coeefficinets are linear, so all you need to do is setup the H matrix and B vector according to your data, then use general linear fit.
    How does the data look like? What does it represent? Can you attach a typical dataset?
    Note: Since you call it "general LS linear fit", you must have an older LabVIEW version, but it is the same as "general linear fit" I mentioned above. It should be able to handle this problem easily. Have you tried?
    LabVIEW Champion . Do more with less code and in less time .

  • Least squares from file

    hi i have a final project i have to write least squares best fit vi,vi should read the datas from a txt file and draw best fit line according to least squares method calculations and also it should show the datas in point form pls help to me i tried a lot but i got nothing...thx

    Hey siktigit ys,
    There are built in Least Square Fit functions in LabVIEW. Here is some information about them. If you are having trouble reading the data from the file, here is an example showing how to use the read and write VIs. In order to show your data in a graph, I'd just read the data from the text files into an array and display that array on an XY graph. That should be enough to get you started. If you need more help, just let me know and have a great weekend!
    John B.
    Embedded Networks R&D
    National Instruments
    Certified LabVIEW Developer

  • The covariance of the least square methods

    what is the meaning of the covariance? In the least square method fitting problems ,if I should make it equal to zero??
    If it isnt equal to zero ? what is the matter?
    thanks for any one can explain it to me !
    As a man you should be ....

    Hi Sharad,
    Try to rename the component name and reload+rebulid the project.
    If there is any build errors try to repair the DC with classpath structure and deploy the DC in server.
    Hope this helps you...
    Regards,
    Saleem Mohammad

  • Least square circle calculation on a series of data points

    I am taking a series of (x,y) part measurements that describe a circle  (radius & rotation angle). The center of the part and center of rotation are offset. Looking for code to do a least square circle calculation on this data set so diameter and out-of-roundness can be estimated. Platform LabView 8.2.

    Here is my simple solution from the above mentione thread:
    (Is it not exaclty "least squares" in the classical sense but the sphere fit works typically very well..)
    LabVIEW Champion . Do more with less code and in less time .

  • Least squares method from file

    hi i have a final project i have to write least squares best fit vi,vi should read the datas from a txt file and draw best fit line according to least squares method calculations and also it should show the datas in point form pls help to me i tried a lot but i got nothing...thx

    Hi,
    I recommend that you search the following websites for assistance:
    www.ni.com
    www.ni.com/community
    Kareem W.
    National Instruments
    Web Product Manager

  • Multi variate damping least square solver

    Hello all,
    I am hoping to get some advice and a starting point for a challenging problem I am working on.
    I have a 33x37 matrix that i need to minimize using a damping least square approach.  in order to achieve the minimization I need to use a set of 50 Matrices (33x37, representing adjustment impacts) that are grouped in 12 separate catagories with different starting points and degrees of freedom, and none of the adjustment matrices are truly independent of each other (they all interact with each other to some extent.)
    For the output I need the minimized matrix and the amount of each vector applied to achieve the minimized solution.
    To hopefully clarify the inputs and outputs I have included an example vi with a dummy set of data.
    I am hoping that someone can give me an idea of where to start, can this be accomplished using the labview libraries or will this have to use scripting via a mathscript node, or matlab?  If it can be done in different ways which method is the most efficient?
    I hope this is enough information, but if you need more details please let me know, although giving exact details can be difficult in this case due to IP considerations.
    As always thank you in advance for any help/suggestions you may be able to offer.
    Chris
    Attachments:
    example2.vi ‏25 KB

    Other then using a least square method I dont have a function defined yet (trying to figure out what function to use), but essentially I am trying to find out how much of each of the adjustment matrices need to be applied to minimize the original matrices..
    I hope that explains what i am trying to do a little better

  • I need to plot the Least Squares Regression Line (not just calculate it with the LINEST function).  How?

    I need to plot the Least Squares Regression Line (not just calculate its values with the LINEST function).  Can anyone advise me on how to graph this line?

      Grapher.app in OS X Utilities is excellent for that : entering data from Numbers Excel etc. into a Grapher "Point Set", plotting all your data, computing and plotting a regression curve linear or exponential or polynomial.
      Unfortunately there is not a single word about point sets and regression in the Grapher Help.
    So, suggest you download (free) "Instruction for Use - Grapher on web site
    http://y.barois.free.fr/grapher/Documentation.html
    and go to page 31 to 37, "Initiation > Lesson 6 : Treating a point set (Regression curve), and also page 68, "Appendix 1. Points sets : from speadsheets to Grapher".
      If you try Grapher's regression curves, please tell us about the manual above : useful or not, too easy or difficult, readable or not etc.
      Thank you.
    YB24
    PS. Documentation about Grapher is also here : Google "Apple Grapher" > http://en.wikipedia.org/wiki/Grapher > Article > Talk > Grapher 1.1 to 2.3 (French and English) (11th september 2012)

  • Why won't Apple at least acknowledge the problems?

    A short while ago I posted expressing my dismay that in trying to contact technical support for my 3g they wanted to charge me $20 just to talk to them (about their defective "upgrade"). Apple deleted it.
    I tried calling my local Apple store and the phone rings and then they hang up on you (I tried twice in succession).
    Apple won't even post a notice that its aware of the problems and is working on a fix.  It's just stranding us out here with "smartphones" that don't work even as they eat up our cellular data plans with mysterious transmissions.
    If Apple cared, it'd at least acknowledge the problem and give an ETA for a fix.

    >After selecting a movie or a TV show and selecting play, there is a long wait and then a message appears, "Apple TV is unable to connect with the servers."  What is going on?
    There is a network problem.  Are you using WiFi?  Have you checked the network connection status in Settings?  Have you run the network test in Settings?

  • Least square linear-phase FIR filter

    I need to geta second order derivative of an array based on 2 stage filtering with a least square linear phase FIR "differentiator " filter. Previously this was done using the matlab routine firls using the "differentiator" tag. Any ideas how this can be done in LabView?
    Thanks in advance.

    I don't believe that LabVIEW has any differentiation functions that use this algorithm. The only derivative function is located in the analyze, signal processing, time domain. You would probably have to build your own.

  • I have a iPad Keyboard Dock for the iPad 1. I purchase an iPad 2 and the dock worked fine for a week no fit problem none worked. I received a tiny message about  iPad Keyboard Dock it stop working immediately after dismissing message.

    I have a iPad Keyboard Dock for the iPad 1. I purchase an iPad 2 and the dock worked fine for a week no fit problem none worked. I received a tiny message about  iPad Keyboard Dock it stop working immediately after dismissing message.

    Hi and welcome..
    Connect your iPad to your computer. Launch iTunes. Make sure your iPad is recognized in iTunes under Devices on the left.
    Select the Summary tab then click Update.
    Just to make sure your iPad software is up to date.
    Carolyn

  • Square root problem

    Problem:
    Using the Newton-Raphson method, find the square root of an input positive integer number (ensure that the input is positive). Compare the result with Math.sqrt (n).
    Hints:
    * Our function is f(x) = x^2 - n where n is the input number. Then, Newton-Raphson method sas that xk+1 is a better approximation for the square root of n than xk where
    xk+1 = (xk + n / xk) ---------->all the k are below the x.
    * You can take x0 as n.
    * Stop when |xk+1 - xk| < e where e is a very small number (take as 1.0E-09).
    Sample execution:
    Type the integer whose square root you would like to compute: 2
    Square root of 2 is 1.414213562373095 by Newton-Raphson method and
    1.4142135623730951 by Math class.
    I didnt understand what is formula of Newton-Raphson method exactly.Therefore, i couldnt apply the method into java.It is my lab assignment please help me until friday morning.Thanks.

    umutcan55 wrote:
    I didnt understand what is formula of Newton-Raphson method exactly.You could [start with the Wikipedia article|http://en.wikipedia.org/wiki/Newton's_method] for example.
    Therefore, i couldnt apply the method into java.So you don't actually have a Java question, right? So this is not the correct forum.
    It is my lab assignment please help me until friday morning.That's irrelevant.

  • Size-to-Fit problem, Planning 9.3.1

    I'm having a problem with a new install of Planning 9.3.1 (with Patch 1).
    When I select "Size-to-Fit" from the options for column size, it doesn't work. The columns don't resize themselves to fit the data, and I can not resize them by dragging the mouse on the column header.
    Things work normally when I select other size options. Very odd.
    Anyone seen this or have an idea what's going on?
    -- Joe
    (P.S. no JavaScript errors indicated in the status bar)

    Joe,
    I can confirm some pretty strange behavior with Size to Fit at my location.
    What I'm seeing in 9.3.1/IE6 is when I select that option are columns that are just a bit under one character wide. The header members are three characters wide.
    I also cannot size the columns in IE6.
    In Firefox 2.0.0.14 (yes, I know, not supported, but I like it a lot better and it works for me) I can resize the column headers but it has no effect on the rows below the header. I can't do this in IE6 (probably shouldn't be able to).
    These are forms that were created natively in Planning 9.3.1.
    Regards,
    Cameron

Maybe you are looking for

  • HT204382 how do I use my foscam for webcam

    I would like to use my FosCam (ip cam -wireless) as my webcam for webex meetings. How do I chnage the video source (imac) OS10.9.1

  • Screen goes blank while using phone

    If this happens with your 3G, I bet it's because it's in a case you purchased for the old model. Take it out of the case and see if it works.

  • Reconnecting files moved to same path on different volume

    I have Photoshop Elements 10 running on Windows Vista. Most of my photos were stored on a network drive (\\mybookworld) which was mapped as drive X:\ . Recently I upgraded my network storage, mapped drive X:\ to the new drive (\\mybooklive), and disp

  • SQLServer to oracle migration. Issue with table having image data.

    Hi, I am using the SQL Developer version 1.5.0.53 Build MAIN-53.38. I am trying to migrate from sql server database to oracle. Sql server database version is 2005 and oracle database version is 10g (10.1.0.2 and 10.2.0.3). Both Oracle and Sql server

  • Master -Child Report

    Dear all, Here I have implememented the matser child report navigation using the interaction option in OBIEE 11g.The scenario is working fine but I am getting the data misamtch in the child report when I clicked on the master report colum. What could