A Python based code to numerically solve generic equations systems.
This code solve numerically a system of generic (linear and non-linear) equations using Newton Method
Python 3.8.3
Python Numpy library
Python SymPy library
There two main files in this project non_linear_equations.py
and non_linear_solver.py
.
From they, you want import Equation
and EquationSolver
, respectively.
Equation class is a SymPy based class structured to store a set of equations and evaluate this system at a given point.
- Constructor
Recive a list of SymPy expressions and a tuple containing the set of symbols (in String) that compose the system
- Equation.str_to_expression(string_expression_list)
Recive a list of Strings and parse to SymPy expression
Ex.
from non_linear_equations import Equation
string_expressions = ["x**2 + y**2 + z**2 - 8",
"x**2 + y**2 - z**2",
"y**2 - x + z - 3"]
expr = Equation.str_to_expression( string_expressions )
- obj.at(self, values)
Evaluate the system in a given point
Ex.
from non_linear_equations import Equation
expr = Equation.str_to_expression( ["x**2 + y**2 + z**2 - 8",
"x**2 + y**2 - z**2",
"y**2 - x + z - 3"])
expr = Equation( expr, ('x', 'y', 'z') )
eval = expr.at( (2,3,4) ) # Respect the given in the constructor
print(eval)
>>Console output
[-8. 0. -3.]
- Constructor
Recive a Equation class
- obj.solve(self, initial_values, stop_criterion, max_iterations = 30, stop_criterion_value = 0.01, generate_output_info = False [WIP])
Solve a determinated Equation System and return the solution vector in Numpy.ndarray type.
This method is the core of the program, it will tries to solve the given sytem with Newton Method.
Parameter | Meaning | Default value |
---|---|---|
initial_values | The initial values of method aproximation, should be close enough to the real solution to garantee convergence | - |
stop_criterion | Should be EquationSolver.max_relative_error or EquationSolver.max_absolute_error | - |
max_iterations | The max number of steps to tries hit the stop_criterion before the method returns | 30 |
stop_criterion_value | The values that the stop_criterion needs to get to reach the convergence and return a solution | 0.01 |
Ex.
from non_linear_equations import Equation
from non_linear_solver import EquationSolver
expr = Equation.str_to_expression( ["x**2 + y**2 + z**2 - 8",
"x**2 + y**2 - z**2",
"y**2 - x + z - 3"])
expr = Equation( expr, ('x', 'y', 'z') )
equation_solver = EquationSolver(expr)
solution = equation_solver.solve( (1.5, 2.1, 3.4),
EquationSolver.max_relative_error,
stop_criterion_value=0.1)
print(solution)
print(type( solution ))
>>Console output
[1.3028102 1.5174782 2.00008165]
<class 'numpy.ndarray'>