I have a user defined VB function, which i want to do in MATLAB, i know its interpolation but not able to figure out, which function will perfectly fit it.
1 view (last 30 days)
Show older comments
I have a piece of code on the excel in VB, which is named as L_invert, the same function i want to write it in MATLAB, i know its interpolation but not able to figure out, which would fit it (PFA example), i tried that example in that VB user defined function, it gives the output(PFA excel).
User Defined function in VB:
Public Function l_invert(ByVal Table As Object, ByVal X_value As Double, _
ByVal y_value As Double)
Dim i As Integer ' counter.
Dim x0, x1, y0, y1 As Integer ' Table lookup indices
Dim x0wt, x1wt, y0wt, y1wt As Double ' weighting factors
Dim r, c As Integer ' number of rows and columns
'Formulas below determine the number of rows and columns in the table
r = Table.Rows.Count - 1
For i = 2 To Table.Rows.Count
If IsEmpty(Table.Cells(i, 1)) Or Not IsNumeric(Table.Cells(i, 1)) Then
r = i - 2
Exit For
End If
Next i
c = Table.Columns.Count - 1
For i = 2 To Table.Columns.Count
If IsEmpty(Table.Cells(1, i)) Or Not IsNumeric(Table.Cells(1, i)) Then
c = i - 2
Exit For
End If
Next i
'Check for bad input
If c <= 1 Or r <= 1 Then
l_invert = [#value]
Exit Function
End If
If Table.Cells(1, 2).Value > X_value Then
x0 = 1
x1 = 1
x0wt = 1
x1wt = 0
End If
If Table.Cells(1, c + 1).Value < X_value Then
x0 = c
x1 = c
x0wt = 1
x1wt = 0
End If
'Fill out x-values if nothing is selected
If IsEmpty(x0) Then
x0 = 1
x1 = 1
x0wt = 1
x1wt = 0
For i = 1 To c - 1
If Table.Cells(1, i + 1) <= X_value And Table.Cells(1, i + 2) >= X_value Then
x0 = i
x1 = i + 1
x1wt = (X_value - Table.Cells(1, i + 1).Value) / (Table.Cells(1, i + 2).Value - Table.Cells(1, i + 1).Value)
x0wt = 1 - x1wt
Exit For
End If
Next i
End If
'Check for out of range low
If ((Table.Cells(2, x0 + 1).Value * x0wt) + (Table.Cells(2, x1 + 1).Value * x1wt)) > y_value Then
l_invert = Table.Cells(2, 1).Value
Exit Function
End If
'Check for out of range high
If ((Table.Cells(r + 1, x0 + 1).Value * x0wt) + (Table.Cells(r + 1, x1 + 1).Value * x1wt)) <= y_value Then
l_invert = Table.Cells(r + 1, 1).Value
Exit Function
End If
For i = 1 To r - 1
If ((Table.Cells(i + 1, x0 + 1).Value * x0wt) + (Table.Cells(i + 1, x1 + 1).Value * x1wt)) <= y_value And ((Table.Cells(i + 2, x0 + 1).Value * x0wt) + (Table.Cells(i + 2, x1 + 1).Value * x1wt)) >= y_value Then
y0 = i
y1 = i + 1
y1wt = (y_value - ((Table.Cells(i + 1, x0 + 1).Value * x0wt) + (Table.Cells(i + 1, x1 + 1).Value * x1wt))) / (((Table.Cells(i + 2, x0 + 1).Value * x0wt) + (Table.Cells(i + 2, x1 + 1).Value * x1wt)) - ((Table.Cells(i + 1, x0 + 1).Value * x0wt) + (Table.Cells(i + 1, x1 + 1).Value * x1wt)))
y0wt = 1 - y1wt
Exit For
End If
Next i
l_invert = Table.Cells(y0 + 1, 1) * y0wt + Table.Cells(y1 + 1, 1) * y1wt
End Function
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!