Python Forum
Natural Logarithm in Python
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Natural Logarithm in Python
#1
OHello all,

I'm relatively new at Python; I need to input an equation, but I need help with expressing the Natural Logarithm in Python (ln):

The equation is:

(7.35 x E) + [17.34 x ln(Len)] + [4.96 x ln(Conc)] + [0.89 x ln(DNA)] - 25.42



E, Len, Conc, and DNA are the variables.  I will post what I have later, thanks in advance!!
Reply
#2
>>> import math
>>> help(math.log)
Help on built-in function log in module math:

log(...)
    log(x[, base])

    Return the logarithm of x to the given base.
    If the base not specified, returns the natural logarithm (base e) of x.

>>>
:)
Reply
#3
Thanks!!  Big Grin
Reply
#4
This is what I have so far... I'd appreciate if someone can take a look:

[Image: cRLurQ]
Reply
#5
(May-01-2017, 12:39 AM)yg89 Wrote: This is what I have so far... I'd appreciate if someone can take a look:

[Image: cRLurQ]

Interesting concept - JPEG as IDE  Naughty . There's a novelty concept for you : copy **arrow** paste  **arrow**  Python tags
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
Sorry, here it goes:

import math
help(math.log)


E = float(input("DNA strength parameter per base:"))
Len = float(input("Length of nucleotide sequence:"))
Conc = float(input("Sodium concentration of the solution:"))
DNA = float(input("Total nucleotide strand concentration:"))

print (7.35 * E) + 17.34 + math.log(Len) + 4.96 * math.log(Conc) + 0.89 * math.log(DNA) - 25.42
Reply
#7
So what do you get when compared to a manual calculation?
If you get the same thing then what you have written could be OK (I'm not making any judgement on this - trying to show you how you can check your own work)? (And yes - there is a mistake if the formula you wrote in the OP is correct)
If not then try just one term and make sure that is correct. Then try adding another and so on.
Extra credit/learning: it is generally good practice to perform some level of error checking, especially on any value that is being input by a person. Without getting in to complex numbers, what happens if you (accidentally) enter a negative number and pass that to the 'log' function? Is that what you want to happen? If not then, once you have the code doing the correct thing with known valid numbers, what do you want to do when someone enters an invalid number?
Susan
Reply
#8
(May-03-2017, 01:48 AM)yg89 Wrote:
import math 
print (7.35 * E) + 17.34 + math.log(Len) + 4.96 * math.log(Conc) + 0.89 * math.log(DNA) - 25.42
There should be multiplication sign after 17.34

I would also suggest removing brackets around the first expression - they are redundant, and probably assign constants in the expression to variable values with meaningful names.

It is customary to define constants with all-uppercase names - in Python, you can't have it protected as in C++, but it provides a hint to the reader of you code.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#9
Made the suggested changes and it worked! Thank you all for your help!  Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Natural language processing project maaaa2401 1 1,797 Dec-04-2020, 07:26 PM
Last Post: Larz60+
  a generator that computes squares of first 20 natural numbers mdshamim06 1 8,930 Oct-01-2019, 10:38 AM
Last Post: buran
  need help in natural language processing irsyadfm 7 4,481 Aug-07-2019, 07:42 AM
Last Post: tkorol
  natural logarithm print atux_null 5 3,612 Oct-20-2017, 02:14 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020