Results 1 to 6 of 6

Thread: 'Slope' function help needed

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    45

    'Slope' function help needed

    I am in need of a Slope function. I know the forumla for Slope, but cannot find a pre-written function. There must be one out there somewhere. Does anyone have any ideas?

    Question 2- Excel has a slope funtion. Is it possible to access it from .Net? I have tried but cannot figure it out short of exporting all the data to a spreadsheet and then calling the function. Can I implement excel functions in .net code (local to the code)?

    Thanks in advance.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Correct me if I'm off base here, but there must be some other restriction to your question, because if you have the formula, you could easily write your own slope function.

    VB Code:
    1. Private Function GetSlope(ByVal x1 as double, ByVal x2 as Double, _
    2.  ByVal y1 as double, ByVal y2 as Double) As Double
    3.  
    4. GetSlope = y2-y1 / x2-x1
    5. Return GetSlope
    6. End Function

  3. #3
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by nemaroller
    Correct me if I'm off base here, but there must be some other restriction to your question, because if you have the formula, you could easily write your own slope function.
    Yeah... I am wondering why the poster asked this question... you just subtract 2 sets of numbers and divide......

  4. #4
    New Member
    Join Date
    Sep 2003
    Posts
    7

    .NET slope funciton

    Function ComputeSlope(ByVal arrY() As Double, ByVal arrX() As Double) As Double

    ' Define equation variables
    Dim n As Integer = arrX.GetLength(0)
    Dim Slope As Double
    Dim Intercept As Double

    ' Get required summations
    Dim i As Integer
    Dim sumX As Double = 0
    Dim sumY As Double = 0
    Dim sumXY As Double = 0
    Dim sumX2 As Double = 0
    For i = 0 To (n - 1)
    sumX += arrX(i)
    sumY += arrY(i)
    sumXY += arrX(i) * arrY(i)
    sumX2 += arrX(i) ^ 2
    Next

    Slope = ((n * sumXY) - (sumX * sumY)) / ((n * sumX2) - (sumX ^ 2))
    Intercept = (sumY - (Slope * sumX)) / n

    Return Slope

    End Function

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: .NET slope funciton

    Originally posted by jsiegendorf
    Function ComputeSlope(ByVal arrY() As Double, ByVal arrX() As Double) As Double

    ' Define equation variables
    Dim n As Integer = arrX.GetLength(0)
    Dim Slope As Double
    Dim Intercept As Double

    ' Get required summations
    Dim i As Integer
    Dim sumX As Double = 0
    Dim sumY As Double = 0
    Dim sumXY As Double = 0
    Dim sumX2 As Double = 0
    For i = 0 To (n - 1)
    sumX += arrX(i)
    sumY += arrY(i)
    sumXY += arrX(i) * arrY(i)
    sumX2 += arrX(i) ^ 2
    Next

    Slope = ((n * sumXY) - (sumX * sumY)) / ((n * sumX2) - (sumX ^ 2))
    Intercept = (sumY - (Slope * sumX)) / n

    Return Slope

    End Function
    You could cut out about half that crap. Also, why bother find Intercept when it doesn't return it?

  6. #6
    New Member
    Join Date
    Sep 2003
    Posts
    7

    slope and intercept

    The slope function in Excel takes two arrays as inputs. Perhaps the original poster was interested in more than just two numbers.

    Also, I originally wrote this function to return both. Even though the question only called for slope, it is not out of the question that the user might need to get the intercept eventually.

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