Book Image

Mastering Python Scientific Computing

Book Image

Mastering Python Scientific Computing

Overview of this book

Table of Contents (17 chapters)
Mastering Python Scientific Computing
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Equation solving


There is a magic function called solve. It can solve all types of equations. This function returns the solutions of an equation. It takes two arguments: the expression to be solved and the variable. The following program uses this function to solve various types of equations. In the following equations, it is assumed that the right-hand side of the equation is zero:

from sympy import solve

solve (6*x**2 - 3*x - 30,x)

a, b, c = symbols('a b c')
solve( a*x**2 + b*x + c, x)
substitute_solution = solve( a*x**2 + b*x + c, x)
[ substitute_solution[0].subs({'a':6,'b':-3,'c':-30}), substitute_solution[1].subs({'a':6,'b':-3,'c':-30}) ]

solve([2*x + 3*y - 3, x -2* y + 1], [x, y])
)

To solve the system of equations, we have another form of the solve method that takes the list of equations as the first argument and the list of unknowns as the second argument. This is demonstrated here:

from sympy import solve

solve ([2*x + y - 4, 5*x - 3*y],[x, y])
solve ([2*x + 2*y - 1, 2*x - 4*y...