\section{Solving non-linear
equations}\label{solving-non-linear-equations}

\subsection{Introduction}\label{introduction}

\subsection{Prerequisites}\label{prerequisites}

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{import}\NormalTok{ numpy}
\ImportTok{import}\NormalTok{ scipy}
\ImportTok{import}\NormalTok{ sympy}
\end{Highlighting}
\end{Shaded}

\subsection{fsolve from SciPy}\label{fsolve-from-scipy}

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{from}\NormalTok{ scipy.optimize }\ImportTok{import}\NormalTok{ fsolve}

\KeywordTok{def}\NormalTok{ equations(}\BuiltInTok{vars}\NormalTok{):}
\NormalTok{    x, y }\OperatorTok{=} \BuiltInTok{vars}
\NormalTok{    eq1 }\OperatorTok{=}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{+}\NormalTok{ y}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}} \DecValTok{25}
\NormalTok{    eq2 }\OperatorTok{=}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}}\NormalTok{ y}
    \ControlFlowTok{return}\NormalTok{ [eq1, eq2]}

\NormalTok{initial\_guess }\OperatorTok{=}\NormalTok{ [}\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{]}
\NormalTok{solution }\OperatorTok{=}\NormalTok{ fsolve(equations, initial\_guess)}
\BuiltInTok{print}\NormalTok{(}\StringTok{"Solution:"}\NormalTok{, solution)}
\end{Highlighting}
\end{Shaded}

\subsection{root from SciPy}\label{root-from-scipy}

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{from}\NormalTok{ scipy.optimize }\ImportTok{import}\NormalTok{ root}

\KeywordTok{def}\NormalTok{ equations(}\BuiltInTok{vars}\NormalTok{):}
\NormalTok{    x, y }\OperatorTok{=} \BuiltInTok{vars}
\NormalTok{    eq1 }\OperatorTok{=}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{+}\NormalTok{ y}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}} \DecValTok{25}
\NormalTok{    eq2 }\OperatorTok{=}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}}\NormalTok{ y}
    \ControlFlowTok{return}\NormalTok{ [eq1, eq2]}

\NormalTok{initial\_guess }\OperatorTok{=}\NormalTok{ [}\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{]}
\NormalTok{solution }\OperatorTok{=}\NormalTok{ root(equations, initial\_guess)}
\BuiltInTok{print}\NormalTok{(}\StringTok{"Solution:"}\NormalTok{, solution.x)}
\end{Highlighting}
\end{Shaded}

\subsection{minimize from SciPy}\label{minimize-from-scipy}

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{from}\NormalTok{ scipy.optimize }\ImportTok{import}\NormalTok{ minimize}

\CommentTok{\# Define the equations}
\KeywordTok{def}\NormalTok{ equation1(x, y):}
    \ControlFlowTok{return}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{+}\NormalTok{ y}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}} \DecValTok{25}

\KeywordTok{def}\NormalTok{ equation2(x, y):}
    \ControlFlowTok{return}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}}\NormalTok{ y}

\CommentTok{\# Define the objective function for optimization}
\KeywordTok{def}\NormalTok{ objective(xy):}
\NormalTok{    x, y }\OperatorTok{=}\NormalTok{ xy}
    \ControlFlowTok{return}\NormalTok{ equation1(x, y)}\OperatorTok{**}\DecValTok{2} \OperatorTok{+}\NormalTok{ equation2(x, y)}\OperatorTok{**}\DecValTok{2}

\CommentTok{\# Initial guess}
\NormalTok{initial\_guess }\OperatorTok{=}\NormalTok{ [}\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{]}

\CommentTok{\# Perform optimization}
\NormalTok{result }\OperatorTok{=}\NormalTok{ minimize(objective, initial\_guess)}
\NormalTok{solution\_optimization }\OperatorTok{=}\NormalTok{ result.x}

\BuiltInTok{print}\NormalTok{(}\StringTok{"Optimization Method Solution:"}\NormalTok{, solution\_optimization)}
\end{Highlighting}
\end{Shaded}

\subsection{nsolve from SymPy}\label{nsolve-from-sympy}

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{from}\NormalTok{ sympy }\ImportTok{import}\NormalTok{ symbols, Eq, nsolve}

\CommentTok{\# Define the variables}
\NormalTok{x, y }\OperatorTok{=}\NormalTok{ symbols(}\StringTok{\textquotesingle{}x y\textquotesingle{}}\NormalTok{)}

\CommentTok{\# Define the equations}
\NormalTok{eq1 }\OperatorTok{=}\NormalTok{ Eq(x}\OperatorTok{**}\DecValTok{2} \OperatorTok{+}\NormalTok{ y}\OperatorTok{**}\DecValTok{2}\NormalTok{, }\DecValTok{25}\NormalTok{)}
\NormalTok{eq2 }\OperatorTok{=}\NormalTok{ Eq(x }\OperatorTok{{-}}\NormalTok{ y, }\DecValTok{0}\NormalTok{)}

\CommentTok{\# Initial guess for the solution}
\NormalTok{initial\_guess }\OperatorTok{=}\NormalTok{ [}\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{]}

\CommentTok{\# Use nsolve to find the solution}
\NormalTok{solution }\OperatorTok{=}\NormalTok{ nsolve([eq1, eq2], [x, y], initial\_guess)}
\BuiltInTok{print}\NormalTok{(}\StringTok{"Solution:"}\NormalTok{, solution)}
\end{Highlighting}
\end{Shaded}

\subsection{newton\_method from NumPy}\label{newton_method-from-numpy}

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np}

\KeywordTok{def}\NormalTok{ equations(}\BuiltInTok{vars}\NormalTok{):}
\NormalTok{    x, y }\OperatorTok{=} \BuiltInTok{vars}
\NormalTok{    eq1 }\OperatorTok{=}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{+}\NormalTok{ y}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}} \DecValTok{25}
\NormalTok{    eq2 }\OperatorTok{=}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}}\NormalTok{ y}
    \ControlFlowTok{return}\NormalTok{ np.array([eq1, eq2])}

\KeywordTok{def}\NormalTok{ newton\_method(initial\_guess, tolerance}\OperatorTok{=}\FloatTok{1e{-}6}\NormalTok{, max\_iter}\OperatorTok{=}\DecValTok{100}\NormalTok{):}
    \BuiltInTok{vars} \OperatorTok{=}\NormalTok{ np.array(initial\_guess, dtype}\OperatorTok{=}\BuiltInTok{float}\NormalTok{)}
    \ControlFlowTok{for}\NormalTok{ \_ }\KeywordTok{in} \BuiltInTok{range}\NormalTok{(max\_iter):}
\NormalTok{        J }\OperatorTok{=}\NormalTok{ np.array([[}\DecValTok{2} \OperatorTok{*} \BuiltInTok{vars}\NormalTok{[}\DecValTok{0}\NormalTok{], }\DecValTok{2} \OperatorTok{*} \BuiltInTok{vars}\NormalTok{[}\DecValTok{1}\NormalTok{]], [}\DecValTok{2} \OperatorTok{*} \BuiltInTok{vars}\NormalTok{[}\DecValTok{0}\NormalTok{], }\OperatorTok{{-}}\DecValTok{1}\NormalTok{]])}
\NormalTok{        F }\OperatorTok{=}\NormalTok{ equations(}\BuiltInTok{vars}\NormalTok{)}
\NormalTok{        delta }\OperatorTok{=}\NormalTok{ np.linalg.solve(J, }\OperatorTok{{-}}\NormalTok{F)}
        \BuiltInTok{vars} \OperatorTok{+=}\NormalTok{ delta}
        \ControlFlowTok{if}\NormalTok{ np.linalg.norm(delta) }\OperatorTok{\textless{}}\NormalTok{ tolerance:}
            \ControlFlowTok{return} \BuiltInTok{vars}

\NormalTok{initial\_guess }\OperatorTok{=}\NormalTok{ [}\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{]}
\NormalTok{solution }\OperatorTok{=}\NormalTok{ newton\_method(initial\_guess)}
\BuiltInTok{print}\NormalTok{(}\StringTok{"Solution:"}\NormalTok{, solution)}
\end{Highlighting}
\end{Shaded}
