|
-
Sep 5th, 2003, 08:46 AM
#1
Thread Starter
Member
'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.
-
Sep 6th, 2003, 02:22 PM
#2
I wonder how many charact
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:
Private Function GetSlope(ByVal x1 as double, ByVal x2 as Double, _
ByVal y1 as double, ByVal y2 as Double) As Double
GetSlope = y2-y1 / x2-x1
Return GetSlope
End Function
-
Sep 6th, 2003, 10:03 PM
#3
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......
-
Sep 26th, 2003, 03:17 PM
#4
New Member
.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
-
Sep 26th, 2003, 11:12 PM
#5
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?
-
Sep 29th, 2003, 07:58 AM
#6
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|