Advertisement
overloop

interpolation3.vba

Jan 3rd, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ' xs - row, ys - row
  3. Function Interp1DRow(xs As Range, ys As Range, x As Double)
  4. Dim i As Integer
  5. Dim x1 As Double, x2 As Double, y1 As Double, y2 As Double, d As Double
  6. For i = 1 To xs.Columns.Count - 1
  7.     x1 = xs.Cells(1, i).Value
  8.     x2 = xs.Cells(1, i + 1).Value
  9.     If x >= x1 And x <= x2 Then
  10.         d = (x - x1) / (x2 - x1)
  11.         y1 = ys.Cells(1, i).Value
  12.         y2 = ys.Cells(1, i + 1).Value
  13.         Interp1DRow = y1 + (y2 - y1) * d
  14.         Exit Function
  15.     End If
  16. Next i
  17. End Function
  18.  
  19. ' xs - row, ys - column, zs - table
  20. Function Interp2D(xs As Range, ys As Range, zs As Range, x As Double, y As Double)
  21. Dim i As Integer, j As Integer
  22. Dim dx As Double, dy As Double
  23. Dim x1 As Double, x2 As Double, y1 As Double, y2 As Double
  24. Dim z1 As Double, z2 As Double, z3 As Double, z4 As Double
  25. For i = 1 To xs.Columns.Count - 1
  26.     x1 = xs.Cells(1, i).Value
  27.     x2 = xs.Cells(1, i + 1).Value
  28.     If x >= x1 And x <= x2 Then
  29.         dx = (x - x1) / (x2 - x1)
  30.         For j = 1 To ys.Rows.Count - 1
  31.             y1 = ys.Cells(j, 1).Value
  32.             y2 = ys.Cells(j + 1, 1).Value
  33.             If y >= y1 And y <= y2 Then
  34.                 dy = (y - y1) / (y2 - y1)
  35.                 z1 = zs.Cells(j, i).Value
  36.                 z2 = zs.Cells(j, i + 1).Value
  37.                 z3 = zs.Cells(j + 1, i).Value
  38.                 z4 = zs.Cells(j + 1, i + 1).Value
  39.                 Interp2D = z1 + (z2 - z1) * dx + z3 * dy - z1 * dy + (z4 - z3) * dx * dy - (z2 - z1) * dx * dy
  40.                 Exit Function
  41.             End If
  42.         Next j
  43.     End If
  44. Next i
  45. End Function
  46.  
  47. ' xs - row, ys - column, zs - table
  48. Function Interp2DSafe(xs As Range, ys As Range, zs As Range, x As Double, y As Double)
  49. Dim i As Integer, j As Integer
  50. Dim dx As Double, dy As Double
  51. Dim x1 As Double, x2 As Double, y1 As Double, y2 As Double
  52. Dim z1 As Double, z2 As Double, z3 As Double, z4 As Double
  53. Dim v1 As Variant, v2 As Variant, v3 As Variant, v4 As Variant
  54. Dim ok As Boolean, n1 As Boolean, n2 As Boolean, n3 As Boolean, n4 As Boolean
  55. Interp2DSafe = CVErr(xlErrValue)
  56. For i = 1 To xs.Columns.Count - 1
  57.     x1 = xs.Cells(1, i).Value
  58.     x2 = xs.Cells(1, i + 1).Value
  59.     If x >= x1 And x <= x2 Then
  60.         dx = (x - x1) / (x2 - x1)
  61.         For j = 1 To ys.Rows.Count - 1
  62.             y1 = ys.Cells(j, 1).Value
  63.             y2 = ys.Cells(j + 1, 1).Value
  64.             If y >= y1 And y <= y2 Then
  65.                 dy = (y - y1) / (y2 - y1)
  66.                 ' z1 z2
  67.                ' z3 z4
  68.                v1 = zs.Cells(j, i).Value
  69.                 v2 = zs.Cells(j, i + 1).Value
  70.                 v3 = zs.Cells(j + 1, i).Value
  71.                 v4 = zs.Cells(j + 1, i + 1).Value
  72.                 z1 = v1
  73.                 z2 = v2
  74.                 z3 = v3
  75.                 z4 = v4
  76.                 n1 = IsNumeric(v1) And Not IsEmpty(v1)
  77.                 n2 = IsNumeric(v2) And Not IsEmpty(v2)
  78.                 n3 = IsNumeric(v3) And Not IsEmpty(v3)
  79.                 n4 = IsNumeric(v4) And Not IsEmpty(v4)
  80.                 ok = True
  81.                 ' cell corners
  82.                If dx = 0 And dy = 0 Then
  83.                     ok = n1
  84.                 ElseIf dx = 1 And dy = 0 Then
  85.                     ok = n2
  86.                 ElseIf dx = 0 And dy = 1 Then
  87.                     ok = n3
  88.                 ElseIf dx = 1 And dy = 1 Then
  89.                     ok = n4
  90.                 End If
  91.                 ' cell borders
  92.                If dy = 0 And dx > 0 And dx < 1 Then
  93.                     ok = n1 And n2
  94.                 ElseIf dx = 0 And dy > 0 And dy < 1 Then
  95.                     ok = n1 And n3
  96.                 ElseIf dx = 1 And dy > 0 And dy < 1 Then
  97.                     ok = n2 And n4
  98.                 ElseIf dy = 1 And dx > 0 And dx < 1 Then
  99.                     ok = n3 And n4
  100.                 End If
  101.                 ' inside cell
  102.                If dx > 0 And dx < 1 And dy > 0 And dy < 1 Then
  103.                     ok = n And n2 And n3 And n4
  104.                 End If
  105.                 If ok Then
  106.                     Interp2DSafe = z1 + (z2 - z1) * dx + z3 * dy - z1 * dy + (z4 - z3) * dx * dy - (z2 - z1) * dx * dy
  107.                 End If
  108.                 Exit Function
  109.             End If
  110.         Next j
  111.     End If
  112. Next i
  113. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement