Results 1 to 22 of 22

Thread: Logarithm Question

  1. #1
    Join Date
    Dec 2001
    Posts
    201

    Logarithm Question

    Iíve got a math question concerning logarithms.

    Iím trying to translate a working VBA program into a VBAish program language that is used in a Programmable Logic Controller (PLC) to calculate wet bulb temperature using outside air humidity and dry bulb temperature as program inputs. My problem is that the VBA program uses the Exp() or antilog function call and the only logarithmic function available in the PLC language set is the log() or natural logarithmic function.

    The VBA Exp() code call is this:

    Es = 6.112 * Exp((17.67 * Ctemp) / (Ctemp + 243.5))

    Is there any mathematical way to write an equivalent or close approximate to the Exp() function using the very limited mathematical function calls available in the PLC language set?

    Program size and speed are definite issues.

    The entire wet bulb program (in VB Script) I'm trying to translate to the PLC can be found by viewing the source code for this site:

    http://www.srh.noaa.gov/epz/?n=wxcalc_rh

    Any input appreciated...

  2. #2
    Join Date
    Nov 2005
    Posts
    55
    Which PLC system?

  3. #3
    Join Date
    May 2005
    Posts
    6,141
    Quote Originally Posted by DALeffler View Post
    I’ve got a math question concerning logarithms.

    I’m trying to translate a working VBA program into a VBAish program language that is used in a Programmable Logic Controller (PLC) to calculate wet bulb temperature using outside air humidity and dry bulb temperature as program inputs. My problem is that the VBA program uses the Exp() or antilog function call and the only logarithmic function available in the PLC language set is the log() or natural logarithmic function.

    The VBA Exp() code call is this:

    Es = 6.112 * Exp((17.67 * Ctemp) / (Ctemp + 243.5))

    Is there any mathematical way to write an equivalent or close approximate to the Exp() function using the very limited mathematical function calls available in the PLC language set?

    Program size and speed are definite issues.

    The entire wet bulb program (in VB Script) I'm trying to translate to the PLC can be found by viewing the source code for this site:

    http://www.srh.noaa.gov/epz/?n=wxcalc_rh

    Any input appreciated...
    What are the limited mathematical functions? It doesn't have a 2^2 type operator?

    You could use the first few terms of the expansion for e^x, around the middle of the range of values that you expect to deal with.

  4. #4
    Join Date
    Dec 2001
    Posts
    201
    Trane MP581 - I think...

    Sorry, have very little experience (exp) in this programming environment (it's like stepping back 30 years in time)...

  5. #5
    Join Date
    Dec 2001
    Posts
    201
    Grapes -

    Log() is it.

    I can call:

    ABS
    CLNG
    FIX
    INT
    MAX
    MIN
    SQR

    and some time/date functions and that's it!

  6. #6
    Join Date
    May 2005
    Posts
    6,141
    I was asking about + - / * ^ ^^


  7. #7
    Join Date
    Dec 2001
    Posts
    201
    Basic math operators: +, -, /. *, nope on the ^.

  8. #8
    Join Date
    Jun 2005
    Posts
    9,560
    Just wondering, but if the question is logarithmic, does that mean we only have to give you a magnitude or order answer?

    As above, so below

  9. #9
    Join Date
    May 2005
    Posts
    6,141
    What was the old fortran operator? **?

    Exp(x) ~ 1 + x + x*x/2 + x*x*x/6 ...

    And it looks like the formula may already be normalized, I dunno

  10. #10
    Join Date
    Dec 2001
    Posts
    201
    From MS Excel VBA debug:

    ?Exp((17.67 * 50) / (50 + 243.5))
    20.29189337337
    x = (17.67 * 50) / (50 + 243.5)
    ? 1 + x + x^2/2 + x^3/6
    13.0870915878352
    ?1 + x + (x^2/2) + (x^3/6)
    13.0870915878352
    ?1 + x + ((x^2)/2) + ((x^3)/6)
    13.0870915878352

    I think I did that correctly...

    Yeah, it's normalized, but what's the denominator progression?

    x_exponent_value * 2?

    so, 1 + x + x*x/2 + x*x*x/6 + x*x*x*x/8...?

  11. #11
    Join Date
    May 2005
    Posts
    6,141
    Denominator is x_exponent_value factorial

    If 50 is a representative value, maybe this is better:

    y = x-3

    Then

    Exp(x) ~ Exp(3) * (1 + y + y*y/2 + y*y*y/6...)

  12. #12
    Join Date
    Jun 2007
    Posts
    4,535
    How about a different approach...is there an easy way to implement a look-up table on this thing?

  13. #13
    Join Date
    Jun 2004
    Posts
    525
    Quote Originally Posted by DALeffler View Post
    I’ve got a math question concerning logarithms.

    I’m trying to translate a working VBA program into a VBAish program language that is used in a Programmable Logic Controller (PLC) to calculate wet bulb temperature using outside air humidity and dry bulb temperature as program inputs. My problem is that the VBA program uses the Exp() or antilog function call and the only logarithmic function available in the PLC language set is the log() or natural logarithmic function.

    The VBA Exp() code call is this:

    Es = 6.112 * Exp((17.67 * Ctemp) / (Ctemp + 243.5))

    Is there any mathematical way to write an equivalent or close approximate to the Exp() function using the very limited mathematical function calls available in the PLC language set?

    Program size and speed are definite issues.

    The entire wet bulb program (in VB Script) I'm trying to translate to the PLC can be found by viewing the source code for this site:

    http://www.srh.noaa.gov/epz/?n=wxcalc_rh

    Any input appreciated...
    Yes, I believe I can show you how the Exp() function can be re-written using log(). Here's the first way that occurs to me. I assume PLC offers you the ability to loop, so write this loop:

    You need to satisfy the equation: log( Es / 6.112 ) = (17.67 * Ctemp) / (Ctemp + 243.5)

    Choose a range of values Es could possibly be and write a for loop where variable y starts as the minimum possible value Es could be, and goes to the maximum value Es could be, incrementing y each iteration by range/numloops. See which value comes closest to (17.67 * Ctemp) / (Ctemp + 243.5) when you divide it by 6.112 and log it. That's your answer. You can increase your efficiency of this function if cpu performance is an issue, by using such techniques as binary search. Let me know if you're interested in how to do that and I can tell you.

    Let me know if you have any questions.

  14. #14
    Join Date
    May 2007
    Location
    New Haven, Connecticut
    Posts
    7,193
    No exponentiation operator? <dons nomex suit>That's only one of many reasons why Fortran is better than C</dons>

    You could look here, and I'll dig into my copy of Knuth.
    Information about American English usage here and here. Floating point issues? Please read this before posting.

  15. #15
    Join Date
    Jun 2003
    Location
    East-central Florida.
    Posts
    3,590
    I remember one Fortran implementation using POW( ) or maybe it was PWR( ) -- i.e., "power" -- for exponentiation operations. But, yes, you use ** when it's available, e.g. C = SQRT(A**2 + B**2).

  16. #16
    Join Date
    Feb 2013
    Posts
    13
    Quote Originally Posted by DALeffler View Post
    Yeah, it's normalized, but what's the denominator progression?

  17. #17
    Join Date
    May 2007
    Location
    New Haven, Connecticut
    Posts
    7,193
    I'm on the perlmonks. BrowserUK, one of the nicest people there, gave me an answer. I've asked if it's OK with him to post a link to his code.

    You could also try here, http://www.ti.com/lit/an/spra619/spra619.pdf
    Last edited by swampyankee; 2013-Feb-19 at 11:04 PM.
    Information about American English usage here and here. Floating point issues? Please read this before posting.

  18. #18
    Join Date
    May 2007
    Location
    New Haven, Connecticut
    Posts
    7,193
    BrowserUK said it was ok, so here's the linky.
    Information about American English usage here and here. Floating point issues? Please read this before posting.

  19. #19
    Join Date
    May 2005
    Posts
    6,141
    Does that program actually attempt to compute powers of ten?

  20. #20
    Join Date
    Dec 2001
    Posts
    201
    Here's what I testing with in VBA:

    Sub ExpSub()
    Dim I As Integer
    Dim CTemp1 As Double
    Dim CTemp2 As Double
    Dim ExpTemp As Double
    Dim Fctr(12) As Long

    Fctr(2) = 2 '2!
    Fctr(3) = 6 '3!
    Fctr(4) = 24 '4!
    Fctr(5) = 120 '5!
    Fctr(6) = 720 'etc...
    Fctr(7) = 5040
    Fctr(8) = 40320
    Fctr(9) = 362880
    Fctr(10) = 3628800
    Fctr(11) = 39916800
    Fctr(12) = 439084800

    CTemp1 = 23.88908 'Degrees C input: >= -40.00032 and <= 48.88928, highest accuracy desired: >= 1.66668 and <= 23.88908

    Debug.Print "Exp(" & CTemp1 & ") = " & Exp((17.67 * CTemp1) / (CTemp1 + 243.5))

    CTemp1 = ((17.67 * CTemp1) / (CTemp1 + 243.5))
    CTemp2 = CTemp1
    ExpTemp = 1 + CTemp1

    For I = 2 To 12
    CTemp2 = CTemp2 * CTemp1
    ExpTemp = ExpTemp + (CTemp2 / Fctr(I))
    Next I

    Debug.Print "ExpSub = " & ExpTemp

    End Sub

    Debug results look like this:

    Exp(-40.00032) = 3.10160049011738E-02
    ExpSub = 3.29744988378953E-02

    Exp(48.88928) = 19.1927445458528
    ExpSub = 19.1925636133456

    Exp(1.66668) = 1.12763590327193
    ExpSub = 1.12763590327193

    Exp(23.88908) = 4.84851977903881
    ExpSub = 4.84851975611891



    So far, this is working pretty well. I need to do more testing and refining before I present it for testing in the PLC, but right now I'm a very happy camper!

    I want to thank everyone for thier input and help in understanding more about the math that's well above my comfort level and look forward to exploring the options presented in the thread. Thanks a lot!

  21. #21
    Join Date
    May 2005
    Posts
    6,141
    Your starting values are all over the place, so I would not shift the center to 3 like I suggested in post #11. Keep it at zero, looks good, good luck!

  22. #22
    Join Date
    May 2007
    Location
    New Haven, Connecticut
    Posts
    7,193
    I don't know; I didn't try it. The problem I posed was to get x**y, where |y| < 1, as x**z, where |z| > 1 can be broken into repeated multiplications for the integer part and a specialized exponentiation function for the fractional part, e.g., a=x**int(z) * x** (z-int(z)) can be broken into

    Code:
    real base, exponent, fracexp, base_intexp, base_fracexp, answer
    
    integer i, intexp
    
    read base, exponent
    
    intexp = int(exponent)
    
    fracexp = exp - intexp
    
    if(intexp > 1) {     # I don't care if it's zero, as x**0 = 1 for x != 0
        
         base_intexp = intpower(base, intexp)
    
    } 
    elsif(intexp < 0) {
    
         base_intexp = 1.0/intpower(base, abs(intexp))
    }
    
    
    answer = base_exp * fracpower(base, fracexp)
    ...where you can pick fracpower from anywhere.
    Last edited by swampyankee; 2013-Feb-20 at 05:45 PM.
    Information about American English usage here and here. Floating point issues? Please read this before posting.

Similar Threads

  1. Replies: 8
    Last Post: 2011-Sep-24, 09:11 PM
  2. Follow on Question for Max B's Another Gravity Question
    By BigDon in forum Space/Astronomy Questions and Answers
    Replies: 17
    Last Post: 2008-Aug-14, 01:32 AM
  3. math question about logarithm's
    By desertmonk in forum Science and Technology
    Replies: 3
    Last Post: 2007-May-06, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
here
The forum is sponsored in-part by: