Anily
Anily

Reputation: 11

How can I use a for loop on python with alternating mathematical operations?

I need to write a code that first calculates an approximation to a logarithm using a series expansion, then display that approximation, as well as the difference between the approximation and the actual logarithm result. The numbers all have to be rounded to 6 decimal places.

The values I am receiving as input are the number which I have to find the logarithm of, and how many terms of the series expansion of the logarithm I want to use.

First, I have to define and call the main function, then initialize any variables, then prompt the user for values, then setup a for-loop to perform the series expansion, then calculate the actual logarithm answer, then calculate the difference. Finally, I have to print the approximation value and the difference. I also need to make sure to include comments for each step.

The formula for a series expansion of a logarithm for a real number x, where 0<x<2, is as follows:

log(x) = (x-1) - (1/2)(x-1)^2 + (1/3)(x-1)^3 - (1/4)(x-1)^4 + ...

The problem I'm having trouble solving is how to use the for loop to alternate the addition and subtraction. I'm also not sure if the rest of my code is written properly, but it's running. The answers I'm getting out of it though are just not the correct math.

Here is the code I have written so far:

# This program displays the approximate logarithm and the difference between that and the actual logarithm of a number inputted by the user to six decimal places. 
# The definition of the main function
def main():
    import math
    # Initialize any necessary variables and prompt the user for values
    num = eval(input("Enter the number whose logarithm you want to be computed: "))
    term = eval(input("Enter how many terms of the series expansion for logarithms you want to use: "))
    approx = 0
    
    # Setup a loop based on the values entered by the user
    # Calculate an approximation to the logarithm of the number entered by the user
    for i in range(2, term+1):
        if i % 2 == 0:
            approx += (num-1) - ((1/i)*(num-1)**i)
        else:
            approx += (num-1) - ((1/i)*(num-1)**i) + ((1/(i+1))*((num-1)**(i+1))
    rapprox = round(approx, 6)
 
    # Compute logarithm and difference
    mog = math.log(num)
    rmog = round(mog, 6)
    diffy = rapprox - rmog

    # Display the computed values
    print("The computed approximation to log", num, "is: ", rapprox)
    print("The difference between the approximation and the actual value for log", num, "is: ", diffy)

    # Calling the main function 
    if __name__ == "__main__":
        main()

We have been given two sample outputs which look like this:

    Please enter the number whose logarithm you want to be computed: 1.5
    Please enter how many terms of the series expansion for logarithm you want to use: 5
    The computed approximation to log 1.5 is: 0.407292
    The difference between the approximation and the actual value for log 1.5 is: 0.001827

and this:

    Please enter the number whose logarithm you want to be computed: 0.8
    Please enter how many terms of the series expansion for logarithm you want to use: 4
    The computed approximation to log 0.8 is: -0.223067
    The difference between the approximation and the actual value for log 0.8 is: 0.000077

If you need any more info on the question because it's not clear, I can clarify, thanks!

Upvotes: 1

Views: 407

Answers (1)

md2perpe
md2perpe

Reputation: 3071

Note that

log(x) = (x-1) - (1/2)(x-1)^2 + (1/3)(x-1)^3 - (1/4)(x-1)^4 + ...
= -(1-x) - (1/2)(1-x)^2 - (1/3)(1-x)^3 - (1/4)(1-x)^4 - ...
= -( (1-x) + (1/2)(1-x)^2 + (1/3)(1-x)^3 + (1/4)(1-x)^4 + ... )

Therefore you can implement the approximation of the logarithm as

def log_approx(x, N):
    """Computes an approximation of log(x) using N terms"""
    s = 0.0
    for n in range(1, N+1):
        s -= (1-x)**n / n
    return s

or shorter

def log_approx(x, N):
    """Computes an approximation of log(x) using N terms"""
    return -sum(((1-x)**n / n for n in range(1, N+1)))

Upvotes: 3

Related Questions