Josè Luis Mietta
Josè Luis Mietta

Reputation: 81

Solve non-linear equation in Python/SageMath

I'm a new user of Python & SageMath.

I have two non-linear equations:

  1. f(x)==h(x)
  2. g(x)+S_{i,j,k}(x) == 0

I know I can solve 1. numerically, doing:

x = var('x')
find_root(f(x)==h(x), x, x_min, x_max)

In 2., S_{i,j,k}(x) is a triple sum function of x and i, j and k are the indices of the sum. How can I solve it numerically?

Upvotes: 2

Views: 1742

Answers (1)

user
user

Reputation: 5716

Using Python and sympy, you can define your S_{i,j,k}(x) function using sympy.mpmath.nsum(), and then use sympy.mpmath.findroot():

import sympy.mpmath

x = sympy.symbols('x')


def S(x_):
    return sympy.mpmath.nsum(lambda i, j: x_*i + j, [0, 2], [3, 4])


print('Function: {}'.format(S(x)))
print('Solution: {}'.format(sympy.mpmath.findroot(S, -1)))

prints:

Function: 6.0*x + 21.0
Solution: -3.5

I chose a linear example but it works with non linear equations too.

Upvotes: 1

Related Questions