diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2025-04-24 15:31:56 -0600 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2025-04-24 15:31:56 -0600 |
| commit | 4abc43cdcb7fba399d5377481dd88e54b2db8cb6 (patch) | |
| tree | aaa358e30142bb7e97797688b4fbd4092bbe981a | |
| parent | 7e0b4501030aa268da323c1eaa69c8a2b29ee6a3 (diff) | |
Added tex file structure for each module
27 files changed, 1609 insertions, 9 deletions
diff --git a/book/module0/module0.tex b/book/module0/module0.tex index 7f74acf..7fe4a51 100644 --- a/book/module0/module0.tex +++ b/book/module0/module0.tex @@ -1,3 +1,3 @@ \chapter{Module 0} -\input{module0/intro_to_programming} \input{module0/intro_to_anaconda} +\input{module0/intro_to_programming} diff --git a/book/module1/basics_of_python.tex b/book/module1/basics_of_python.tex index 64883e7..29f8cc4 100644 --- a/book/module1/basics_of_python.tex +++ b/book/module1/basics_of_python.tex @@ -1,8 +1,3 @@ - \maketitle - - - - \hypertarget{basics-of-python}{ \section{Basics of Python}\label{basics-of-python}} diff --git a/book/module1/classes_and_objects.tex b/book/module1/classes_and_objects.tex new file mode 100644 index 0000000..cb9f7ec --- /dev/null +++ b/book/module1/classes_and_objects.tex @@ -0,0 +1,74 @@ +\section{Modular Programming}\label{modular-programming} + +\subsection{1. Introduction}\label{introduction} + +\begin{itemize} +\tightlist +\item + A. What is Object-Oriented Programming? +\item + B. Why use OOP? (vs.~procedural) +\item + C. Real-world analogies (e.g., modeling components like pumps, motors, + or vehicles) +\end{itemize} + +\begin{longtable}[]{@{} + >{\raggedleft\arraybackslash}p{(\columnwidth - 0\tabcolsep) * \real{0.0556}}@{}} +\toprule\noalign{} +\begin{minipage}[b]{\linewidth}\raggedleft +\#\# 2. Core OOP Concepts - A. \textbf{Classes and Objects} - +Definitions - Syntax in Python - B. \textbf{Attributes and Methods} - +Instance variables - Functions inside classes - C. +\textbf{Encapsulation} - Public vs private variables - Using +\texttt{\_\_init\_\_} and \texttt{self} - D. \textbf{Inheritance} - +Parent and child classes - Reuse and extension of code - E. +\textbf{Polymorphism} \emph{(brief overview)} - Method overriding - +Flexibility in interfaces +\end{minipage} \\ +\midrule\noalign{} +\endhead +\bottomrule\noalign{} +\endlastfoot +\#\# 3. Python OOP Syntax and Examples - A. Define a simple class (e.g., +\texttt{Spring}) - B. Instantiate objects and use methods - C. Show +\texttt{\_\_init\_\_}, \texttt{\_\_str\_\_}, custom methods - D. Add a +derived class (e.g., \texttt{DampedSpring} inherits from +\texttt{Spring}) \\ +\end{longtable} + +\subsection{4. Engineering Applications of +OOP}\label{engineering-applications-of-oop} + +\begin{itemize} +\tightlist +\item + A. Modeling a mechanical system using classes + + \begin{itemize} + \tightlist + \item + Example: Mass-Spring-Damper system + \end{itemize} +\item + B. Creating reusable components (e.g., \texttt{Material}, + \texttt{Beam}, \texttt{Force}) +\item + C. Organizing simulation code with OOP +\end{itemize} + +\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} + +\subsection{5. Hands-On Coding Activity}\label{hands-on-coding-activity} + +\begin{itemize} +\tightlist +\item + A. Write a class for a basic physical component (e.g., \texttt{Motor}) +\item + B. Add behavior (e.g., \texttt{calculate\_torque}) +\item + C. Extend with inheritance (e.g., \texttt{ServoMotor}) +\item + D. Bonus: Integrate two objects to simulate interaction +\end{itemize} diff --git a/book/module1/computational_expense.tex b/book/module1/computational_expense.tex new file mode 100644 index 0000000..f315969 --- /dev/null +++ b/book/module1/computational_expense.tex @@ -0,0 +1 @@ +\section{Computational Expense}\label{computational-expense} diff --git a/book/module1/computing_fundamentals.tex b/book/module1/computing_fundamentals.tex new file mode 100644 index 0000000..af38b93 --- /dev/null +++ b/book/module1/computing_fundamentals.tex @@ -0,0 +1,19 @@ +\section{Computing Fundamentals}\label{computing-fundamentals} + +\subsection{Using computers as a tool for +engineers}\label{using-computers-as-a-tool-for-engineers} + +\subsection{How do computers work?}\label{how-do-computers-work} + +Globe analogy: Hardware, Kernel, Shell, Application, Software. + +\subsection{Interfaces}\label{interfaces} + +\subsubsection{Text editor for +Scripting}\label{text-editor-for-scripting} + +\subsubsection{Command window}\label{command-window} + +Command window, terminal, console, command prompt you might've heard of +theses terms before. They all essentially mean the same thing. The +command window is used to control your system. diff --git a/book/module1/control_structures.tex b/book/module1/control_structures.tex new file mode 100644 index 0000000..746b7a5 --- /dev/null +++ b/book/module1/control_structures.tex @@ -0,0 +1,206 @@ +\section{Control Structures}\label{control-structures} + +Control structures allow us to control the flow of execution in a Python +program. The two main types are \textbf{conditional statements +(\texttt{if} statements)} and \textbf{loops (\texttt{for} and +\texttt{while} loops)}. + +\subsection{Conditional Statements}\label{conditional-statements} + +Conditional statements allow a program to execute different blocks of +code depending on whether a given condition is \texttt{True} or +\texttt{False}. These conditions are typically comparisons, such as +checking if one number is greater than another. + +\subsubsection{\texorpdfstring{The \texttt{if} +Statement}{The if Statement}}\label{the-if-statement} + +The simplest form of a conditional statement is the \texttt{if} +statement. If the condition evaluates to \texttt{True}, the indented +block of code runs. Otherwise, the program moves on without executing +the statement. + +For example, consider a situation where we need to determine if a person +is an adult based on their age. If the age is 18 or greater, we print a +message saying they are an adult. + +\subsubsection{\texorpdfstring{The \texttt{if-else} +Statement}{The if-else Statement}}\label{the-if-else-statement} + +Sometimes, we need to specify what should happen if the condition is +\texttt{False}. The \texttt{else} clause allows us to handle this case. +Instead of just skipping over the block, the program can execute an +alternative action. + +For instance, if a person is younger than 18, they are considered a +minor. If the condition of being an adult is not met, the program will +print a message indicating that the person is a minor. + +\subsubsection{\texorpdfstring{The \texttt{if-elif-else} +Statement}{The if-elif-else Statement}}\label{the-if-elif-else-statement} + +When dealing with multiple conditions, the \texttt{if-elif-else} +structure is useful. The program evaluates conditions in order, +executing the first one that is \texttt{True}. If none of the conditions +are met, the \texttt{else} block runs. + +For example, in a grading system, different score ranges correspond to +different letter grades. If a student's score is 90 or higher, they +receive an ``A''. If it's between 80 and 89, they get a ``B'', and so +on. If none of the conditions match, they receive an ``F''. + +\subsubsection{\texorpdfstring{Nested \texttt{if} +Statements}{Nested if Statements}}\label{nested-if-statements} + +Sometimes, we need to check conditions within other conditions. This is +known as \textbf{nesting}. For example, if we first determine that a +person is an adult, we can then check if they are a student. Based on +that information, we print different messages. + +\begin{Shaded} +\begin{Highlighting}[] +\CommentTok{\# Getting user input for the student\textquotesingle{}s score} +\NormalTok{score }\OperatorTok{=} \BuiltInTok{int}\NormalTok{(}\BuiltInTok{input}\NormalTok{(}\StringTok{"Enter the student\textquotesingle{}s score (0{-}100): "}\NormalTok{))} + +\ControlFlowTok{if} \DecValTok{0} \OperatorTok{\textless{}=}\NormalTok{ score }\OperatorTok{\textless{}=} \DecValTok{100}\NormalTok{:} + \ControlFlowTok{if}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{90}\NormalTok{:} +\NormalTok{ grade }\OperatorTok{=} \StringTok{"A"} + \ControlFlowTok{elif}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{80}\NormalTok{:} +\NormalTok{ grade }\OperatorTok{=} \StringTok{"B"} + \ControlFlowTok{elif}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{70}\NormalTok{:} +\NormalTok{ grade }\OperatorTok{=} \StringTok{"C"} + \ControlFlowTok{elif}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{60}\NormalTok{:} +\NormalTok{ grade }\OperatorTok{=} \StringTok{"D"} + \ControlFlowTok{else}\NormalTok{:} +\NormalTok{ grade }\OperatorTok{=} \StringTok{"F"} \CommentTok{\# Score below 60 is a failing grade} + + + \ControlFlowTok{if}\NormalTok{ grade }\OperatorTok{==} \StringTok{"F"}\NormalTok{:} + \BuiltInTok{print}\NormalTok{(}\StringTok{"The student has failed."}\NormalTok{)} +\NormalTok{ retake\_eligible }\OperatorTok{=} \BuiltInTok{input}\NormalTok{(}\StringTok{"Is the student eligible for a retest? (yes/no): "}\NormalTok{).strip().lower()} + + \ControlFlowTok{if}\NormalTok{ retake\_eligible }\OperatorTok{==} \StringTok{"yes"}\NormalTok{:} + \BuiltInTok{print}\NormalTok{(}\StringTok{"The student is eligible for a retest."}\NormalTok{)} + \ControlFlowTok{else}\NormalTok{:} + \BuiltInTok{print}\NormalTok{(}\StringTok{"The student has failed the course and must retake it next semester."}\NormalTok{)} + + +\end{Highlighting} +\end{Shaded} + +\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} + +\subsection{Loops in Python}\label{loops-in-python} + +Loops allow a program to execute a block of code multiple times. This is +especially useful for tasks such as processing lists of data, performing +repetitive calculations, or automating tasks. + +\subsubsection{\texorpdfstring{The \texttt{for} +Loop}{The for Loop}}\label{the-for-loop} + +A \texttt{for} loop iterates over a sequence, such as a list, tuple, +string, or a range of numbers. Each iteration assigns the next value in +the sequence to a loop variable, which can then be used inside the loop. + +For instance, if we have a list of fruits and want to print each fruit's +name, a \texttt{for} loop can iterate over the list and display each +item. + +Another useful feature of \texttt{for} loops is the \texttt{range()} +function, which generates a sequence of numbers. This is commonly used +when we need to repeat an action a specific number of times. For +example, iterating from 0 to 4 allows us to print a message five times. + +Additionally, the \texttt{enumerate()} function can be used to loop +through a list while keeping track of the index of each item. This is +useful when both the position and the value in a sequence are needed. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{fruits }\OperatorTok{=}\NormalTok{ [}\StringTok{"apple"}\NormalTok{, }\StringTok{"banana"}\NormalTok{, }\StringTok{"cherry"}\NormalTok{] } +\ControlFlowTok{for}\NormalTok{ x }\KeywordTok{in}\NormalTok{ fruits: } + \BuiltInTok{print}\NormalTok{(x)} +\end{Highlighting} +\end{Shaded} + +\begin{Shaded} +\begin{Highlighting}[] +\ControlFlowTok{for}\NormalTok{ x }\KeywordTok{in} \BuiltInTok{range}\NormalTok{(}\DecValTok{6}\NormalTok{): } + \BuiltInTok{print}\NormalTok{(x) } +\ControlFlowTok{else}\NormalTok{: } + \BuiltInTok{print}\NormalTok{(}\StringTok{"Finally finished!"}\NormalTok{)} +\end{Highlighting} +\end{Shaded} + +\subsubsection{\texorpdfstring{The \texttt{while} +Loop}{The while Loop}}\label{the-while-loop} + +Unlike \texttt{for} loops, which iterate over a sequence, \texttt{while} +loops continue running as long as a specified condition remains +\texttt{True}. This is useful when the number of iterations is not known +in advance. + +For example, a countdown timer can be implemented using a \texttt{while} +loop. The loop will continue decreasing the count until it reaches zero. + +It's important to be careful with \texttt{while} loops to avoid infinite +loops, which occur when the condition never becomes \texttt{False}. To +prevent this, ensure that the condition will eventually change during +the execution of the loop. + +A \texttt{while} loop can also be used to wait for a certain event to +occur. For example, in interactive programs, a \texttt{while\ True} loop +can keep running until the user provides a valid input, at which point +we break out of the loop. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{i }\OperatorTok{=} \DecValTok{1} +\ControlFlowTok{while}\NormalTok{ i }\OperatorTok{\textless{}} \DecValTok{6}\NormalTok{: } +\NormalTok{ print(i) } +\NormalTok{ i }\OperatorTok{+=} \DecValTok{1} +\end{Highlighting} +\end{Shaded} + +\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} + +\subsection{Loop Control Statements}\label{loop-control-statements} + +Python provides special statements to control the behavior of loops. +These can be used to break out of a loop, skip certain iterations, or +simply include a placeholder for future code. + +\subsubsection{\texorpdfstring{The \texttt{break} +Statement}{The break Statement}}\label{the-break-statement} + +The \texttt{break} statement is used to exit a loop before it has +iterated through all its elements. When the \texttt{break} statement is +encountered, the loop stops immediately, and the program continues +executing the next statement outside the loop. + +For instance, if we are searching for a specific value in a list, we can +use a \texttt{break} statement to stop the loop as soon as we find the +item, instead of continuing unnecessary iterations. + +\subsubsection{\texorpdfstring{The \texttt{continue} +Statement}{The continue Statement}}\label{the-continue-statement} + +The \texttt{continue} statement is used to skip the current iteration +and proceed to the next one. Instead of exiting the loop entirely, it +simply moves on to the next cycle. + +For example, if we are iterating over numbers and want to skip +processing number 2, we can use \texttt{continue}. The loop will ignore +that iteration and proceed with the next number. + +\subsubsection{\texorpdfstring{The \texttt{pass} +Statement}{The pass Statement}}\label{the-pass-statement} + +The \texttt{pass} statement is a placeholder that does nothing. It is +useful when a block of code is syntactically required but no action +needs to be performed yet. + +For example, in a loop where a condition has not yet been implemented, +using \texttt{pass} ensures that the code remains valid while avoiding +errors. diff --git a/book/module1/functions.tex b/book/module1/functions.tex new file mode 100644 index 0000000..07da182 --- /dev/null +++ b/book/module1/functions.tex @@ -0,0 +1,110 @@ +\section{Functions}\label{functions} + +Like a traditional mathematical functions, python functions can take an +input, process it, and give an output. In python, the input variables +are referred to as \emph{arguments}. Functions are blocks of code that +is run every time it's called. This allows us to re-use code. + +Functions are defined by using the def keyword. Reminder: it is +important to keep track of indentations as it signifies the end of the +function when the indentation returns back to the same level. + +\subsection{Defining Functions}\label{defining-functions} + +\subsubsection{Simple function}\label{simple-function} + +A simple function with no input variable can be useful if you need to +re-use code multiple times without having to re-write it. + +\begin{Shaded} +\begin{Highlighting}[] + \KeywordTok{def}\NormalTok{ function\_name():} + \BuiltInTok{print}\NormalTok{(}\StringTok{"This is from a function"}\NormalTok{)} +\end{Highlighting} +\end{Shaded} + +\subsubsection{Defining a function with one +input}\label{defining-a-function-with-one-input} + +We can pass variables through to the function to be processed as +follows: + +\begin{Shaded} +\begin{Highlighting}[] + \KeywordTok{def}\NormalTok{ function(x):} + \BuiltInTok{print}\NormalTok{(x }\OperatorTok{+} \StringTok{" is best"}\NormalTok{)} +\end{Highlighting} +\end{Shaded} + +Note input variables can be of any data type (integer, float, string, +etc.). \#\#\# Returning values from a function If we want to calculate a +value and pass it back to the script for further use, we can use the +\texttt{return} keyword. Let's define a linear function that takes two +inputs, \texttt{x} and \texttt{b}, computes the corresponding \texttt{y} +value, and returns it so it can be used elsewhere in the code. + +\begin{Shaded} +\begin{Highlighting}[] + \KeywordTok{def}\NormalTok{ function(x, b):} +\NormalTok{ y }\OperatorTok{=} \DecValTok{3}\OperatorTok{*}\NormalTok{x}\OperatorTok{+}\NormalTok{b} + \ControlFlowTok{return}\NormalTok{ y} +\end{Highlighting} +\end{Shaded} + +For multiple output variables we can add \#\# Calling functions Now that +we've covered defining functions we want to call the function in order +to execute the block inside the function. To do this, we simply re-call +the function name as follows. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{function(}\DecValTok{2}\NormalTok{,}\OperatorTok{{-}}\DecValTok{1}\NormalTok{)} +\end{Highlighting} +\end{Shaded} + +Note that when running this code, nothing happens. This is because we +haven't told the computer what to do with the output. Hence, if we wish +to store the output then we need to use the assign operator \texttt{=}. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{output }\OperatorTok{=}\NormalTok{ function(}\DecValTok{2}\NormalTok{,}\OperatorTok{{-}}\DecValTok{1}\NormalTok{)} + +\BuiltInTok{print}\NormalTok{(output)} +\end{Highlighting} +\end{Shaded} + +In case you want to return multiple output variable from a single +function we will have\ldots{} + +\subsection{Summary}\label{summary} + +\begin{Shaded} +\begin{Highlighting}[] +\KeywordTok{def}\NormalTok{ function\_name(argument1, argument2, argument3)} +\NormalTok{ output1 }\OperatorTok{=}\NormalTok{ argument1 }\OperatorTok{*}\NormalTok{ argument2 }\OperatorTok{{-}}\NormalTok{ argument3} +\NormalTok{ output2 }\OperatorTok{=}\NormalTok{ argument2 }\OperatorTok{+}\NormalTok{ argument3} + \ControlFlowTok{return}\NormalTok{ output1, output2} + +\NormalTok{[solution1, solution2] }\OperatorTok{=}\NormalTok{ function\_name(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{)} +\end{Highlighting} +\end{Shaded} + +\begin{itemize} +\tightlist +\item + \texttt{def} - defines a function. All the code that is indented + underneath is considered inside the function block. +\item + \texttt{function\_name} - this is used to call the function block. +\item + \texttt{argument1} (optional) - input variable. This is data that can + be pass to the function. It is possible to have multiple variables + separated by a comma. As well as can be omitted if the function should + just give you an output such as. +\item + \texttt{return} (optional) - if you wish to return something to your + script, the return keyword is used. The keyword can be followed by an + output variable or a constant. For multiple output variables, separate + them by a comma. +\end{itemize} diff --git a/book/module1/fundamentals_of_programming.tex b/book/module1/fundamentals_of_programming.tex new file mode 100644 index 0000000..6a412d1 --- /dev/null +++ b/book/module1/fundamentals_of_programming.tex @@ -0,0 +1,25 @@ +\section{Fundamentals of programming}\label{fundamentals-of-programming} + +\subsection{Orientation of common +interfaces}\label{orientation-of-common-interfaces} + +In this section we will cover the use and purpose of some common +interfaces that you'll be using in this course. + +\subsubsection{Command window, terminal, console, command +prompt.}\label{command-window-terminal-console-command-prompt.} + +This is a text based interface that allows the users to interact with +the computer. It is used to execute commands, run scripts or programs. + +\subsubsection{Text Editor / Script}\label{text-editor-script} + +Your text editor is the program used to write a script which can be +re-run every time you call it from the command window. This can be a +built-in text editor such as Spyder and MATLAB provide or an external on +such a notepad++. + +\begin{verbatim} + Globe analogy: Hardware, Kernel, shell, Application software. +- Scripting +\end{verbatim} diff --git a/book/module1/installing_anaconda.tex b/book/module1/installing_anaconda.tex new file mode 100644 index 0000000..9b9bf1a --- /dev/null +++ b/book/module1/installing_anaconda.tex @@ -0,0 +1,160 @@ +\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. + +\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} + +\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/module1/intro_to_anaconda.tex b/book/module1/intro_to_anaconda.tex new file mode 100644 index 0000000..5e58908 --- /dev/null +++ b/book/module1/intro_to_anaconda.tex @@ -0,0 +1,193 @@ +\section{Introduction to Anaconda +Navigator}\label{introduction-to-anaconda-navigator} + +Anaconda Navigator is a program that we will be using in this course to +manage Python environments, libraries and launch programs to help us +write our python code. + +The Anaconda website nicely describes \emph{Navigator} as: + +a graphical user interface (GUI) that enables you to work with packages +and environments without needing to type conda commands in a terminal +window.Find the packages you want, install them in an environment, run +the packages, and update them -- all inside Navigator. + +To better understand how Navigator works and interacts with the anaconda +ecosystem see the figure below. +\includegraphics{figures/AnacondaSchematic.png} As you schematic +indicated, Navigator is a tool in the Anaconda toolbox that allows the +user to select and configure python environments and libraries. Let's +see how we can do this. + +\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} + +\subsection{Getting Started}\label{getting-started} + +Note to windows 10 users: Some installation instances do not allow users +to search the start menu for \emph{Navigator}, instead, you'll have to +find the program under the \emph{Anaconda (anaconda3)} folder. Expand +the folder and click on \emph{Anaconda Navigator} to launch the program. + +\begin{figure} +\centering +\includegraphics{figures/installingAnaconda_windows_launched.png} +\caption{Anaconda Navigator screen} +\end{figure} + +Once Navigator starts, under \emph{Home}, you'll see tiles of programs +that come with anaconda. The tab allows you to launch the programs we +will be using in this course. Before jumping straight into the programs +we will first need to configure our Python instance. + +The \emph{Environment} page allows us to install a variety of libraries +and configure our environments for different project, more on this in +the next section. + +\subsection{Environments}\label{environments} + +A Python environment can be thought of as a ``container'' where you can +have all the tools, libraries, and dependencies your Python project +needs without interfering with other projects. Think of it as a +dedicated toolbox for your project. + +Although the base environment comes with many libraries and programs +pre-installed, it's recommended to create a dedicated environment for +your projects. This protects the base environment from breaking due to +complex dependency conflicts. Let us go ahead and create a new +environment for us to use Spyder with. + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\tightlist +\item + Click on the \emph{Environments} page located on the left hand side. +\end{enumerate} + +\begin{figure} +\centering +\includegraphics{https://mintlify.s3.us-west-1.amazonaws.com/anaconda-29683c67/images/nav-env-labeled.png} +\caption{Environment Page} +\end{figure} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{1} +\tightlist +\item + At the bottom of the environments list, click \emph{Create}. +\end{enumerate} + +\begin{figure} +\centering +\includegraphics{https://mintlify.s3.us-west-1.amazonaws.com/anaconda-29683c67/images/nav-getting-started-create.png} +\caption{Create new environment} +\end{figure} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{2} +\item + Select the python checkbox. +\item + Select versions of python. At the time of making this tutorial the + latest version of Python is 3.xx.x. We will go ahead and use that one. +\item + Choose an appropriate name for your project. We will be creating an + environment for the Spyder IDE so we'll call it ``Spyder-env''. +\item + Click \emph{Create}. +\end{enumerate} + +For more information see +\href{https://docs.anaconda.com/working-with-conda/environments/}{Anaconda +Environments} and +\href{https://docs.anaconda.com/navigator/tutorials/manage-environments/}{Managing +environment}. + +\subsection{Package Management}\label{package-management} + +Now that we have a clean environment configured, let us install some +library we will be using for this class. + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\tightlist +\item + Navigate to the environment page and select the environment we just + created in the previous section. +\end{enumerate} + +\begin{figure} +\centering +\includegraphics{https://mintlify.s3.us-west-1.amazonaws.com/anaconda-29683c67/images/nav-pkg-list.png} +\caption{Select environment to manage} +\end{figure} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{1} +\tightlist +\item + Use the search bar in the top right corner to search for the following + packages: +\end{enumerate} + +\begin{longtable}[]{@{}ll@{}} +\toprule\noalign{} +Library & Usage \\ +\midrule\noalign{} +\endhead +\bottomrule\noalign{} +\endlastfoot +numpy & Numerical computation \\ +scipy & Scientific and techical computing \\ +pandas & Data manipulation and analysis \\ +matplotlib & Plots and visualizations \\ +sympy & Symbolic mathematics \\ +\end{longtable} + +\emph{Note: The libraries list may change throughout the development of +this course} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{2} +\tightlist +\item + Check the boxes to install the selected packages to the current + environment. +\end{enumerate} + +\subsection{Installing Applications}\label{installing-applications} + +From the \emph{Home} page you can install applications, to the current +environment we created in the Environment section above. In this section +we will install Spyder IDE, but the process is exactly the same for +other applications. + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\item + Go to the \emph{Home} page. +\item + Select the desired environment. In our case, we select + \emph{Spyder-env}. +\item + From the Home page find the Spyder IDE tile. Click the \emph{Install} + button to start the download. +\end{enumerate} + +\begin{figure} +\centering +\includegraphics{https://mintlify.s3.us-west-1.amazonaws.com/anaconda-29683c67/images/nav-tabs.png} +\caption{Anaconda Home Page} +\end{figure} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\setcounter{enumi}{3} +\tightlist +\item + Once the download is complete, press \emph{Launch} to start the + applications. +\end{enumerate} diff --git a/book/module1/intro_to_programming.tex b/book/module1/intro_to_programming.tex new file mode 100644 index 0000000..c73dcf4 --- /dev/null +++ b/book/module1/intro_to_programming.tex @@ -0,0 +1,38 @@ +\section{Introduction to Programming}\label{introduction-to-programming} + +\subsection{The Importance of Programming in +Engineering}\label{the-importance-of-programming-in-engineering} + +Engineering is all about solving problems, designing innovative +solutions, and making systems work efficiently. Whether you're designing +cars, airplanes, rockets, or even everyday machines, programming plays a +critical role in modern engineering. + +In mechanical engineering, programming helps us \textbf{analyze data, +model complex systems, automate repetitive tasks, and simulate +real-world physics.} For example, instead of spending hours solving +equations by hand, engineers can write a program that does it in +seconds. This saves time and therefore do more. + +With programming, mechanical engineers can: + +\begin{itemize} +\tightlist +\item + \textbf{Automate calculations:} Quickly solve equations for heat + transfer, fluid dynamics, and mechanical stresses. +\item + \textbf{Simulate systems:} Model how a bridge bends under weight or + how an engine burns fuel efficiently. +\item + \textbf{Analyze data:} Process thousands of test results to improve + designs. +\item + \textbf{Control machines:} Program robots, 3D printers, and CNC's. +\end{itemize} + +In this course, you'll see how computing and programming applies to +mechanical engineering and how they can make you a better problem +solver. By the end, you'll have the skills and understanding of how to +write programs that help you \textbf{think like an engineer in the +digital age.} diff --git a/book/module1/module1.tex b/book/module1/module1.tex index cecf097..5f4f16b 100644 --- a/book/module1/module1.tex +++ b/book/module1/module1.tex @@ -1,3 +1,14 @@ \chapter{Module 1} -\input{module1/basics_of_python} \input{module1/arrays} +\input{module1/basics_of_python} +\input{module1/classes_and_objects} +\input{module1/computational_expense} +\input{module1/computing_fundamentals} +\input{module1/control_structures} +\input{module1/functions} +\input{module1/fundamentals_of_programming} +\input{module1/installing_anaconda} +\input{module1/intro_to_anaconda} +\input{module1/intro_to_programming} +\input{module1/open_source_software} +\input{module1/spyder_getting_started} diff --git a/book/module1/open_source_software.tex b/book/module1/open_source_software.tex new file mode 100644 index 0000000..96de292 --- /dev/null +++ b/book/module1/open_source_software.tex @@ -0,0 +1,104 @@ +\section{Open Source Software}\label{open-source-software} + +Open-source software (OSS) is a type of software that allows users to +access, modify, and distribute its source code freely. It is built on +principles of collaboration, transparency, and community-driven +development. + +You've probably heard of the saying ``Don't reinventing the wheel''. +This + +\subsubsection{Key Principles of Open Source +Software}\label{key-principles-of-open-source-software} + +\begin{itemize} +\tightlist +\item + \textbf{Free Distribution:} Anyone can download and use the software + without cost. +\item + \textbf{Access to Source Code:} Users can view and modify the code to + suit their needs. +\item + \textbf{Community Collaboration:} Developers from around the world + contribute to improvements and security fixes. +\end{itemize} + +\subsubsection{Benefits of Open Source +Software}\label{benefits-of-open-source-software} + +\begin{itemize} +\tightlist +\item + \textbf{Cost-effectiveness:} Open-source software is free to use, + making it accessible to individuals and organizations. +\item + \textbf{Transparency and Security:} Open code allows for peer review, + reducing security vulnerabilities. +\item + \textbf{Community Support:} Global developer communities provide + assistance, troubleshooting, and improvements. +\item + \textbf{Customization and Flexibility:} Users can modify software to + fit their specific requirements. +\end{itemize} + +\subsubsection{Challenges of Open Source +Software}\label{challenges-of-open-source-software} + +\begin{itemize} +\tightlist +\item + \textbf{Usability Issues:} Some open-source software may have a + steeper learning curve. +\item + \textbf{Compatibility Problems:} Integration with proprietary systems + may require additional effort. +\item + \textbf{Support and Documentation:} The quality of documentation and + support varies. +\item + \textbf{Sustainability:} Open-source projects often rely on + volunteers, which can lead to inconsistent updates. +\end{itemize} + +\subsubsection{Popular Open Source +Projects}\label{popular-open-source-projects} + +\begin{itemize} +\tightlist +\item + \textbf{Operating Systems:} Linux, Ubuntu +\item + \textbf{Web Browsers:} Mozilla Firefox +\item + \textbf{Programming Languages:} Python, JavaScript +\item + \textbf{Office Suites:} LibreOffice +\item + \textbf{Multimedia Tools:} Audacity, Blender +\item + \textbf{Software Development:} Git, GitHub, Apache +\end{itemize} + +\subsubsection{How to Contribute to Open +Source}\label{how-to-contribute-to-open-source} + +\begin{itemize} +\tightlist +\item + \textbf{Finding Projects:} Platforms like GitHub, GitLab, and + SourceForge host many open-source projects. +\item + \textbf{Understanding Licensing:} Common licenses include GPL, MIT, + and Apache. +\item + \textbf{Ways to Contribute:} Developers can contribute code, test + software, write documentation, translate, or help with design. +\item + \textbf{Best Practices for Contributions:} Using version control + (Git), writing clean code, and following community guidelines are + essential for successful collaboration. +\end{itemize} + +\subsection{Licensing}\label{licensing} diff --git a/book/module1/spyder_getting_started.tex b/book/module1/spyder_getting_started.tex new file mode 100644 index 0000000..d1e20fe --- /dev/null +++ b/book/module1/spyder_getting_started.tex @@ -0,0 +1,105 @@ +\section{Getting started with Spyder}\label{getting-started-with-spyder} + +In this tutorial we will cover the basics of using the Spyder IDE +(Interactive Development Environment). If you've ever worked with MATLAB +before, then this will feel familiar. Spyder is a program that allows +you to write, run and de-bug code. + +\subsection{Launching Spyder}\label{launching-spyder} + +Using Anaconda we will select the environment we created earlier +\emph{spyder-dev} and then we can launch spyder from the Home page. + +\subsection{Spyder Interface}\label{spyder-interface} + +\begin{figure} +\centering +\includegraphics{https://docs.spyder-ide.org/current/_images/mainwindow_default_1610.png} +\caption{Spyder Interface} +\end{figure} + +Once you open up Spyder in it's default configuration, you'll see three +sections; the editor IPython Console, Help viewer. You can customize the +interface to suit your prefference and needs some of which include, +rearrange, undock, hide panes. Feel free to set up Spyder as you like. + +\subsubsection{Editor}\label{editor} + +This pane is used to write your scripts. The + +\begin{figure} +\centering +\includegraphics{https://docs.spyder-ide.org/5/_images/editor-components.png} +\caption{Editor key components} +\end{figure} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\tightlist +\item + The left sidebar shows line numbers and displays any code analysis + warnings that exist in the current file. Clicking a line number + selects the text on that line, and clicking to the right of it sets a + \href{https://docs.spyder-ide.org/5/panes/debugging.html\#debugging-breakpoints}{breakpoint}. +\item + The scrollbars allow vertical and horizontal navigation in a file. +\item + The context (right-click) menu displays actions relevant to whatever + was clicked. +\item + The options menu (``Hamburger'' icon at top right) includes useful + settings and actions relevant to the Editor. +\item + The location bar at the top of the Editor pane shows the full path of + the current file. +\item + The tab bar displays the names of all opened files. It also has a + Browse tabs button (at left) to show every open tab and switch between + them---which comes in handy if many are open. +\end{enumerate} + +\subsubsection{IPython Console}\label{ipython-console} + +This pane allows you to interactively run functions, do math +computations, assign and modify variables. + +\begin{figure} +\centering +\includegraphics{https://docs.spyder-ide.org/5/_images/console-standard.png} +\caption{IPython Console} +\end{figure} + +\begin{itemize} +\tightlist +\item + Automatic code completion +\item + Real-time function calltips +\item + Full GUI integration with the enhanced Spyder + \href{https://docs.spyder-ide.org/5/panes/debugging.html}{Debugger}. +\item + The + \href{https://docs.spyder-ide.org/5/panes/variableexplorer.html}{Variable + Explorer}, with GUI-based editors for many built-in and third-party + Python objects. +\item + Display of Matplotlib graphics in Spyder's + \href{https://docs.spyder-ide.org/5/panes/plots.html}{Plots} pane, if + the Inline backend is selected under Preferences ‣ IPython console ‣ + Graphics ‣ Graphics backend, and inline in the console if Mute inline + plotting is unchecked under the Plots pane's options menu. +\end{itemize} + +\subsubsection{Variable Explorer}\label{variable-explorer} + +This pane shows all the defined variables (objects) stored. This can be +used to identify the data type of variables, the size and inspect larger +arrays. Double clicking the value cell opens up a window which allowing +you to inspect the data in a spreadsheet like view. + +\begin{figure} +\centering +\includegraphics{https://docs.spyder-ide.org/5/_images/variable-explorer-standard.png} +\caption{Variable Explorer} +\end{figure} diff --git a/book/module2/ai_assisted_programming.tex b/book/module2/ai_assisted_programming.tex new file mode 100644 index 0000000..2fee22b --- /dev/null +++ b/book/module2/ai_assisted_programming.tex @@ -0,0 +1,65 @@ +\section{AI Assisted Programming}\label{ai-assisted-programming} + +\subsection{What is it?}\label{what-is-it} + +Artificial Intelligence (AI) has been around for a long time. However, +not until recently did engineers make it easy and ``fun'' to work with. +By now you probably have a pretty good idea of what AI can do. However, +you may not have used an AI assistant before. As the name suggests, an +AI assistant can help you develop code, speed up your writing with code +suggestions and allows you to focus on solving the problem at hand +rather. AI is a technology that is constantly improving. As engineers we +need to \emph{understand} how we can use AI as a tool to achieve our +goals more efficiently. This section cover good practices of how we can +implement AI and lists some AI assistant tools that we can use. \#\# +Good vs.~Bad uses of AI Don't try to get AI to do work \emph{for you} +but \emph{with you}. You need to understand what you're doing. If you +don't understand what the AI is doing, then you're not in control of the +work. You're not going to go far until something unexpected happens. + +AI is a great learning tool, research as show that students can benefit +from using AI as personal tutor +\href{https://hbsp.harvard.edu/inspiring-minds/ai-as-personal-tutor}{more}. +\#\# Available tools Below is a comprehensive list of tools that are +available at not cost to you. + +\begin{longtable}[]{@{}ll@{}} +\toprule\noalign{} +Name & Features \\ +\midrule\noalign{} +\endhead +\bottomrule\noalign{} +\endlastfoot +GitHub Copilot & Paid, but free for students. Integrated in GitHub. \\ +ChatGPT & Free, optional paid upgrade \\ +Grok & Free, optional paid upgrade \\ +Gemini & Free, optional paid upgrade \\ +GPT4ALL & Free and Open-Source \\ +Code GPT & Free and Open-Source \\ +Cody & Free and Open-Source \\ +DataLab AI & Free \\ +Codeium & Free \\ +aiXcoder & Free \\ +\end{longtable} + +Many of the tools above come with similar, if not, the same features. +Some of the tools come as chatbots on the web and others are extensions +that can be implemented in your favorite IDE. \#\# VSCode and GitHub +Copilot Integration We will not cover how to use VSCode in this course, +however it is a very versatile IDE that comes with many other extension, +for example git, github and github copilot integration. There are also +other extensions for other IDE's however we will only cover some basic +features that the GithHub Copilot extension in VSCode can do. + +Copilot Comes with the following features: - Get code suggestions as you +type - Ask questions about the code - Inline chat to generate code. - +Fix and debug code using the chat window - Generate code documentation + +\href{https://code.visualstudio.com/}{VSCode} +\href{https://code.visualstudio.com/docs/copilot/setup-simplified}{Copilot +extension} \#\# A note on integrity If you have a +non-disclosure-agreement (NDA) with your employer, it may not always be +possible to use AI for security and integrity reasons as you may risk +exposing confidential information with third party vendors. It is highly +recommended to be able to be able to write program independently of an +AI assistant. Always think before you share data. diff --git a/book/module2/debugging_code.tex b/book/module2/debugging_code.tex new file mode 100644 index 0000000..ce3e99e --- /dev/null +++ b/book/module2/debugging_code.tex @@ -0,0 +1,144 @@ +\section{Debugging Code}\label{debugging-code} + +\subsection{Introduction}\label{introduction} + +Have you ever had a piece of code not work the way you expected? What +did you do? You may have , asked a friend or used an AI assistant. In +this section, the following concepts are introduced - definition of a +bug, common types of bugs and debugging techniques. + +A \emph{software bug} is an unintentional mistake or defect with a +program, this comes either from when the programmer makes a mistake in +writing the code or the code works in a way which has consequences that +were not foreseen by the programmer. Debugging is the act removing the +bugs in the software. Debugging is a normal part of programming that +even experiences developers spend a lot of time on. + +\subsection{Types of Bugs}\label{types-of-bugs} + +When writing code you are guaranteed to have bugs in your code. These +bugs can be categorized in the following three groups. + +\begin{itemize} +\tightlist +\item + \textbf{Syntax errors} - this type of error occurs when the code fails + due to missing colons, missing indentation or a typo in code - some + languages like python are case sensitive meaning that the a capital + letter are different symbols. +\item + \textbf{Runtime errors} - e.g., dividing by zero or file not found. +\item + \textbf{Logical errors} - this may be the most dangerous that we need + to be careful with because this error can occur without any error + messages but it gives you the wrong result. +\end{itemize} + +\subsection{Debugging Techniques}\label{debugging-techniques} + +\paragraph{Print Debugging}\label{print-debugging} + +Insert print statements to check values of variables throughout the +program. + +\begin{Shaded} +\begin{Highlighting}[] +\KeywordTok{def}\NormalTok{ add(x, y):} + \BuiltInTok{print}\NormalTok{(}\SpecialStringTok{f"x = }\SpecialCharTok{\{}\NormalTok{x}\SpecialCharTok{\}}\SpecialStringTok{, y = }\SpecialCharTok{\{}\NormalTok{y}\SpecialCharTok{\}}\SpecialStringTok{"}\NormalTok{)} + \ControlFlowTok{return}\NormalTok{ x }\OperatorTok{+}\NormalTok{ y} +\end{Highlighting} +\end{Shaded} + +In the example above the print statement gives us feedback on what the +code is doing. The function in this example is obviously very simple, +but when we start applying more complex equations or function then +checking to see if the input variables are correct can indicate whether +there is an issue lies within the \texttt{add()} function or if the +function is given an incorrect input. + +\paragraph{Rubber Duck Debugging}\label{rubber-duck-debugging} + +This is a technique by which you explaining your code line by line in +natural language to someone else, yourself or an inanimate object like a +rubber duck. This can help you spot your mistake in the code. + +\paragraph{Commenting Out Code}\label{commenting-out-code} + +Using comments to temporarily suppress parts of your code help you +isolate and find the bug. + +\paragraph{IDE Debugging tools}\label{ide-debugging-tools} + +Depending if you use an IDE, they often come with some sort of debugging +tools such as breakpoints, step into/over and variables explorers. + +\paragraph{AI Chat}\label{ai-chat} + +AI chat bots can help you find typo or fix logic in your code. You may +find yourself going through the steps above when using an AI assistant +to help you debug the code. However \emph{never} assume that the code AI +gives you works the way you intend it to work. + +\subsection{Interactive Debugging +Activity}\label{interactive-debugging-activity} + +In the following code snippets, debug the code and document the +following: - What the bug is - How you found it (technique used) - What +actions you took to fix the bug + +\paragraph{Code 1}\label{code-1} + +\begin{Shaded} +\begin{Highlighting}[] +\KeywordTok{def}\NormalTok{ greet(name)} + \BuiltInTok{print}\NormalTok{(}\StringTok{"Hello, "} \OperatorTok{+}\NormalTok{ Name)} +\NormalTok{greet(}\StringTok{"John"}\NormalTok{)} +\end{Highlighting} +\end{Shaded} + +\paragraph{Code 2}\label{code-2} + +\begin{Shaded} +\begin{Highlighting}[] +\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np} + +\NormalTok{x }\OperatorTok{=}\NormalTok{ np.linspace(}\DecValTok{0}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{100}\NormalTok{)} +\NormalTok{y }\OperatorTok{=} \DecValTok{1}\OperatorTok{/}\NormalTok{x} + +\BuiltInTok{print}\NormalTok{(}\StringTok{"Result:"}\NormalTok{, y[}\DecValTok{0}\NormalTok{])} +\end{Highlighting} +\end{Shaded} + +\paragraph{Code 3}\label{code-3} + +\begin{Shaded} +\begin{Highlighting}[] +\KeywordTok{def}\NormalTok{ f(x):} + \ControlFlowTok{return}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}} \DecValTok{4} \CommentTok{\# Root at x = ±2} + +\KeywordTok{def}\NormalTok{ bisection(a, b, tol}\OperatorTok{=}\FloatTok{1e{-}5}\NormalTok{, max\_iter}\OperatorTok{=}\DecValTok{100}\NormalTok{):} + \ControlFlowTok{if}\NormalTok{ f(a) }\OperatorTok{*}\NormalTok{ f(b) }\OperatorTok{\textgreater{}=} \DecValTok{0}\NormalTok{:} + \BuiltInTok{print}\NormalTok{(}\StringTok{"Bisection method fails. f(a) and f(b) should have opposite signs."}\NormalTok{)} + \ControlFlowTok{return} \VariableTok{None} + + \ControlFlowTok{for}\NormalTok{ i }\KeywordTok{in} \BuiltInTok{range}\NormalTok{(max\_iter):} +\NormalTok{ c }\OperatorTok{=}\NormalTok{ (a }\OperatorTok{+}\NormalTok{ b) }\OperatorTok{/} \DecValTok{2} + \ControlFlowTok{if} \BuiltInTok{abs}\NormalTok{(f(c)) }\OperatorTok{\textless{}}\NormalTok{ tol:} + \ControlFlowTok{return}\NormalTok{ c} + \ControlFlowTok{elif}\NormalTok{ f(c) }\OperatorTok{*}\NormalTok{ f(b) }\OperatorTok{\textless{}} \DecValTok{0}\NormalTok{:} +\NormalTok{ a }\OperatorTok{=}\NormalTok{ c} + \ControlFlowTok{else}\NormalTok{:} +\NormalTok{ b }\OperatorTok{=}\NormalTok{ c} + \ControlFlowTok{return}\NormalTok{ (a }\OperatorTok{+}\NormalTok{ b) }\OperatorTok{/} \DecValTok{2} +\end{Highlighting} +\end{Shaded} + +\subsection{Reflection}\label{reflection} + +\begin{itemize} +\tightlist +\item + What was the most challenging bug you found? +\item + What debugging method did you find most useful? +\end{itemize} diff --git a/book/module2/intro_to_numerical_methods.tex b/book/module2/intro_to_numerical_methods.tex new file mode 100644 index 0000000..737d69d --- /dev/null +++ b/book/module2/intro_to_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} diff --git a/book/module2/module2.tex b/book/module2/module2.tex index d5e9785..f16e7e2 100644 --- a/book/module2/module2.tex +++ b/book/module2/module2.tex @@ -1,2 +1,5 @@ \chapter{Module 2} -\input{module2/debugging} +\input{module2/ai_assisted_programming} +\input{module2/debugging_code} +\input{module2/intro_to_numerical_methods} +\input{module2/version_control} diff --git a/book/module2/version_control.tex b/book/module2/version_control.tex new file mode 100644 index 0000000..3c5ac6a --- /dev/null +++ b/book/module2/version_control.tex @@ -0,0 +1,165 @@ +\section{Version Control Software}\label{version-control-software} + +\subsection{What is Version Control}\label{what-is-version-control} + +Version control is a system that tracks changes to files, enabling +developers to collaborate, manage code history, and revert to previous +versions when needed. The most used version control software (VCS) is +git. In this course git is the VCS we will be using. + +In the open-source community VCS is the - Tracks changes and history. - +Enables collaboration among developers. - Reduces errors by managing +code versions. - Supports branching and merging for parallel +development. + +In this section is divided up into two major sections. The first section +\emph{Git} will cover the basics of how to create backups of your +project. The second section will cover how to use git with GitHub to +\emph{collaborate} on projects. + +\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} + +\subsection{Git}\label{git} + +Git is a version control program that tracks changes to files and +directories using snapshots. It is a distributed version control system, +meaning that each developer has a complete copy of the repository on +their local computer. Git is the most widely used version control system +and is popular among developers for its speed, flexibility, and +efficiency. + +\subsubsection{The Three States}\label{the-three-states} + +Pay attention now --- here is the main thing to remember about Git if +you want the rest of your learning process to go smoothly. Git has three +main states that your files can reside in: modified, staged, and +committed: + +\begin{itemize} +\tightlist +\item + \textbf{Modified} means that you have changed the file but have not + committed it to your database yet. +\item + \textbf{Staged} means that you have marked a modified file in its + current version to go into your next commit snapshot. +\item + \textbf{Committed} means that the data is safely stored in your local + database. +\end{itemize} + +This leads us to the three main sections of a Git project: the working +tree, the staging area, and the Git directory. + +\includegraphics{https://git-scm.com/book/en/v2/images/areas.png} \#\#\# +Branching In git, branches allow for parallel workflow on a project. It +give contributors the ability to work different features at the same +time. Each branch represents an independent line of development, and +once a feature or fix is complete, it can be merged back into the main +branch. Here is a common branching structure used in many Git projects: +- Main Branch - The main branch (a.k.a. master branch) is the default +branch in a Git repository and contains the stable version of the code. +- Development Branch - is created to develop a new feature or fix a bug +without affecting the main branch. It isn't necessarily always stable, +but whenever it gets to a stable state, it can be merged into master. - +Topic Branch - A topic branch is a short-lived branch that you create +and use for a single particular feature or related work. +\includegraphics{https://git-scm.com/book/en/v2/images/lr-branches-2.png} +\#\#\# Best Practices - Use descriptive commit messages. - Commit early +and often. - Keep commits focused on a single change. - Use feature +branches for new features or bug fixes. - Review and test changes before +merging. - Resolve conflicts promptly. - Keep the commit history clean +and organized. + +\subsubsection{Basic Commands}\label{basic-commands} + +Here is a list of some git commands to get you started. \#\#\#\# +Starting your repository - \texttt{git\ init} - Initialize a new Git +repository. - \texttt{git\ clone} - Clone an existing repository. +\#\#\#\# Committing - \texttt{git\ status} - Check the status of the +repository. - \texttt{git\ add} - Add files to the staging area. - +\texttt{git\ commit} - Commit changes to the repository. \#\#\#\# +Branching - \texttt{git\ branch} - List, create, or delete branches. - +\texttt{git\ checkout} - Switch between branches. \#\#\#\# +History/Inspection - \texttt{git\ log} - View the commit history. +\#\#\#\# Collaborative - \texttt{git\ fetch} - Fetches updates from a +remote but does not merge. - \texttt{git\ merge} - Merge changes from a +named commit to the current branch. - \texttt{git\ pull} - Fetch and +merge changes from a remote repository. - \texttt{git\ push} - Push +changes to a remote repository. + +\subsubsection{More on git}\label{more-on-git} + +Interested in learning more about git? Here's a free book that teaches +you everything about git and how to use it at a professional level. +Available as both HTML and PDF download: +\href{https://git-scm.com/book/en/v2}{Git Pro}. + +\subsection{GitHub - The collaborative +platform}\label{github---the-collaborative-platform} + +GitHub is a web-based platform that hosts Git repositories and provides +collaboration tools for developers. It allows developers to share code, +track issues, and collaborate on projects with other developers. GitHub +is widely used for open-source projects, team collaboration, and code +hosting. \#\#\# GitHub Features - Remote Repository Hosting - GitHub +allows you to host projects and code remotely on their servers. - Issues +- Issues are used to track bugs, feature - Pull Requests - Internal +request system for contributors to request code to be pulled. \#\#\# +Workflow Depending on the size of the project and whether the project is +closed- or open-source, the workflow of the project will differ. In this +section we cover some git workflow models and the model you're going to +be using for this course. + +\textbf{Centralized}: The project has only one central hub or +\emph{repository}, can accept code and everybody synchronizes their work +with it. This model is suitable for small and closed-sourced projects. +\includegraphics{https://git-scm.com/book/en/v2/images/centralized_workflow.png} + +\textbf{Integration-Manager:} There are multiple public variants of the +code original code known as \emph{forks}. The integration manager can +decide what features to pull from the forks. This is the model that is +similar to the one used on GitHub +\includegraphics{https://git-scm.com/book/en/v2/images/integration-manager.png} + +\textbf{Dictator and Lieutenants Workflow:} This is similar to the +integration-manager model, however due to the size of the project. A +rank of integration managers is formed. one example of this is the +development of the Linux kernel. +\includegraphics{https://git-scm.com/book/en/v2/images/benevolent-dictator.png} + +GitHub is designed around a particular collaboration workflow, centered +on Pull Requests. This flow works whether you're collaborating with a +tightly-knit team in a single shared repository, or a +globally-distributed company or network of strangers contributing to a +project through dozens of forks. It is centered on the Topic Branches +workflow covered in Git Branching. + +Here's how it generally works: 1. Fork the project. 2. Create a topic +branch from master. 3. Make some commits to improve the project. 4. Push +this branch to your GitHub project. 5. Open a Pull Request on GitHub. 6. +Discuss, and optionally continue committing. 7. The project owner merges +or closes the Pull Request. 8. Sync the updated master back to your +fork. + +\subsubsection{Terms}\label{terms} + +\begin{itemize} +\tightlist +\item + Pull Request - A \emph{pull request} is a request to merge changes + from a feature branch into the main branch or from a forked repository + to the original or ``upstream'' repository. +\item + Merge - A \emph{merge} combines the changes from one branch into + another branch. +\item + Conflict - A \emph{conflict} occurs when Git cannot automatically + merge changes and requires manual intervention. +\end{itemize} + +\subsubsection{Code resource - Using GitHub to re-use existing +code.}\label{code-resource---using-github-to-re-use-existing-code.} + +In your engineering career, you will most likely use a computation +method that has been come up with before. In such scenarios, open-source diff --git a/book/module3/module3.tex b/book/module3/module3.tex new file mode 100644 index 0000000..30dfa79 --- /dev/null +++ b/book/module3/module3.tex @@ -0,0 +1,4 @@ +\chapter{Module 3} +\input{module3/non_linear_eqn_solver} +\input{module3/supersonic} +\input{module3/system_of_equations} diff --git a/book/module3/non_linear_eqn_solver.tex b/book/module3/non_linear_eqn_solver.tex new file mode 100644 index 0000000..5550d12 --- /dev/null +++ b/book/module3/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/supersonic.tex b/book/module3/supersonic.tex new file mode 100644 index 0000000..d8dfc4a --- /dev/null +++ b/book/module3/supersonic.tex @@ -0,0 +1 @@ +\section{Supersonic}\label{supersonic} diff --git a/book/module3/system_of_equations.tex b/book/module3/system_of_equations.tex new file mode 100644 index 0000000..89fa2ae --- /dev/null +++ b/book/module3/system_of_equations.tex @@ -0,0 +1 @@ +\section{System of Equations}\label{system-of-equations} diff --git a/book/module4/module4.tex b/book/module4/module4.tex new file mode 100644 index 0000000..52442db --- /dev/null +++ b/book/module4/module4.tex @@ -0,0 +1,2 @@ +\chapter{Module 4} +\input{module4/plotting} diff --git a/book/module4/plotting.tex b/book/module4/plotting.tex new file mode 100644 index 0000000..f2130e4 --- /dev/null +++ b/book/module4/plotting.tex @@ -0,0 +1,3 @@ +\section{Plotting}\label{plotting} + +\subsection{matlibplot}\label{matlibplot} diff --git a/book/module5/module5.tex b/book/module5/module5.tex new file mode 100644 index 0000000..a644df8 --- /dev/null +++ b/book/module5/module5.tex @@ -0,0 +1 @@ +\chapter{Module 5} diff --git a/book/preamble.inc b/book/preamble.inc index eecf8c1..096a0e2 100644 --- a/book/preamble.inc +++ b/book/preamble.inc @@ -2,7 +2,7 @@ \usepackage[breakable]{tcolorbox} \usepackage{parskip} % Stop auto-indenting (to mimic markdown behaviour) - + \setcounter{tocdepth}{1} % Basic figure setup, for now with no caption control since it's done % automatically by Pandoc (which extracts  syntax from Markdown). |
