Non-linear simultaneous equations can be easily solved in Python.

When doing numerical calculations, there is a good chance to solve nonlinear simultaneous equations. Many years ago, when I was a graduate student doing research, I implemented it in Fortran. Recently, I have more opportunities to write programs in Python, and I have come across the situation of solving nonlinear simultaneous equations again. Newton's method is a well-known method for solving nonlinear simultaneous equations, but I searched for a good library because it is troublesome to implement from scratch.

It can be solved with "Scipy.optimize.root"

It was easy, not odd. This time, I would like to solve the following nonlinear simultaneous equations as an example.

\begin{align} x^{2}+y^{2}-1=0 \\\ x=0 \end{align}

The source code is as follows. I will give a brief explanation later.

import numpy as np
from scipy import optimize

#Return the function you want to solve in a list
def func(x):
    return [x[0]**2 + x[1]**2 -1.0,
            x[0]]


result = optimize.root( func, [ 1.0, 0.0], method="broyden1")
print(result)

The execution result is as follows.

     fun: array([-1.98898568e-07, -5.14009858e-06])
 message: 'A solution was found at the specified tolerance.'
     nit: 9
  status: 1
 success: True
       x: array([-5.14009858e-06,  9.99999901e-01])

Description of source code / execution result

See the scipy documentation (https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.root.html) for more information on how to use scipy.optimize.root. Here is a rough explanation. The first argument of optimize.root is the func function that defines the function you want to solve. The second argument is the initial value to use when starting to solve the problem. The third argument is where you specify how to solve. For details, please refer to scipy documentation, but here is one caveat. .. This time, broyden1 is specified in method, but depending on the argument, the Jacobian matrix needs to be defined separately. I was too lazy to choose one that didn't require a Jacobian procession.

Also, the important part about the execution result is the part of x: array (), which represents the actual solution, and it seems that it looks like $ x = 0, y = 1 $. See scipy.optimize.OptimizeResult for solution details.

Finally

Python is convenient because it can be easily implemented in various ways. I wanted to meet when I was a graduate student. No, I met him, but it was troublesome to change trains, so I pretended not to see it. sorry.

Recommended Posts

Non-linear simultaneous equations can be easily solved in Python.
Solve simultaneous equations in an instant using Python
Japanese can be used with Python in Docker environment
Can be used in competition pros! Python standard library
Easily format JSON in Python
Nonlinear function modeling in Python
Scripts that can be used when using bottle in Python
Solving simultaneous linear equations in Python (sweeping method and fractional representation)
New features in Python 3.9 (1)-Union operators can be used in dictionary types
Python standard input summary that can be used in competition pro
Solve ordinary differential equations in Python
AtCoder C problem summary that can be solved in high school mathematics
Easily use your own functions in Python
Implemented in Python PRML Chapter 7 Nonlinear SVM
Easily graph data in shell and Python
Tkinter could not be imported in Python
I tried to create a class that can easily serialize Json in Python
I want to create a priority queue that can be updated in Python (2.7)
You will be an engineer in 100 days --Day 35 --Python --What you can do with Python
Animation and communication state transitions can be easily described using JS and Python generators.
I made a familiar function that can be used in statistics with Python
List of tools that can be used to easily try sentiment analysis of Japanese sentences in Python (try with google colab)
++ and-cannot be used for increment / decrement in python
Let's solve simultaneous linear equations with Python sympy!
Building Sphinx that can be written in Markdown
You can easily create a GUI with Python
ABC125_C --GCD on Blackboard [Notes solved in Python]
Getting started with AWS IoT easily in Python
Matrix Calculations and Linear Equations: Linear Algebra in Python <3>
Summary of statistical data analysis methods using Python that can be used in business
Geographic information visualization of R and Python that can be expressed in Power BI
In Python3.8 and later, the inverse mod can be calculated with the built-in function pow.
A mechanism to call a Ruby method from Python that can be done in 200 lines