Results 1 to 4 of 4

Thread: fitting function to data by adjusting variables

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    234

    fitting function to data by adjusting variables

    Hi, I've posted this before, but I think due to my approach not many people were willing to contribute their time. This time I am trying a different approach to my problem, and I would greatly appreciate it if anyone could offer any advice or experience in similar problem.
    Here is the previous thread: http://vbforums.com/showthread.php?t=487398 Caution: do not read into the details unless you are willing to sacrifice some serious brain cells to optical theory.
    Basically, I have this function that uses a few parameters to describe real data, and I need to figure out some of those parameters to make the function fit the data.
    Code:
    Function Rpc(Lambda As Single, SubstIndex As Single, n As Single, FilmProperty As Variant)
    'FilmProperty(2N) made of R(1), T(1), R(2), T(2), ..., R(N), T(N).
    'R(I) = FilmIndex
    'T(I) = FilmThickness
    'The layers are numbered starting from the one most away from the substrate.
    'MEDIUM INDEX = R(0)
    ReDim R(1 To n), T(1 To n), X(1 To n), C(1 To n), S(1 To n) As Variant
        AA = 1 / SubstIndex
        BB = 1
        CC = 1 / SubstIndex
    For i = 1 To n
            R(i) = FilmProperty(2 * i - 1)
            T(i) = FilmProperty(2 * i)
        X(i) = (2 * 3.141592654 * R(i) * T(i)) / Lambda
    'CALCULATE SINE AND COSINE TERMS FOR MATRIX ELEMENTS
        C(i) = Cos(X(i))
        S(i) = Sin(X(i))
    Next i
    'SETUP MATRIX ELEMENTS
        B11 = C(1)
        B12 = S(1) / R(1)
        B21 = S(1) * R(1)
        B22 = B11
    'CHECK FOR ONE FILM
    If n = 1 Then GoTo 500
    For i = 1 To n
        C11 = C(i)
        C12 = S(i) / R(i)
        C21 = S(i) * R(i)
        C22 = C11
        A11 = B11 * C11 - B12 * C21 'C11
        A12 = B11 * C12 + B12 * C22 'C12
        A21 = B21 * C11 + B22 * C21 'C21
        A22 = -B21 * C12 + B22 * C22 'C22
        B11 = A11
        B12 = A12
        B21 = A21
        B22 = A22
    Next i
    500 CA = (AA * B11 - B22) ^ 2 + (BB * B12 - CC * B21) ^ 2
        CB = (AA * B11 + B22) ^ 2 + (BB * B12 + CC * B21) ^ 2
        Rpc = (CA / CB)
    End Function
    Right now I'm doing this with Excel Solver, but hitting the limitation of excel's functions. How can I emulate this behavior in VB6, crunching through my function to make the output fit the data by modifying the input values.

    this graph shows the raw data, and the curve generated by the above function, plotted through values adjusted for various losses.

    The best I could think of was to search through all possible values withing a certain range, but that seems almost impossible since I've never done this before... should I really set up nested loops that will try to pick the best value by incrementing in a certain direction, depending on the trend of the curve?

  2. #2
    Junior Member
    Join Date
    Aug 2007
    Posts
    17

    Re: fitting function to data by adjusting variables

    I assume that the solution will be something like y = A sin(Bx + C) + D

    For polynomial data sets, you can fit a line perfectly to the data if you have the same number of points as you do parameters.

    I think you have many more data points than parameters -- in this case 4 parameters, A,B,C,D and ??? data points -- so you would be using a regression to come up with values for A,B,C,D that minimizes the errors between your model and the data set.

    I've never had to minimize the errors for a sinusoidal regression, here are few links that might help.
    Curve fitting
    TI sinusoidal regression
    Matlab would probably be better to use than VB. I'm not sure of VB's capabilities with math and stats functions.

  3. #3
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: fitting function to data by adjusting variables

    Is this simply a matter of fitting data to a mathematical function? If so, pls tell us what the equation of the function is and identify the unknowns. Also, how many data points do you have.

    I scanned the posts and the situation appears to be more complicated than just fitting data to a function.

    On the other hand, if you've figured out how to solve the problem in Excel using the solver, you should be able to code something up in VB.

    Can you explain the problem in pure mathematical terms and leave out all the technology (films, deposition, etc.)?

    P.S. If you are looking for a simple explanation of Levenberg-Marquardt for least squares, here's a link that has a decent explanation:

    http://www.physicsforums.com/showthread.php?t=97391
    Last edited by VBAhack; Nov 14th, 2007 at 05:17 AM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    234

    Re: fitting function to data by adjusting variables

    I WISH I could put this in simple mathematical terms, but I'm so lame at math, and this stuff is so advanced, that I'm stuck with using what I've got. I have the Rpc function that will give me all the number if I feed it the parameters, and I'm trying to find out the parameters from the number I get.

    I found this DMFitter ActiveX http://fitting.datamaster2003.com/index.htm but so far I've been unable to properly feed it my function as the expression its looking for
    property Expression: OleVariant

    Expression should be defined before you start fitting. There are several possible situations:

    * For predefined linear models possible values of this property are listed in the LinearBasisType Enumeration;
    * You can define arbitrary linear basis if you assign to this property a string expression composed from basis members (expressions dependent only on CX parameter) divided by vbCrLf delimiter (see TestLinearGFit.htm demo for details);
    * If you assign -1 (or True) to this property, DMFitter will invoke OnGetLinearBasis event handler to calculate user-defined basis members (see TestLinearEFit.htm demo);
    * For LM fitter you can provide DM2003-compatible string expression (see details about expression syntax); in addition, the components of multidimensional independent variable are denoted by "cx1", "cx2", .. "cx25".
    * Also you can define custom LM fitting object (see TestFitObject.htm demo for more information);
    * If you assign -1 (or True) to this property, LM fitter will rely on OnGetLMFunction and OnGetLMDerivative event handlers to obtain values of function and partial derivatives. Value of -2 instructs LM fitter to invoke only OnGetLMFunction event and perform differentiation numerically (see TestLMFitEvents.htm and TestLMFitEJS.htm examples).
    but so far FitResult returns 3: "Incorrect parameters or data to be fitted" This is what I'm concentrated on now, but I'm fairly unexperienced in VB6, and these objects are a bit beyond me for now. If anyone understands how to feed Rpc to LMFit, please give me some hints.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width