Results 1 to 17 of 17

Thread: help me figure out refraction equation, please! its easy!

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    234

    help me figure out refraction equation, please! its easy!

    This is my function, it calculates the normal reflectivity (%) of a non-absorbing multi-layer film on a substrate.
    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
    i will also have data being gathered from an instrument, which is monitoring the deposition of each thin film, by mearusing the transmission of light through quartz. trasferring between transmission and reflection is easy:
    Code:
    T=(1 - R) * 100
    what I'm having trouble with is the crux of my program. I need to figure out at what thickness I am, and adjust that as deposition progresses.
    I found some code online that will do what Solver does in Excel, plug in variables, get an answer, and iterate until the answer matches prediction, outputting the neccesary thickness values.
    here, the example, uses a simple equation p1+p2*cx+p3*cx^2+p4*cx^3+p5*sin(p6*cx)
    Code:
    Private Function Fitter_OnGetLMFunction(ByVal X As Variant, ByVal Parameters As Variant) As Double
      Fitter_OnGetLMFunction = Parameters(0) + Parameters(1) * X + _
        Parameters(2) * X ^ 2 + Parameters(3) * X ^ 3 + _
        Parameters(4) * Sin(Parameters(5) * X)
    End Function
    Sub Fitter_OnProgress()
    Dim I, X, Parameters
      Workbooks(1).Worksheets(1).Cells(39, 7) = Fitter.Iterations
      For I = 1 To NumPoints
        X = Workbooks(1).Worksheets(2).Cells(I, 1) ' X in 1st column
        Parameters = Fitter.Parameters
        Workbooks(1).Worksheets(2).Cells(I, 3) = Fitter_OnGetLMFunction(X, Parameters)
      Next
      DoEvents ' refresh chart
    End Sub
    
    Private Sub FitCommandButton_Click()
    Dim I, datasheet, X, Y, Parameters
      Set datasheet = Workbooks(1).Worksheets(2)
      ReDim X(1000)
      ReDim Y(1000)
      I = 1
      While datasheet.Cells(I, 1) <> ""
        X(I - 1) = datasheet.Cells(I, 1)
        Y(I - 1) = datasheet.Cells(I, 2)
        I = I + 1
      Wend
      ReDim Preserve X(I - 2)
      ReDim Preserve Y(I - 2)
      NumPoints = UBound(X) + 1 ' save to use in Fitter_OnProgress
      
      ReDim Parameters(5)
      For I = 0 To UBound(Parameters)
        If Workbooks(1).Worksheets(1).Cells(34 + I, 4) <> "" Then
          Parameters(I) = Workbooks(1).Worksheets(1).Cells(34 + I, 4)
        Else
          Parameters(I) = 1
        End If
      Next
      
      Set Fitter = CreateObject("Fitter.DMFitter")
      Fitter.X = X
      Fitter.Y = Y
      Fitter.Expression = -2    ' perform differentiation numerically
      Fitter.ParamCount = 6
      '  Fitter.Parameters = Array(1, -1, -0.1, 0, 50, 0.192)
      Fitter.Parameters = Parameters
      Fitter.Sigmas = Array(0, 0, 0, 0, 0, 0)
      Fitter.WeightType = 0
      Fitter.Options = Array(0, 0.0000001, 0)
      Fitter.Iterations = 60
      If Fitter.LMFit Then
        Workbooks(1).Worksheets(1).Cells(33, 4) = Fitter.Deviation
        Parameters = Fitter.Parameters
        For I = 0 To UBound(Parameters)
          Workbooks(1).Worksheets(1).Cells(34 + I, 4) = Parameters(I)
        Next
      Else
        MsgBox "Fitter error " & Fitter.ResultCode
      End If
    End Sub
    but i need to adapt it to use my matrix equation. I am doing this in VB6, and I have no idea how I can begin to adopt this Levenberg-Marquardt least-squares fitter (ActiveX from http://fitting.datamaster2003.com/index.htm )to use my matrix function...but if anybody could help point me in the right direction for curve-fitting, it'll be greatly appreciated.


    I found this Levenberg-Marquardt algorithm for multivariate optimization, sadly this is not something covered in school (or maybe i was sleeping?)
    I cannot make head or tail of this example http://www.alglib.net/optimization/l...gmarquardt.php
    maybe someone can point me to whatever is being optimized? I THINK i have to extrace a Jacobian determinant from the function in form of a matrix, and then plug it in...but where are the dependant variables??
    Last edited by unxzst; Sep 6th, 2007 at 10:18 AM.

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