Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default Interpolator function refuses to work...driving me crazy

Hi all

I have written a function to interpolate either linearly or
exponentially but when using it keeps throwing up a #VALUE! error and
the debugger is no help. Ultimately I want to be able to extend this
to include cubic spline and constrained cubic spline interpolation
(aside: if anyone has any code to help there then I would appreciate
it).

I was hoping that someone might be able to see what stupid error I have
made

Thanks a lot for your help in advance

Lloyd

The code is he

Option Explicit
Option Base 1


' Interpolator
Function ZCinterp2(x As Date, vX() As Date, vY() As Double, lInterpType
As Long) As Double
Dim i As Long
Dim vLogY() As Double

If lInterpType = 0 Then
' Linear interpolation
If (x < vX(LBound(vX))) Then
' x less than lowest?
ZCinterp2 = vY(LBound(vY)) + (x - vX(LBound(vX))) *
(vY(LBound(vY) + 1) - vY(LBound(vY))) / (vX(LBound(vX) + 1) -
vX(LBound(vX)))
ElseIf (x vX(UBound(vX))) Then
' x is more than the highest!
ZCinterp2 = vY(UBound(vY)) + (x - vX(UBound(vX))) *
(vY(UBound(vY) - 1) - vY(UBound(vY))) / (vX(UBound(vX) - 1) -
vX(UBound(vX)))
Else
' x is between two.
For i = LBound(vX) To UBound(vX)
If vX(i) x Then Exit For
If vX(i) = x Then
ZCinterp2 = vY(i)
Exit Function
End If
Next
ZCinterp2 = vY(i - 1) + (x - vX(i - 1)) * (vY(i) - vY(i -
1)) / (vX(i) - vX(i - 1))
End If
ElseIf lInterpType = 1 Then
' Exponential interpolation
ReDim vLogY(LBound(vY) To UBound(vY))
For i = LBound(vY) To UBound(vY)
If vY(i) = 0 Then vLogY(i) = 100000000000# Else vLogY(i) =
Log(vY(i))
ZCinterp2 = Exp(ZCinterp2(x, vX, vLogY, 0))
Next
Else
' Unknown interpolation code
ErrorMessage "Unknown interpolation type."
End If
End Function

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Interpolator function refuses to work...driving me crazy

Try your function like this using the same arguments.

Function ZCinterp2(x As Date, vX() As Date, vY() As Double, lInterpType
As Long) As Double

ZCinterp2 = 100.25
End Function

does it work that way.

If not, then you have a disconnect with the way your arguments are declared.

I tried it that way using
=ZCinterp2(E1,A1:A21,B1:B20,0)

E1 was a date
A1:A21 contained dates
B1:B20 contained numbers

It didn't work for me. (#Value)

Try changing your 2nd and 3rd arguments to Ranges and make the adjustment in
the internal workings in your macro.

--
Regards,
Tom Ogilvy




"pinkfloydfan" wrote:

Hi all

I have written a function to interpolate either linearly or
exponentially but when using it keeps throwing up a #VALUE! error and
the debugger is no help. Ultimately I want to be able to extend this
to include cubic spline and constrained cubic spline interpolation
(aside: if anyone has any code to help there then I would appreciate
it).

I was hoping that someone might be able to see what stupid error I have
made

Thanks a lot for your help in advance

Lloyd

The code is he

Option Explicit
Option Base 1


' Interpolator
Function ZCinterp2(x As Date, vX() As Date, vY() As Double, lInterpType
As Long) As Double
Dim i As Long
Dim vLogY() As Double

If lInterpType = 0 Then
' Linear interpolation
If (x < vX(LBound(vX))) Then
' x less than lowest?
ZCinterp2 = vY(LBound(vY)) + (x - vX(LBound(vX))) *
(vY(LBound(vY) + 1) - vY(LBound(vY))) / (vX(LBound(vX) + 1) -
vX(LBound(vX)))
ElseIf (x vX(UBound(vX))) Then
' x is more than the highest!
ZCinterp2 = vY(UBound(vY)) + (x - vX(UBound(vX))) *
(vY(UBound(vY) - 1) - vY(UBound(vY))) / (vX(UBound(vX) - 1) -
vX(UBound(vX)))
Else
' x is between two.
For i = LBound(vX) To UBound(vX)
If vX(i) x Then Exit For
If vX(i) = x Then
ZCinterp2 = vY(i)
Exit Function
End If
Next
ZCinterp2 = vY(i - 1) + (x - vX(i - 1)) * (vY(i) - vY(i -
1)) / (vX(i) - vX(i - 1))
End If
ElseIf lInterpType = 1 Then
' Exponential interpolation
ReDim vLogY(LBound(vY) To UBound(vY))
For i = LBound(vY) To UBound(vY)
If vY(i) = 0 Then vLogY(i) = 100000000000# Else vLogY(i) =
Log(vY(i))
ZCinterp2 = Exp(ZCinterp2(x, vX, vLogY, 0))
Next
Else
' Unknown interpolation code
ErrorMessage "Unknown interpolation type."
End If
End Function


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Interpolator function refuses to work...driving me crazy

Just to expand, this worked

Function ZCinterp2(x As Date, vX As Range, vY As Range, lInterpType As Long)
As Double

ZCinterp2 = 100.25
End Function


Now you could adapt your fuction to work with those arguments.

If you want to continue to deal with arrays, then it would be

Function ZCinterp2(x As Date, vX As Variant, vY As Variant, lInterpType As
Long) As Double

ZCinterp2 = 100.25
End Function

However, your arrays would be 2 dimensional. If the 2nd argument was
A1:A10, the then array would by vX(1 to 10, 1 to 1) as an example. The
lower bound in each dimension would be 1 regardless of the option base
setting.

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote:

Try your function like this using the same arguments.

Function ZCinterp2(x As Date, vX() As Date, vY() As Double, lInterpType
As Long) As Double

ZCinterp2 = 100.25
End Function

does it work that way.

If not, then you have a disconnect with the way your arguments are declared.

I tried it that way using
=ZCinterp2(E1,A1:A21,B1:B20,0)

E1 was a date
A1:A21 contained dates
B1:B20 contained numbers

It didn't work for me. (#Value)

Try changing your 2nd and 3rd arguments to Ranges and make the adjustment in
the internal workings in your macro.

--
Regards,
Tom Ogilvy




"pinkfloydfan" wrote:

Hi all

I have written a function to interpolate either linearly or
exponentially but when using it keeps throwing up a #VALUE! error and
the debugger is no help. Ultimately I want to be able to extend this
to include cubic spline and constrained cubic spline interpolation
(aside: if anyone has any code to help there then I would appreciate
it).

I was hoping that someone might be able to see what stupid error I have
made

Thanks a lot for your help in advance

Lloyd

The code is he

Option Explicit
Option Base 1


' Interpolator
Function ZCinterp2(x As Date, vX() As Date, vY() As Double, lInterpType
As Long) As Double
Dim i As Long
Dim vLogY() As Double

If lInterpType = 0 Then
' Linear interpolation
If (x < vX(LBound(vX))) Then
' x less than lowest?
ZCinterp2 = vY(LBound(vY)) + (x - vX(LBound(vX))) *
(vY(LBound(vY) + 1) - vY(LBound(vY))) / (vX(LBound(vX) + 1) -
vX(LBound(vX)))
ElseIf (x vX(UBound(vX))) Then
' x is more than the highest!
ZCinterp2 = vY(UBound(vY)) + (x - vX(UBound(vX))) *
(vY(UBound(vY) - 1) - vY(UBound(vY))) / (vX(UBound(vX) - 1) -
vX(UBound(vX)))
Else
' x is between two.
For i = LBound(vX) To UBound(vX)
If vX(i) x Then Exit For
If vX(i) = x Then
ZCinterp2 = vY(i)
Exit Function
End If
Next
ZCinterp2 = vY(i - 1) + (x - vX(i - 1)) * (vY(i) - vY(i -
1)) / (vX(i) - vX(i - 1))
End If
ElseIf lInterpType = 1 Then
' Exponential interpolation
ReDim vLogY(LBound(vY) To UBound(vY))
For i = LBound(vY) To UBound(vY)
If vY(i) = 0 Then vLogY(i) = 100000000000# Else vLogY(i) =
Log(vY(i))
ZCinterp2 = Exp(ZCinterp2(x, vX, vLogY, 0))
Next
Else
' Unknown interpolation code
ErrorMessage "Unknown interpolation type."
End If
End Function


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Driving me CRAZY~ please help Tara New Users to Excel 0 July 7th 08 07:29 PM
Driving me crazy! RobEdgeler[_7_] Excel Programming 0 October 3rd 05 10:19 PM
Vlookup in VBA - Driving me crazy James Excel Programming 1 June 18th 05 12:55 AM
It doesn't add up - It's driving me crazy Francis Hayes (The Excel Addict) Excel Programming 10 February 28th 05 10:40 PM
Driving me crazy! Dick Kusleika[_3_] Excel Programming 0 October 21st 03 10:18 PM


All times are GMT +1. The time now is 11:53 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"