ln (Natural Log) in Python
Asked Answered
D

2

40

In this assignment I have completed all the problems except this one. I have to create a python script to solve an equation (screenshot).

formula

Unfortunately, in my research all over the internet I cannot figure out how in the world to either convert ln to log or anything usable, or anything. The code I have written so far is below. I will also post the answer that our teacher says we should get.

import math
p = 100
r = 0.06 / 12
FV = 4000

n = str(ln * ((1 + (FV * r) / p) / (ln * (1 + r))))

print ("Number of periods = " + str(n))

The answer I should get is 36.55539635919235 Any advice or help you have would be greatly appreciated!

Also, we are not using numpy. I already attempted that one.

Thanks!

Downhearted answered 30/8, 2016 at 19:44 Comment(1)
Also, please don't be offended, but I would advise you to brush up on your background knowledge regarding logarithms. Your code seems to imply that you think ln is a constant that is being multiplied with an expression in parentheses. There are a lot of great resources nowadays for learning about math concepts. For example, check out this link.Lodovico
C
88

math.log is the natural logarithm:

From the documentation:

math.log(x[, base]) With one argument, return the natural logarithm of x (to base e).

Your equation is therefore:

n = math.log((1 + (FV * r) / p) / math.log(1 + r)))

Note that in your code you convert n to a str twice which is unnecessary

Cyclamen answered 30/8, 2016 at 19:47 Comment(0)
A
12

Here is the correct implementation using numpy (np.log() is the natural logarithm)

import numpy as np
p = 100
r = 0.06 / 12
FV = 4000

n = np.log(1 + FV * r/ p) / np.log(1 + r)

print ("Number of periods = " + str(n))

Output:

Number of periods = 36.55539635919235
Amortization answered 4/5, 2020 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.