| 1 |
%% broydensolve-doc.tex |
| 2 |
%% Copyright 2025 Matthias Floré |
| 3 |
% |
| 4 |
% This work may be distributed and/or modified under the |
| 5 |
% conditions of the LaTeX Project Public License, either version 1.3c |
| 6 |
% of this license or (at your option) any later version. |
| 7 |
% The latest version of this license is in |
| 8 |
% http://www.latex-project.org/lppl.txt |
| 9 |
% and version 1.3c or later is part of all distributions of LaTeX |
| 10 |
% version 2005/12/01 or later. |
| 11 |
% |
| 12 |
% This work has the LPPL maintenance status `maintained'. |
| 13 |
% |
| 14 |
% The Current Maintainer of this work is Matthias Floré. |
| 15 |
% |
| 16 |
% This work consists of the files broydensolve-doc.pdf, broydensolve.sty, |
| 17 |
% broydensolve-doc.tex and README.md. |
| 18 |
\documentclass[a4paper,english,dvipsnames]{l3doc} |
| 19 |
\usepackage[english]{babel} |
| 20 |
\usepackage[a4paper]{geometry} |
| 21 |
\usepackage{parskip} |
| 22 |
\usepackage{pdflscape} |
| 23 |
\usepackage{pgfplots} |
| 24 |
\pgfplotsset{compat=1.18} |
| 25 |
\usetikzlibrary{calc} |
| 26 |
\usepackage{broydensolve} |
| 27 |
\usepackage{codehigh} |
| 28 |
\usepackage[nottoc]{tocbibind} |
| 29 |
\AtEndPreamble{\hypersetup{% |
| 30 |
linktoc=all, |
| 31 |
pdfstartview=FitH, |
| 32 |
colorlinks=true, |
| 33 |
linkcolor=Mahogany, |
| 34 |
citecolor=ForestGreen, |
| 35 |
urlcolor=MidnightBlue, |
| 36 |
bookmarksnumbered=true, |
| 37 |
pdftitle=The broydensolve package, |
| 38 |
pdfauthor=Matthias Floré, |
| 39 |
pdfsubject=Manual, |
| 40 |
pdfkeywords={Broyden,solve,system of equations}}} |
| 41 |
\setcounter{tocdepth}{2} |
| 42 |
\setcounter{secnumdepth}{2} |
| 43 |
\newcommand{\tikzname}{Ti\emph{k}Z} |
| 44 |
\IndexPrologue |
| 45 |
{ |
| 46 |
\section*{Index} |
| 47 |
\markboth{Index}{Index} |
| 48 |
\addcontentsline{toc}{section}{Index} |
| 49 |
} |
| 50 |
%\let\part\section |
| 51 |
%\makeatletter |
| 52 |
%\addtocontents{toc}{\let\protect\l@part\protect\l@section} |
| 53 |
%\makeatother |
| 54 |
\title{The \texttt{broydensolve} package\\[12pt]\large Solve a system of equations with Broyden's good method} |
| 55 |
\author{Matthias Floré\\\href{mailto:[email protected]}{\texttt{[email protected]}}} |
| 56 |
\date{Version 2.0 (2025/07/24)}%\\[12pt] |
| 57 |
\begin{document} |
| 58 |
\maketitle |
| 59 |
\begin{abstract} |
| 60 |
\noindent This package implements Broyden's good method to solve a system of equations. It is also possible to use coordinates defined by \tikzname{} as known and unknown variables.% This is the manual for version . |
| 61 |
\end{abstract} |
| 62 |
\tableofcontents |
| 63 |
\section{Usage} |
| 64 |
The package |broydensolve| can be used by putting the following in the preamble. |
| 65 |
\begin{codehigh} |
| 66 |
\usepackage{broydensolve} |
| 67 |
\end{codehigh} |
| 68 |
\section{Motivation} |
| 69 |
To solve a system of (nonlinear) equations in a \LaTeX{} document, for example |Python| with the package |pythontex| or |Sage| with the package |sagetex| can be used. This could require multiple compilations or the option |shell-escape|. The package |broydensolve| is implemented with \LaTeX3, requires 1 compilation and does not require the option |shell-escape|. Moreover, it is possible to use coordinates defined by \tikzname{} as known and unknown variables. |
| 70 |
\section{Commands} |
| 71 |
\begin{function}[EXP]{\ang} |
| 72 |
\begin{syntax} |
| 73 |
\cs{ang}|(|\meta{coordinate}|)| |
| 74 |
\cs{ang}|(|\meta{coordinate_1},\meta{coordinate_2}|)| |
| 75 |
\cs{ang}|(|\meta{coordinate_1},\meta{coordinate_2},\meta{coordinate_3}|)| |
| 76 |
\cs{ang}|(|\meta{coordinate_1},\meta{coordinate_2},\meta{coordinate_3},\meta{coordinate_4}|)| |
| 77 |
\end{syntax} |
| 78 |
This command is only defined while the command |\BroydenSolve| is executed. Hence it does not conflict with the eponymous command of the package |siunitx|. It can be used in the function(s) given to the key |func| and gives an angle in radians. For 1 coordinate, its expansion comes down to |atan(|\meta{y_1}|,|\meta{x_1}|)|, for 2 coordinates to |atan(|\meta{y_2}-\meta{y_1}|,|\meta{x_2}-\meta{x_1}|)|, for 3 coordinates to |atan(|\meta{y_3}-\meta{y_2}|,|\meta{x_3}-\meta{x_2}|)-atan(|\meta{y_1}-\meta{y_2}|,|\meta{x_1}-\meta{x_2}|)|, for 4 coordinates to |atan(|\meta{y_4}-\meta{y_3}|,|\meta{x_4}-\meta{x_3}|)-atan(|\meta{y_2}-\meta{y_1}|,|\meta{x_2}-\meta{x_1}|)| and if this is smaller than $0$ then |2*pi| is added. |
| 79 |
\end{function} |
| 80 |
\hspace{1cm}\begin{minipage}{\textwidth-1cm} |
| 81 |
\begin{function}[EXP]{\BroydenIterations} |
| 82 |
\begin{syntax} |
| 83 |
\cs{BroydenIterations} |
| 84 |
\end{syntax} |
| 85 |
This command gives the total number of iterations after the command |\BroydenSolve| is executed. This command is expandable. |
| 86 |
\end{function} |
| 87 |
\end{minipage} |
| 88 |
\begin{function}[EXP]{\BroydenRoot} |
| 89 |
\begin{syntax} |
| 90 |
\cs{BroydenRoot}\oarg{iteration}\marg{variable} |
| 91 |
\end{syntax} |
| 92 |
This command gives the approximation for the root for \meta{variable} after the command |\BroydenSolve| is executed. The default value for \meta{iteration} is the total number of iterations. This command is expandable. |
| 93 |
\end{function} |
| 94 |
\begin{function}[EXP]{\BroydenRoots} |
| 95 |
\begin{syntax} |
| 96 |
\cs{BroydenRoots} |
| 97 |
\end{syntax} |
| 98 |
This command gives the approximation(s) for the root(s) for the variable(s) as a comma separated list after the command |\BroydenSolve| is executed. This command is expandable. |
| 99 |
\end{function} |
| 100 |
\begin{function}{\BroydenSetup} |
| 101 |
\begin{syntax} |
| 102 |
\cs{BroydenSetup}\marg{keys} |
| 103 |
\end{syntax} |
| 104 |
This command sets the \meta{keys} described in Section \ref{Keys}. |
| 105 |
\end{function} |
| 106 |
\begin{function}{\BroydenSolve} |
| 107 |
\begin{syntax} |
| 108 |
\cs{BroydenSolve}\marg{keys} |
| 109 |
\end{syntax} |
| 110 |
This command sets the \meta{keys} described in Section \ref{Keys} inside a group and tries to approximate the root(s) for the function(s) defined by the key |func| with respect to the variable(s) defined by the key |var| with initial value(s) defined by the key |init| using Broyden's good method, see \cite{Bacomfsnse}. In general, this method does \emph{not} always converge and does \emph{not} give an exact result. Angles and trigonometric functions are typically best in radians for this purpose. The key |stop-crit| influences when the iteration stops. |
| 111 |
\end{function} |
| 112 |
\begin{function}[EXP]{\col} |
| 113 |
\begin{syntax} |
| 114 |
\cs{col}|(|\meta{coordinate_1},\meta{coordinate_2},\meta{coordinate_3}|)| |
| 115 |
\end{syntax} |
| 116 |
This command is only defined while the command |\BroydenSolve| is executed. It can be used in the function(s) given to the key |func| and can be used to require that 3 points are collinear. Its expansion comes down to $\meta{x_1}(\meta{y_2}-\meta{y_3})+\meta{x_2}(\meta{y_3}-\meta{y_1})+\meta{x_3}(\meta{y_1}-\meta{y_2})$. |
| 117 |
\end{function} |
| 118 |
\begin{function}[EXP]{\dis} |
| 119 |
\begin{syntax} |
| 120 |
\cs{dis}|(|\meta{coordinate}|)| |
| 121 |
\cs{dis}|(|\meta{coordinate_1},\meta{coordinate_2}|)| |
| 122 |
\end{syntax} |
| 123 |
This command is only defined while the command |\BroydenSolve| is executed. It can be used in the function(s) given to the key |func| and gives a distance. For 1 coordinate, its expansion comes down to $\sqrt{\meta{x_{1}}^{2}+\meta{y_{1}}^{2}}$ and for 2 coordinates to $\sqrt{(\meta{x_{2}}-\meta{x_{1}})^{2}+(\meta{y_{2}}-\meta{y_{1}})^{2}}$. |
| 124 |
\end{function} |
| 125 |
\section{Keys}\label{Keys} |
| 126 |
\begin{function}{abs-approx-error} |
| 127 |
\begin{syntax} |
| 128 |
abs-approx-error=\meta{value} |
| 129 |
\end{syntax} |
| 130 |
The \meta{value} is used by the stopping criterion. Initially, it is |10^-3|. |
| 131 |
\end{function} |
| 132 |
\begin{function}{coordinates} |
| 133 |
\begin{syntax} |
| 134 |
coordinates=\meta{boolean} |
| 135 |
\end{syntax} |
| 136 |
If true then the function variables are given by \meta{variable}|x| and \meta{variable}|y| for each \meta{variable} in the \meta{list} given to the key |var| until the number of variables is the same as the number of functions. The functions can contain names of coordinates defined by \tikzname. For a coordinate \meta{name}, also \meta{name}|x| and \meta{name}|y| can be used in the functions to represent its $x$ and $y$ coordinate. This requires the package |tikz| and the \tikzname{} library |calc|. After the iteration ends, \tikzname{} coordinates are defined using the computed approximate solutions. |
| 137 |
\end{function} |
| 138 |
\begin{function}{func} |
| 139 |
\begin{syntax} |
| 140 |
func=\meta{list} |
| 141 |
\end{syntax} |
| 142 |
The comma separated \meta{list} defines the function(s). If a function contains a |,| then this function should be placed inside braces. Also a pair of braces can be required around the whole function(s). |
| 143 |
\end{function} |
| 144 |
\begin{function}{func-error} |
| 145 |
\begin{syntax} |
| 146 |
func-error=\meta{value} |
| 147 |
\end{syntax} |
| 148 |
The \meta{value} is used by the stopping criterion. Initially, it is |10^-3|. |
| 149 |
\end{function} |
| 150 |
\begin{function}{init} |
| 151 |
\begin{syntax} |
| 152 |
init=\meta{list} |
| 153 |
\end{syntax} |
| 154 |
The \meta{list} defines the initial value(s) for the iteration. |
| 155 |
\end{function} |
| 156 |
\begin{function}{iterations} |
| 157 |
\begin{syntax} |
| 158 |
iterations=\meta{number} |
| 159 |
\end{syntax} |
| 160 |
The \meta{number} is used by the stopping criterion. Initially, it is |5|. |
| 161 |
\end{function} |
| 162 |
\begin{function}{rel-approx-error} |
| 163 |
\begin{syntax} |
| 164 |
rel-approx-error=\meta{value} |
| 165 |
\end{syntax} |
| 166 |
The \meta{value} is used by the stopping criterion. Initially, it is |10^-3|. |
| 167 |
\end{function} |
| 168 |
\begin{function}{stop-crit} |
| 169 |
\begin{syntax} |
| 170 |
stop-crit=abs-approx-error |
| 171 |
stop-crit=func-error |
| 172 |
stop-crit=iterations |
| 173 |
stop-crit=rel-approx-error |
| 174 |
\end{syntax} |
| 175 |
The stopping criterion influences when the iteration stops. If the function(s) evaluate(s) to $0$ then the iteration stops immediately. Otherwise, the stopping behaviors are listed below. |
| 176 |
\begin{description} |
| 177 |
\item[|abs-approx-error|] The iteration stops if the $1$-norm of the difference between the current and the previous approximation is smaller than the value given to the key |abs-approx-error|. |
| 178 |
\item[|func-error|] The iteration stops if the $1$-norm of the function value(s) is smaller than the value given to the key |func-error|. |
| 179 |
\item[|iterations|] The iteration stops after a number of iterations determined by the key |iterations|. |
| 180 |
\item[|rel-approx-error|] The iteration stops if the $1$-norm of the difference between the current and the previous approximation is smaller than the product of the value given to the key |rel-approx-error| and the $1$-norm of the current approximation. |
| 181 |
\end{description} |
| 182 |
Initially, the stopping criterion is |rel-approx-error|. |
| 183 |
\end{function} |
| 184 |
\begin{function}{var} |
| 185 |
\begin{syntax} |
| 186 |
var=\meta{list} |
| 187 |
\end{syntax} |
| 188 |
The \meta{list} defines the function variable(s). Only the first $N$ elements of the \meta{list} are used where $N$ is the number of functions. The variable(s) must consist entirely of Latin letters in the range |[a-zA-Z]| and cannot be an existing floating point identifier. Initially, it is |x,y,z|. |
| 189 |
\end{function} |
| 190 |
\section{Examples} |
| 191 |
\begin{demohigh} |
| 192 |
\BroydenSolve{ |
| 193 |
func=x^5-x^2-1, |
| 194 |
init=1 |
| 195 |
} |
| 196 |
Found an approximate root after \BroydenIterations{} iterations:\\ |
| 197 |
$x\approx\BroydenRoot{x}$ |
| 198 |
\end{demohigh} |
| 199 |
\begin{demohigh} |
| 200 |
%\usepackage{pgfplots} |
| 201 |
%\pgfplotsset{compat=1.18} |
| 202 |
\begin{tikzpicture} |
| 203 |
\BroydenSolve{ |
| 204 |
func=x-cos(x), |
| 205 |
init=0.5, |
| 206 |
rel-approx-error=10^-1 |
| 207 |
} |
| 208 |
\begin{axis}[ |
| 209 |
axis equal image, |
| 210 |
axis lines=middle, |
| 211 |
xmin=0.45, |
| 212 |
xmax=0.95, |
| 213 |
ymin=0.45, |
| 214 |
ymax=0.95, |
| 215 |
xlabel=$x$, |
| 216 |
ylabel=$y$ |
| 217 |
] |
| 218 |
\addplot[smooth,domain=0.45:0.95,trig format plots=rad] {cos(x)}; |
| 219 |
\addplot[smooth] {x}; |
| 220 |
\pgfplotsinvokeforeach{0,...,\BroydenIterations}{ |
| 221 |
\draw (\BroydenRoot[#1]{x},\BroydenRoot[#1]{x}) circle[radius=2pt] |
| 222 |
node[xshift=\fpeval{#1=3?0:6},yshift=\fpeval{#1=3?10:-6}] {$P_{#1}$}; |
| 223 |
} |
| 224 |
\node[align=left] at (0.76,0.52) {Found an approximate root\\ |
| 225 |
after \BroydenIterations{} iterations:\\$x\approx\BroydenRoot{x}$}; |
| 226 |
\end{axis} |
| 227 |
\end{tikzpicture} |
| 228 |
\end{demohigh} |
| 229 |
Below, we construct the points $A$ and $B$ so that $A$ lies on the circle with center $E$, $B$ lies on the circle with center $F$, $\|AB\|=5$ and $AB\perp CD$. |
| 230 |
\begin{demohigh} |
| 231 |
%\usepackage{tikz} |
| 232 |
%\usetikzlibrary{calc} |
| 233 |
\begin{tikzpicture} |
| 234 |
\coordinate (C) at (-2,5); |
| 235 |
\coordinate (D) at (6,-2); |
| 236 |
\coordinate (E) at (0,0); |
| 237 |
\coordinate (F) at (4,3); |
| 238 |
\BroydenSolve{ |
| 239 |
coordinates, |
| 240 |
func={ |
| 241 |
\dis(A)-1.5, |
| 242 |
%{\dis(A,E)-1.5}, |
| 243 |
%Ax^2+Ay^2-1.5^2, |
| 244 |
{\dis(B,F)-2}, |
| 245 |
%(Bx-Fx)^2+(By-Fy)^2-2^2, |
| 246 |
{\dis(A,B)-5}, |
| 247 |
{\ang(A,B,D,C)-pi/2} |
| 248 |
%{atan(Cy-Dy,Cx-Dx)-atan(By-Ay,Bx-Ax)-pi/2} |
| 249 |
%(Ax-Bx)*(Cx-Dx)+(Ay-By)*(Cy-Dy) |
| 250 |
}, |
| 251 |
init={-1,-1,2,3}, |
| 252 |
var={A,B} |
| 253 |
} |
| 254 |
\draw[help lines] (-2,-2) grid (6,5); |
| 255 |
\draw (C)--(D) (E) circle[radius=1.5] (F) circle[radius=2] (A)--(B); |
| 256 |
%(\BroydenRoot{Ax},\BroydenRoot{Ay})--(\BroydenRoot{Bx},\BroydenRoot{By}) |
| 257 |
\foreach\coord in {A,B,C,D,E,F}{ |
| 258 |
\fill (\coord) circle[radius=2pt] node[left] {$\coord$}; |
| 259 |
} |
| 260 |
\end{tikzpicture} |
| 261 |
\end{demohigh} |
| 262 |
\begin{demohigh} |
| 263 |
%\usepackage{tikz} |
| 264 |
%\usetikzlibrary{calc} |
| 265 |
\begin{tikzpicture} |
| 266 |
\coordinate (A) at (0,3); |
| 267 |
\coordinate (B) at (0,0); |
| 268 |
\coordinate (C) at (3,4); |
| 269 |
\BroydenSolve{ |
| 270 |
coordinates, |
| 271 |
func={ |
| 272 |
{\col(B,D,C)}, |
| 273 |
{\ang(A,D,B)-70*deg} |
| 274 |
}, |
| 275 |
init={2,2.5}, |
| 276 |
var=D |
| 277 |
} |
| 278 |
\draw[help lines] (0,0) grid (3,4); |
| 279 |
\draw (B)--(C) (A)--(D); |
| 280 |
\foreach\n in {0,...,\inteval{\BroydenIterations-1}}{ |
| 281 |
\fill (\BroydenRoot[\n]{Dx},\BroydenRoot[\n]{Dy}) circle[radius=1pt]; |
| 282 |
} |
| 283 |
\foreach\coord in {A,B,C,D}{ |
| 284 |
\fill (\coord) circle[radius=2pt] node[below] {$\coord$}; |
| 285 |
} |
| 286 |
\end{tikzpicture} |
| 287 |
\end{demohigh} |
| 288 |
\begin{demohigh} |
| 289 |
%\usepackage{tikz} |
| 290 |
%\usetikzlibrary{calc} |
| 291 |
\begin{tikzpicture} |
| 292 |
\coordinate (A) at (0.3,0.4); |
| 293 |
\coordinate (B) at (4.7,0.8); |
| 294 |
\coordinate (C) at (0.2,3.2); |
| 295 |
\coordinate (D) at (4.8,4.7); |
| 296 |
\BroydenSolve{ |
| 297 |
coordinates, |
| 298 |
func={ |
| 299 |
{\ang(A,X,B)-100*deg}, |
| 300 |
{\ang(D,X,C)-110*deg} |
| 301 |
}, |
| 302 |
init={2,2}, |
| 303 |
var=X |
| 304 |
} |
| 305 |
\draw[help lines] (0,0) grid (5,5); |
| 306 |
\draw (A)--(X)--(B)--cycle (C)--(X)--(D)--cycle; |
| 307 |
\foreach\coord in {A,B,C,D,X}{ |
| 308 |
\fill (\coord) circle[radius=2pt] |
| 309 |
node[above] {$\coord$}; |
| 310 |
} |
| 311 |
\end{tikzpicture} |
| 312 |
\end{demohigh} |
| 313 |
\section{Version history} |
| 314 |
\begin{itemize} |
| 315 |
\item[] \textbf{Version 1.0 (2025/07/20)} First version. |
| 316 |
\item[] \textbf{Version 2.0 (2025/07/24)} Updated the documentation. |
| 317 |
\end{itemize} |
| 318 |
\begin{thebibliography}{9} |
| 319 |
\bibitem{Bacomfsnse} |
| 320 |
C. G. Broyden, |
| 321 |
\emph{A class of methods for solving nonlinear simultaneous equations}, |
| 322 |
Math. Comp. \textbf{19} (1965), 577-593. |
| 323 |
\end{thebibliography} |
| 324 |
\PrintIndex |
| 325 |
\newgeometry{left=2.25cm,right=2.25cm,top=2.25cm,bottom=2.25cm} |
| 326 |
\appendix |
| 327 |
\begin{landscape} |
| 328 |
\section{The source code} |
| 329 |
\dochighinput[language=latex/latex3]{broydensolve.sty} |
| 330 |
\end{landscape} |
| 331 |
\end{document} |