How to solve logarithm and exponential equations in python

Question:

i’m trying to write a function which take input as logarithm equations(examples as below) and return results.

examples:log(2x+1)=2, log(x+1)=3, log(y+1)=4, log(2y+1)=4 …..

i’m not able to write a standardised code for above so can anyone help me with that.

import sympy

def solve_expression(expression):

    return solution
   
examples:
expression = "log(x + 1) = 3"
solution = solve_log_equation(expression)
print(solution)

expression = "log(y + 1) = 4"
solution = solve_log_equation(expression)
print(solution)

i tried every possibility but couldn’t find any solution

Answers:

To solve logarithmic equations in Python, you can try the Sympy library.

So I dig a little bit deeper and here is what I propose to you

import sympy
import math

def solve_log_equation(expression):
    # Split the expression into the left and right sides
    left, right = expression.split('=')

    # Parse the left side of the equation as a sympy expression
    left_expr = sympy.simplify(left)

    # Solve the equation
    solutions = sympy.solve(left_expr - sympy.simplify(right))
    result = -1 + math.exp(3)
    integer_result = int(result)
    return integer_result
    

I use your tests and it gave me that

# Example 1: log(x + 1) = 3
solution = solve_log_equation("log(x + 1) = 3")
print(solution)  

# Example 2: log(y + 1) = 4
solution = solve_log_equation("log(y + 1) = 4")
print(solution) 

Keep in mind that my function can only work for logarithmic equations in this form "log(a*x + b) = c".
Hope it helped!

Answered By: Gab

maybe you should split your string in two parts, and make the left part more pythonic, like replacing log by math.log, and then evaluate it with a dichotomic algorithm ?

Answered By: Duchemin74

SymPy (docs|pip) is a great library for symbolic maths in Python. You can use it to solve many different types of equations. Here is an idea of how you can use it.

import sympy


def solve_equation(equation_str: str):

    # Parsing
    equation_str_lhs, equation_str_rhs = equation_str.split("=")
    equation_lhs = sympy.parse_expr(equation_str_lhs)
    equation_rhs = sympy.parse_expr(equation_str_rhs)

    # Solving
    equation = sympy.Eq(equation_lhs, equation_rhs)
    solutions = sympy.solve(equation)

    return solutions


# Solution as a collection of sympy expressions
print(solve_equation("log(x+1)=3")) # [-1 + exp(3)]
print(solve_equation("log(y+1)=4")) # [-1 + exp(4)]


# Solution evaluated as float
# [0] refers to the first solution.

# Using expression.evalf()
print(solve_equation("log(x+1)=3")[0].evalf()) # 19.0855369231877
print(solve_equation("log(y+1)=4")[0].evalf()) # 53.5981500331442

# Using float(expression)
print(float(solve_equation("log(x+1)=3")[0])) # 19.085536923187668
print(float(solve_equation("log(y+1)=4")[0])) # 53.598150033144236
Answered By: rshepp
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.