Nonlinear_Equations
Solving Systems of Non-Linear Equations¶
Copyright By PowCoder代写 加微信 powcoder
In this Notebook we learn how to solve systems of non-linear equations.
We will solve the following system of equations:
$$\begin{array}{lll}
& x^3 + y = 1 \\
& y^3 − x = −1
\end{array}.$$
You can easily check that $(x, y) = (1, 0)$ is a solution of this system. By graphing both of the equations you
can also see that $(1, 0)$ is the only solution.
import numpy as np
x1 = np.linspace(-4,4,100) # 100 linearly spaced numbers
y1 = -x1**3+1
y2 = np.linspace(-4,4,100) # 100 linearly spaced numbers
x2 = y2**3+1
import matplotlib.pyplot as plt
%matplotlib inline
# compose plot
plt.plot(x1,y1)
plt.plot(x2,y2)
plt.xlim(-4.0, 4.0)
plt.ylim(-4.0, 4.0)
plt.xlabel(“x”)
plt.ylabel(“y”)
plt.title(‘$x^3+y=1$\n$y^3-x=-1$’)
plt.show() # show the plot
We can put these equations into vector-function form by letting $x_1 = x$, $x_2 = y$ and $f_1(x_1, x_2) = x_1^3 + x_2 − 1$, $f_2(x_1, x_2) = x_2^3 − x_1 + 1.$
Define function equations that computes $f_1$ and $f_2$.
from scipy.optimize import fsolve
def equations(p):
return (x**3+y-1, y**3-x+1)
Solve equations and print solution.
x, y = fsolve(equations, (0.5, 0.5))
(1.0000000000081608, -1.652294360384222e-11)
Check solution.
equations((x, y))
(7.959410908142672e-12, -8.160805364809676e-12)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com