Solved

Slope Function for VB 6.0

Posted on 2006-11-30
5
1,519 Views
Last Modified: 2008-01-09
Anyone know where I can see code for the Slope funtion written in VB?

I suppose I could write one but why reinvent the wheel.

I actually don't need it for two arrays necessarily.  The Y data will just be ascending dates
so I figure i can just use a count of the data in the X array.  I would think that would be valid?
Y       X
1       5
2       9
3       2
4       6
5       5
6       7

0
Comment
Question by:mayfieldjr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 125 total points
ID: 18048242
Hi mayfieldjr,

I did that in VB.Net a couple of months ago. You should be able to convert it easily.

Here is my code:
    Public Shared Function Slope(ByVal pArrayY() As Double, ByVal pArrayX() As Double) As Double
        Dim dblEX As Double
        Dim dblEX2 As Double
        Dim dblEXY As Double
        Dim dblEY As Double
        Dim intN As Integer
        For intCtr As Integer = 0 To pArrayX.Length - 1
            If (pArrayX(intCtr) <> Double.MinValue) AndAlso (pArrayY(intCtr) <> Double.MinValue) Then
                dblEX += pArrayX(intCtr)
                dblEX2 += (pArrayX(intCtr) * pArrayX(intCtr))
                dblEXY += (pArrayX(intCtr) * pArrayY(intCtr))
                dblEY += pArrayY(intCtr)
                intN += 1
            End If
        Next

        Dim dblA As Double
        Dim dblB As Double
        Dim dblAvgX As Double
        Dim dblAvgY As Double

        dblAvgX = dblEX / intN
        dblAvgY = dblEY / intN
        dblA = dblEXY - ((dblEX * dblEY) / intN)
        dblB = dblEX2 - ((dblEX * dblEX) / intN)
        Return (dblA / dblB)
    End Function

Cheers!
0
 

Author Comment

by:mayfieldjr
ID: 18048677
I'm not sure what Double.MinValue is????

I believe I have everything else.  Is Double.MinValue mean the min value in the array?

Thanks
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 18048861
it is the lowest value a double can hold. You can replace this with a value you are sure you won't find into your array.
0
 

Author Comment

by:mayfieldjr
ID: 18049043
I have tested your function but am getting a different answer than Excel gives me.

Example:
Y     X
1     7
2     1
3     4
4     8
5     8
6     4
7     9
8     8
9     1

I get .038   Excel gets .05

I either did not translate to VB properly (probably the case) or made some other error.  

What do you get using the above data with your function.

I'll keep trying.

Thanks
0
 

Author Comment

by:mayfieldjr
ID: 18049312
I see MY error now.  Works very well.

Thank you.  You saved me allot of time.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question