PDA

View Full Version : Help With Log Function



thenatural10
11-04-2010, 08:12 AM
I wrote a program that will do linear regression based on linear, exponential, or power functions. I get the linear to work but b for exponential and power requires it be a ln(b). I know that log function returns natural log but every way I try to add it into the program, it gives me response that it is an invalid procedure call or argument.
Please help! My program is due in 4 hours!
(I have b = log(b) under the 3rd elseif just to show one thing I tried)
Thank you!

Option Explicit
Private Sub CommandButton1_Click()
'
Dim x(1 To 100) As Single, y(1 To 100) As Single, line As Single
Dim sumx As Single, sumx2 As Single, sumy As Single, sumy2 As Single, x1 As Single, lny As Single, lnx As Single
Dim m As Single, b As Single, r As Single, sumxy As Single, lnb As Single, xyvalues As Single
Dim j As Integer

Open ("H:\Eng 160\HW8\leastsquares.txt") For Input As 1
Input #1, xyvalues

For j = 1 To xyvalues
Input #1, x(j), y(j)
Next j

sumy = 0
sumx = 0
sumxy = 0
sumy2 = 0
sumx2 = 0

line = InputBox(" What type of line would you like to calculate? (1) Linear, (2) Exponential, (3) Power.")
If line = 1 Then ' linear
For j = 1 To xyvalues
sumx = sumx + x(j)
sumx2 = sumx2 + x(j) ^ 2
sumy = sumy + y(j)
sumy2 = sumy2 + y(j) ^ 2
sumxy = sumxy + x(j) * y(j)
Next j

Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)

MsgBox (" the equation of the line is y=" & m & "x +" & b & ". Regression = " & r)

ElseIf line = 2 Then ' exponential
For j = 1 To xyvalues
x1 = x(j)
lny = Log(y(j))
sumy = sumy + lny
sumy2 = sumy2 + lny ^ 2
sumx = sumx + x1
sumx2 = sumx2 + x1 ^ 2
sumxy = sumxy + lny + x1
Next j

Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)

MsgBox (" the equation of the line is y=" & b & "e^" & m & "x. Regression = " & r)

ElseIf line = 3 Then ' power
For j = 1 To xyvalues
lny = Log(y(j))
lnx = Log(x(j))
sumy = sumy + lny
sumy2 = sumy2 + lny ^ 2
sumx = sumx + lnx
sumx2 = sumx2 + lnx ^ 2
sumxy = sumxy + lny + lnx
Next j
Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)
b = Log(b)

MsgBox (" the equation of the line is y=" & b & "x ^" & m & ". Regression = " & r)
End If

Close
End Sub

Sub regression(ByVal sumxy As Single, ByVal sumx As Single, ByVal sumy As Single, ByVal sumx2 As Single, ByRef m As Single, ByRef b As Single, ByVal sumy2 As Single, ByRef r As Single)
Dim logb As Single
m = (11 * sumxy - sumx * sumy) / (11 * sumx2 - sumx ^ 2)
b = (sumy - m * sumx) / 11
r = (11 * sumxy - sumx * sumy) / (Sqr(11 * sumx2 - sumx ^ 2) * (Sqr(11 * sumy2 - sumy ^ 2)))
End Sub

Paul_Hossler
11-04-2010, 12:41 PM
Not doing your homework, but I don't see how Log would be 'Invalid'

Faked a little data, and this version gives the expected answers with no Invalid's. Only thing I can see might be the values for Log being = 0, so you'd have to test for them and not include the pair



Option Explicit
Sub drv()
'
Dim x(1 To 100) As Single, y(1 To 100) As Single, line As Single
Dim sumx As Single, sumx2 As Single, sumy As Single, sumy2 As Single, x1 As Single, lny As Single, lnx As Single
Dim m As Single, b As Single, r As Single, sumxy As Single, lnb As Single, xyvalues As Single
Dim j As Integer

' Open ("H:\Eng 160\HW8\leastsquares.txt") For Input As 1
' Input #1, xyvalues

' For j = 1 To xyvalues
' Input #1, x(j), y(j)
' Next j

sumy = 0
sumx = 0
sumxy = 0
sumy2 = 0
sumx2 = 0

'fake some data
xyvalues = 5

For j = 1 To 5
x(j) = j * 1.05
y(j) = j * 3.456
Next j




line = InputBox(" What type of line would you like to calculate? (1) Linear, (2) Exponential, (3) Power.")

If line = 1 Then ' linear
For j = 1 To xyvalues
sumx = sumx + x(j)
sumx2 = sumx2 + x(j) ^ 2
sumy = sumy + y(j)
sumy2 = sumy2 + y(j) ^ 2
sumxy = sumxy + x(j) * y(j)
Next j

Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)

MsgBox ("Linear: the equation of the line is y=" & m & "x +" & b & ". Regression = " & r)

ElseIf line = 2 Then ' exponential
For j = 1 To xyvalues
x1 = x(j)
lny = Log(y(j))
sumy = sumy + lny
sumy2 = sumy2 + lny ^ 2
sumx = sumx + x1
sumx2 = sumx2 + x1 ^ 2
sumxy = sumxy + lny + x1
Next j

Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)

MsgBox ("Exponetial: the equation of the line is y=" & b & "e^" & m & "x. Regression = " & r)

ElseIf line = 3 Then ' power
For j = 1 To xyvalues
lny = Log(y(j))
lnx = Log(x(j))
sumy = sumy + lny
sumy2 = sumy2 + lny ^ 2
sumx = sumx + lnx
sumx2 = sumx2 + lnx ^ 2
sumxy = sumxy + lny + lnx
Next j
Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)

MsgBox ("Power: the equation of the line is y=" & b & "x ^" & m & ". Regression = " & r)
End If

Close
End Sub

Sub regression(ByVal sumxy As Single, _
ByVal sumx As Single, ByVal sumy As Single, _
ByVal sumx2 As Single, _
ByRef m As Single, ByRef b As Single, _
ByVal sumy2 As Single, ByRef r As Single)
Dim logb As Single
m = (11 * sumxy - sumx * sumy) / (11 * sumx2 - sumx ^ 2)
b = (sumy - m * sumx) / 11
r = (11 * sumxy - sumx * sumy) / (Sqr(11 * sumx2 - sumx ^ 2) * (Sqr(11 * sumy2 - sumy ^ 2)))
End Sub


BTW, in


m = (11 * sumxy - sumx * sumy) / (11 * sumx2 - sumx ^ 2)
b = (sumy - m * sumx) / 11
r = (11 * sumxy - sumx * sumy) / (Sqr(11 * sumx2 - sumx ^ 2) * (Sqr(11 * sumy2 - sumy ^ 2)))


I'm a little (OK, a lot) fuzzy about my basic math, but why the 11's in the formulas?

Paul

thenatural10
11-04-2010, 02:30 PM
Thank you, yea I think that is what the problem is or negative numbers..

The 11s are in there because its n which is the number of values in the .txt file. I just was lazy and didnt use a variable haha.

Paul_Hossler
11-04-2010, 06:44 PM
BTW, you'd have better luck with stuff like this in the Excel and not the Word Forum

Don't forget to mark it 'Solved' under thread tools if you're OK with it

Paul