Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implementing the Natural Logarithm in my code . . . 1

Status
Not open for further replies.

Phalken

Programmer
Jan 8, 2004
24
US
How can I use the Natural Logarithm in my code? I am having a difficult time finding information on how to use the Natural Logarithm in my Visual Basic code.

If anyone is familiar with using the Natural Logarithm with Visual Basic, please give me a hand.

Here is my code example:

txtTime.Text = LN((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1)

Note: the LN in my code is where I need to place the Natural Logarithm for my calculation.

Also, I am using Visual Basic v6.0 [for some reason I can not start new threads in that forum - sorry all]

Thank you very much for any help to come!
 
Take a look at Math.Log()
The inverse is Math.E()

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I understand how to calculate my problem and cancel out everything perfect while using the natural logarithm, but I am unfamiliar with substituting the LN with log because of the cancelation differences.

Code example:

txtTime.Text = LN((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1)

Note for my code example:
sngT_Coffee = 25
sngT_Ambient = 20

While using the Natural Logarithm the answer results:

txtTime.Text = LN((25 - 20) / (75)) / (-0.1)

Thus txtTime.Text = ~27.1 minutes

So is there actually not a Natural Logarithm function built into Visual Basic v6.0 ? If there is not, can you please suggest and help me with an alternative other than using the log function? Again I am sorry, but I am unfamiliar with solving many problems using log compared to LN!

Thanks for any help.
 
Sorry all!

I just needed to divide my solution with log(e)

Wohoo! Applied Learning!

Although I beleive Visual Basic should include a Natural Logarithm function!
 
Hi Phalken,
I assume you are using VB.NET as you are posting to the VB.NET forum.
In VB.NET, there is a class called the math class that supplies may mathematical functions, including the natural logarithm, math.log. The equivalent for base 10 log is math.log10.
So your code in vb.net will look like:

txtTime.Text = math.log((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1)

If you have other problems, feel free to post again.

Happy Programming.
 
Actually PC888, I am coding in VB6. I am sorry for posting in VB.Net, but at the time I was unable to post a new thread in VB5&6!

But I am still having a problem . . . [Grrrrr] ;)


Here is my code:

Private Sub cmdCalculate_Click()

Dim sngT_Coffee As Single
Dim sngT_Ambient As Single
'Dim sngTime As Single

If optT_Coffee_C = True Then
sngT_Coffee = (txtT_Coffee.Text) / (1) 'Keeps the celsius temperature inputted into textbox "Temperature of Coffee" the same in the formula calculation.

If optT_Coffee_F = True Then
sngT_Coffee = (5 / 9) * (txtT_Coffee.Text - 32) 'Converts fahrenheit temperature inputted into textbox "Temperature of Coffee" to celsius in the formula calculation.

If optT_Ambient_C = True Then
sngT_Ambient = (txtT_Ambient.Text) / (1) 'Keeps the celsius temperature inputted into textbox "Ambient Temperature" the same in the formula calculation.

If optT_Ambient_F = True Then
sngT_Ambient = (5 / 9) * (txtT_Ambient.Text - 32) 'Converts fahrenheit temperature inputted into textbox "Ambient Temperature" to celsius in the formula calculation.

'If optTime_s = True Then
'sngTime = (txtTime.Text) * (60) 'Converts the time in seconds inputted into textbox "Time" to minutes in the formula calculation.
'
'If optTime_m = True Then
'sngTime = (txtTime.Text) / (1) 'Keeps the time in minutes inputted into textbox "Time" the same in the formula calculation.
'
'If optTime_h = True Then
'sngTime = (txtTime.Text) / (60) 'Converts the time in hours inputted into textbox "Time" to minutes in the formula calculation.

txtTime.Text = (Log((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1)) / Exp(1) 'This may not convert the solution in seconds, minutes, and hours

End Sub


Private Sub cmdExit_Click()
' Unloads the form from system memory and terminates the application.
Unload frmMain
End
End Sub

txtTime.Text should = ~27.1 minutes
and I am using Exp(1) as an identity of log(e) in Visual Basic v6.0 language

'HERE IS THE ERROR I RECEIVED
'Compile Error: Block If without End If
'Private Sub cmdCalculate_Click() is highlighted in yellow
'End Sub is highlighted in dark blue

Please help me understand why I received this error. I beleive it has something to do with me introducing the log into my calculation. Everything else in my code and form are solid.
 
SORRY AGAIN! I forgot to include the End If
 
Why do you insist that VB 6 does not have a natural logarithm function? Look at the Help Files that come with VB6 or at MSDN. They are poor crafrspersons who blame their tools.


Log Function

Returns a Double specifying the natural logarithm of a number.

Syntax
Log(number)

The required number argument is a Double or any valid numeric expression greater than zero.

Remarks

The natural logarithm is the logarithm to the base e. The constant e is approximately 2.718282.




Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Thank you all for your responses.
Below is my working code [and it works - wohoo! ;)]

Private Sub cmdCalculate_Click()

Dim sngT_Coffee As Single
Dim sngT_Ambient As Single
Dim sngTime As Single

If optT_Coffee_C = True Then
sngT_Coffee = (txtT_Coffee.Text) / (1) 'Keeps the celsius temperature inputted into textbox "Temperature of Coffee" the same in the formula calculation.
End If

If optT_Coffee_F = True Then
sngT_Coffee = (5 / 9) * (txtT_Coffee.Text - 32) 'Converts fahrenheit temperature inputted into textbox "Temperature of Coffee" to celsius in the formula calculation.
End If

If optT_Ambient_C = True Then
sngT_Ambient = (txtT_Ambient.Text) / (1) 'Keeps the celsius temperature inputted into textbox "Ambient Temperature" the same in the formula calculation.
End If

If optT_Ambient_F = True Then
sngT_Ambient = (5 / 9) * (txtT_Ambient.Text - 32) 'Converts fahrenheit temperature inputted into textbox "Ambient Temperature" to celsius in the formula calculation.
End If

sngTime = Format(Log((sngT_Coffee - sngT_Ambient) / (75)) / (-0.1), "###,##0.000")

If optTime_s = True Then
txtTime.Text = sngTime * 60 'Converts the time in minutes that was calculated as sngTime to time in seconds into textbox "Time". Note, you must click the "Calculate" command button again to see this conversion.
End If

If optTime_m = True Then
txtTime.Text = sngTime / 1 'Keeps the time in minutes that was calculated as sngTime unchanged. This value is inputted into textbox "Time".
End If

If optTime_h = True Then
txtTime.Text = sngTime / 60 'Converts the time in minutes that was calculated as sngTime to time in hours into textbox "Time". Note, you must click the "Calculate" command button again to see this conversion.
End If

End Sub


Private Sub cmdExit_Click()
' Unloads the form from system memory and terminates the application.
Unload frmMain
End
End Sub
 
Congratulations that everything works.

I was looking into a mathematical problem when I found that I do not understand what your program does!

At first, I thought it was to calculate the time it takes to cool down a cup of coffee for given coffee and ambient temperatures. But then I found that for a coffee temperature of 75, 40, 20 and 0 degrees C over ambient, the times are 0, 6, 13 and infinity minutes. Without being too inquisitive, may I ask if that was the intended result, and if so, what does the calculated time represent?

There is also another little puzzle. If the coffee temperature is below ambient, your program will break, as log(X) where X is negative is undefined.

Happy programming.
 
Hello PC888, thanks for asking!

I was having the same question myself! The higher the coffee temperature, the shorter it would take the coffee to cool . . . that seemed way wrong to me also! I beleive this results because the constants in this logarithmic and exponential function calculation are built (I am assuming) for near room temperature settings). Anytime you model and data information while using logarithmic and exponential functions the results become unstable and I beleive that this is the case here!

This program I wrote is actually from a formula that I took from a pre-calculus math book. It is a formula derived from Newton's Law of Cooling. This law states that a temperature of a body at time 't' after being introduced into an environment having a constant temperature 'To' is modeled by:

A(t) = To + Ce^-kt

C is a constant
k is a constant

C = 75
k = -.1
t = time measures in minutes

Then suppose we are to answer this question:

How long will it take a hot cup of coffee to cool to a temperature of 25 C. in a room with an ambient temperature of 20 C. ?

LN((25 - 20) / (75)) / (-0.1) = 27.0805 minutes

==- I am realizing right now that I may have overlooked something VERY IMPORTANT -==

The coffee temperature is actually not 25 C. but is higher, and I am actually solving to find how long it will take an unknown temperature coffee to cool to 25 C. in a 20 C ambient room temperature! This must be the error . . .

hmmmmmmmm ;)
 
Actually I am not a student at all - I am self-taught and lack many skills when it comes to efficient programming.

However I must mention that the options in my form are grouped into seperate frames (three seperate frames, one for each textbox).

If you feel so inclined I would greatly appreciate it if you happened to have some free time and would show me how you would handle writing this code to be more efficient. I am a very quick learner and maybe I will gain some much needed insight in my coding.

Thanks for the advice either way!

If you would like me to Upload the VB6.0 project to you please ask. Possibly you will not need the form and can visualize the project. It is simple with three textboxes, three frames (one for each textbox for the option buttons), one calculate command button, and one exit command button.

I currently am unfamiliar with writing error checking procedures in my code. I have not compiled many of my programs and run them through VB6.0 Working Model Edition so that if it catches a user error my program does not crash. I know this is not the way to code lol ;) I am trying to learn! I would like to learn more so that I may compile and make my software available for anyone who may find my programs useful.

Thanks again.
 
Hi Phalken,
Thank you for writing, because I was still thinking about your problem. What you wrote made a lot of sense. The time you calculate is what it takes to cool down to a certain temperature ABOVE ambient, say 5 degrees in your example.
Supposing D=5 degrees, then your equation reduces to:

A(t)-To= D = 5 = Ce^(-kt)

which on solving gives

t=(1/-k)ln(D/C)

You will note that the constant C is the initial coffee temperature minus the ambient temperature. The reasoning is that if D=C=75, the time it requires to cool down to a difference of 75 degrees is (1/-k)ln(1)=0.

Also, if the Coffee temperature is below ambient, the time calculation will be the same, except the sign of the expression inside the LN() function must be reversed.

Note also that if the coffee temperature is already within the difference, the resulting time will be negative, meaning that the required temperature difference was a while back!

Thus I propose to rewrite your calculation as follows:

dim Diff as double = 5 ' cool down to ambient - 5 degrees
dim DiffInit as double = sngT_Coffee-sngT_Ambient
if DiffInit>0 then
sngTime = Format(Log((Diff) / DiffInit) / (-0.1), "###,##0.000")
else
sngTime = Format(-Log((Diff) / -DiffInit) / (-0.1), "###,##0.000")
end if

Let me know what you think, and Happy Programming.
 
Thankx for the post PC888! Everything you wrote seemed to make sense to me. Here is another cool work with Logarithmic and exponential functions that involves the modeling of internet hosts. I hope you enjoy.

[The modeling data may be off, but it was as accurate as it could get in 1999 ;) ]

Perhaps I may find a website that publishes how many internet hosts there were for each year so that I can see how the modeled data matches to the real thing. Again, as of with my last example, the data was provided by my pre-calculus math book.

Again PC888, enjoy . . .

=======================
Modeling Internet Hosts
=======================

1999: 43.2 million Internet Hosts
1994: 2.2 million Internet Hosts

t = 5 years
r = ?

43,200,000 = 2,200,000[e^(5r)]

LN(43,200,000/2,200,000) / 5 = r

0.595476627 = r


===============================================================================================================
Formula to Model Internet Hosts:


A = Number of Internet Hosts in t years from 1994

t = year to model (ie: When t = 1, the year I am modeling is 1995. When t = 2, the year I am modeling is 1996)

2,200,000[e^(0.595476617 * t)] = A
===============================================================================================================


===========================================================================================
Modeling Internet Hosts Based on ratio from the number of Internet Hosts from 1999 to 1994:

1994: 2,200,000.00
1995: 3,990,570.00
1996: 7,238,475.00
1997: 13,129,836.18
1998: 23,816,148.72
1999: 43,200,000.00
2000: 78,360,276.56
2001: 142,137,336.60
2002: 257,822,245.50
2003: 467,662,556.80
2004: 848,290,909.30
===========================================================================================
 
That .30 of a host bothers me for some reason...

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Dear Phalken,

You seem to have been confident in extrapolating two points with an exponential curve. I have some doubts about the suitability of the exponential function. It has to slow-down SOME TIME.

You see, if it continues to grow at the same rate, by 2009, we will have about 17,000,000,000 hosts, which makes 2.5 hosts for every person living on earth!

I believe that it would be prudent to collect more data, study the growth pattern, if there is any, before deciding on the mathematical model. You would be mathematically correct if the data support it!

Happy programming.
 
2.5 hosts per person. Maybe not that impossible. Soon any mobile phone will be a host, and if the engineers get their way, also microwave ovens and bathtubs... In Japan, you can buy a fridge with a built in webcam, so you can check from the office if you need to buy butter on the way home!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top