diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2025-04-29 18:34:26 -0600 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2025-04-29 18:34:26 -0600 |
| commit | 8ab48120f03185a051f652b18f97439265884221 (patch) | |
| tree | 36071317cde36ccd88c4189ccd725ce71c61010f | |
| parent | 098fb4a08ca946ed2c8aa234f1a7129abde9fde0 (diff) | |
Added tex files to book/
| -rw-r--r-- | book/module1/installing_anaconda.tex | 158 | ||||
| -rw-r--r-- | book/module2/documentation.tex | 64 | ||||
| -rw-r--r-- | book/module2/error.tex | 97 | ||||
| -rw-r--r-- | book/module2/non_linear_eqn_solver.tex | 129 | ||||
| -rw-r--r-- | book/module3/numerical_methods.tex | 41 |
5 files changed, 489 insertions, 0 deletions
diff --git a/book/module1/installing_anaconda.tex b/book/module1/installing_anaconda.tex new file mode 100644 index 0000000..0ff3d6f --- /dev/null +++ b/book/module1/installing_anaconda.tex @@ -0,0 +1,158 @@ +\section{Installing Anaconda}\label{installing-anaconda} + +This tutorial will cover the steps on how to install Anaconda. + +\emph{Note for Advanced users: For those who wish to have a lightweight +installation, can install miniconda or miniForge, however for this +course we will show you how to use Anaconda Navigator. If you've never +used the programs before then using Anaconda is recommended.} + +\subsubsection{What is Anaconda?}\label{what-is-anaconda} + +Anaconda Distribution is a popular open-source Python distribution +specifically designed for scientific computing, data science, machine +learning, and artificial intelligence applications. It simplifies the +set up and use of Python for data science, machine learning, and +scientific computing. It comes with all the important tools you need, +like NumPy, Pandas, and JupyterLab, so you don't have to install +everything separately. The Conda package manager helps you install and +update software without worrying about breaking other programs. It also +lets you create separate environments, so different projects don't +interfere with each other. Additionally, Anaconda includes programs like +JupyterLab for interactive coding, and Spyer a MATLAB-like IDE. + +\subsection{Instructions}\label{instructions} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\item + Find the latest version of Navigator from the official Anaconda + Inc.~website: \href{https://www.anaconda.com/download}{Download + Anaconda} +\item + Press the \emph{Download Now} button. +\item + Press the \emph{Skip registration} button below the submit button, + otherwise submit your email address to subscribe to the Anaconda email + list. +\item + Under Anaconda Installers press \emph{Download} or find the + appropriate version for your operating system below. +\end{enumerate} + +Proceed to next section for your respective operating system. + +\subsubsection{Windows}\label{windows} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{4} +\tightlist +\item + Once the download is complete, double click the executable (.exe) file + to start the installer. Proceed with the installation instructions. +\end{enumerate} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_01_welcome.png} +\caption{Welcome screen} +\end{figure} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_02_terms.png} +\caption{Terms and conditions} +\end{figure} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{5} +\tightlist +\item + Select the \emph{Just Me} recommended option. +\end{enumerate} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_03_for.png} +\caption{Install for} +\end{figure} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{6} +\tightlist +\item + You can leave the destination folder as is, just make sure you have a + minimum of \textasciitilde5 GB available storage space. Press + \emph{Next} to proceed. +\end{enumerate} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_04_destination.png} +\caption{Installation destination} +\end{figure} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{7} +\tightlist +\item + It is recommended to register Anaconda3 as the default python version + if you already have an instance of python installed. Otherwise, you + can leave the checkboxes as defaults. +\end{enumerate} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_05_advanced.png} +\caption{Avanced Options} +\end{figure} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_06_installing.png} +\caption{Installing} +\end{figure} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_07_installing2.png} +\caption{Installing 2} +\end{figure} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_08_installing_complete.png} +\caption{Complete} +\end{figure} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_09_cloud.png} +\caption{Cloud} +\end{figure} + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_installer_10_finish.png} +\caption{Finish} +\end{figure} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{8} +\tightlist +\item + You made it! Anaconda is now installed, you are ready for launch. + Assuming that you didn't add Anaconda to PATH environment variable you + will need to start navigator from the start menu. +\end{enumerate} + +\subsubsection{Mac/Linux}\label{maclinux} + +Anaconda provides installation documentation for Mac and Linux users, +please refer to the +\href{https://docs.anaconda.com/anaconda/install/}{documentation page}. diff --git a/book/module2/documentation.tex b/book/module2/documentation.tex new file mode 100644 index 0000000..ebd9d87 --- /dev/null +++ b/book/module2/documentation.tex @@ -0,0 +1,64 @@ +\section{Documentation of your code}\label{documentation-of-your-code} + +Code documentation is essential for maintaining and scaling software +projects. Whether it's an open-source project or your own private code. +It ensures that you can understand, use, troubleshoot and build upon the +code in the future. + +\subsection{Keep Detailed and Accurate +Notes}\label{keep-detailed-and-accurate-notes} + +Just as a recipe requires clear instructions, your code should be +accompanied by comprehensive notes. Document your process thoroughly to +ensure that others (and future you) can follow along without confusion. + +When documenting a project, it's essential to include detailed notes +that capture not just what the code does, but how it was developed. This +includes recording libraries used, citing any external code snippets +along with their sources, and outlining the sequence of steps taken +throughout the coding process. Such comprehensive documentation enables +others---and your future self---to understand, recreate, and maintain +the project more effectively, reducing confusion and improving long-term +usability. + +\subsection{Explain Your Decisions}\label{explain-your-decisions} + +In programming, there are often several valid approaches to solving a +problem. When documenting your code, it's important to clarify why you +chose a particular method---especially if it deviates from common +practices. Anticipating potential questions and addressing them directly +in your documentation helps others follow your reasoning and builds +trust in your solution. \includegraphics{figures/rubberDuck.png} A +useful strategy for articulating these decisions is the ``rubber duck'' +technique---explaining your code as if you're teaching it to someone +else. Whether spoken aloud or written down, this practice helps you +clarify your logic and communicate the reasoning behind your choices, +providing valuable context for future collaborators or +reviewers.\hspace{0pt} + +\subsection{Include a README}\label{include-a-readme} + +A README file serves as the introduction to your project. It should be +placed in the top-level directory and provide essential +information.\hspace{0pt} A good readme file may include: - Project title +and description - Installation instructions - Usage examples - +Contribution guidelines (if applicable) - License information (if +applicable) + +This file acts as a roadmap for anyone interacting with your project. +\hspace{0pt} + +\subsection{In-line Comments}\label{in-line-comments} + +While external documentation is vital, in-code comments provide +immediate context. Use them to explain complex logic or important +sections within your code.\hspace{0pt}Here are some guidelines to +follow: Keep comments concise and relevant. Avoid stating the obvious; +focus on the ``why'' rather than the ``what.''. + +\subsection{Maintain and Update +Documentation}\label{maintain-and-update-documentation} + +Similarly to your code, the documentation should evolve alongside your +code. Regularly review and update it to reflect changes, ensuring +accuracy and relevance.\hspace{0pt} diff --git a/book/module2/error.tex b/book/module2/error.tex new file mode 100644 index 0000000..2646dc8 --- /dev/null +++ b/book/module2/error.tex @@ -0,0 +1,97 @@ +\section{Errors in Numerical +Computations}\label{errors-in-numerical-computations} + +In any numerical method, \textbf{error} is inevitable. Understanding +\textbf{what kinds of errors occur} and \textbf{why} is essential to +building reliable and accurate computations. + +We mainly classify errors into two major types: - Truncation Error - +Round-off Error + +\subsection{What is Error?}\label{what-is-error} + +Let's remind ourselves what error is: \[ +\text{Error} = \text{True Value} - \text{Approximate Value} +\] However, often the \textbf{true value} is unknown, so we focus on +\textbf{reducing} and \textbf{analyzing} different types of errors +instead of eliminating them completely. This can be done by using +relative error when using iterative methods and is calculated as +follows: \[ +\text{Relative Error} = \frac{\text{Best} - \text{Second to best}}{Best} +\] + +\subsection{Truncation Error}\label{truncation-error} + +Truncation error occurs \textbf{when an infinite process is approximated +by a finite process}.\\ +In simple terms, it happens \textbf{when you cut off or ``truncate'' +part of the computation}. An example of this could be using a finite +number of terms from a Taylor Series expansion to approximate a +function. + +Approximating \(e^x\) by the first few terms of its Taylor series: + +\[e^x \approx 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!}\] + +The error comes from \textbf{neglecting} all the higher order terms +(\(\frac{x^4}{4!}, \frac{x^5}{5!}\), \ldots). + +Truncation error occurs when using numerical methods such as +approximating and calculating derivatives and integrals. A +representation of the truncation error is show in the figure below. +Using our numerical methods we are left if some degree of error. + +\begin{figure} +\centering +\includegraphics{figures/truncationError.png} +\caption{Representation of truncation error under a curve} +\end{figure} + +In order to reduce truncation error there are a few things we can do: - +Include more terms (higher-order methods) - Decrease step sizes (e.g., +smaller \(\Delta x\) in approximations) - Use better approximation +algorithms. + +\subsection{Round-off Error}\label{round-off-error} + +Round-off error is caused by \textbf{the limited precision} with which +computers represent numbers. Since computers cannot store an infinite +number of digits, \textbf{they round off} after a certain number of +decimal or binary places. For example, instead of representing π with +infinite decimal places it may be rounded off to approximately 16 digits +depending on number of bits and the representation of the bits. + +In other words, round-off error happens because of how computers store +numbers. For a double-floating point, the number is stored using +64-bits. The more bits we use, the more precise of a number we can +store. However, it makes it costs us more memory making it more +computational expensive. + +While individual round-off errors may seem negligible, their effects can +\textbf{accumulate over repeated computations}, leading to significant +inaccuracies. This is particularly problematic in operations such as +\textbf{subtracting two nearly equal numbers}, where \textbf{loss of +significance} can occur, severely reducing numerical precision and +amplifying the impact of round-off error. + +\subsubsection{How to Reduce Round-off +Error:}\label{how-to-reduce-round-off-error} + +To reduce round-off error, use higher-precision data types when storing +numerical values. Additionally, code and algorithms should be structured +to \textbf{avoid subtracting nearly equal numbers}, a common source of +significant error. Finally, employing \textbf{numerically stable +algorithms} is essential for minimizing the accumulation of round-off +errors during computation. + +\subsection{Total Error}\label{total-error} + +Truncation and round-off error are inversely proportional, meaning that +if we decrease one, the other increases. If we want to minimize total +error we must find the optimal point between step size and error. + +\begin{figure} +\centering +\includegraphics{figures/totalError.png} +\caption{Total Error} +\end{figure} diff --git a/book/module2/non_linear_eqn_solver.tex b/book/module2/non_linear_eqn_solver.tex new file mode 100644 index 0000000..5550d12 --- /dev/null +++ b/book/module2/non_linear_eqn_solver.tex @@ -0,0 +1,129 @@ +\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} diff --git a/book/module3/numerical_methods.tex b/book/module3/numerical_methods.tex new file mode 100644 index 0000000..737d69d --- /dev/null +++ b/book/module3/numerical_methods.tex @@ -0,0 +1,41 @@ +\section{Numerical Methods}\label{numerical-methods} + +Engineering + +\subsection{What is a numerical +method?}\label{what-is-a-numerical-method} + +Numerical methods are techniques that transform mathematical problems +into forms that can be solved using arithmetic and logical operations. +Because digital computers excel at these computations, numerical methods +are often referred to as computer mathematics. + +\subsection{Numerical Differentiation}\label{numerical-differentiation} + +Forwards difference Backwards difference + +\href{https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter20.00-Numerical-Differentiation.html}{Read +More} + +\subsection{Roots and Optimization}\label{roots-and-optimization} + +Incremental Search Bisection Modified Secant Newton-Raphson + +\subsection{Numerical Integration}\label{numerical-integration} + +Trapezoidal + +Simpson's Rule + +\href{https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter21.00-Numerical-Integration.html}{Read +More} + +\subsection{Numerical Solutions of Ordinary Differential +Equations}\label{numerical-solutions-of-ordinary-differential-equations} + +Euler's Method - Forward - Backwards + +Runge-Kutta Method + +\href{https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter22.00-ODE-Initial-Value-Problems.html}{Read +More} |
