Merge pull request #19 from FYS3150-G2-2023/6-solve-problem-6

Finished exercise 6
This commit is contained in:
Cory Balaton 2023-09-10 12:56:54 +02:00 committed by GitHub Enterprise
commit 02302e1255

View File

@ -1,7 +1,6 @@
\section*{Problem 6} \section*{Problem 6}
\subsection*{a)} \subsection*{a)}
% Use Gaussian elimination, and then use backwards substitution to solve the equation % Use Gaussian elimination, and then use backwards substitution to solve the equation
Renaming the sub-, main-, and supdiagonal of matrix $\boldsymbol{A}$ Renaming the sub-, main-, and supdiagonal of matrix $\boldsymbol{A}$
\begin{align*} \begin{align*}
@ -10,40 +9,38 @@ Renaming the sub-, main-, and supdiagonal of matrix $\boldsymbol{A}$
\vec{c} &= [c_{1}, c_{2}, c_{3}, ..., c_{n-1}] \\ \vec{c} &= [c_{1}, c_{2}, c_{3}, ..., c_{n-1}] \\
\end{align*} \end{align*}
Following Thomas algorithm for gaussian elimination, we first perform a forward sweep Following Thomas algorithm for gaussian elimination, we first perform a forward sweep followed by a backward sweep to obtain $\vec{v}$
\begin{algorithm}[H] \begin{algorithm}[H]
\caption{Foreward sweep}\label{algo:foreward} \caption{General algorithm}\label{algo:general}
\begin{algorithmic} \begin{algorithmic}
\State Create new vector \vec{\hat{b}} of length n. \Procedure{Forward sweep}{$\vec{a}$, $\vec{b}$, $\vec{c}$}
\State \hat{b}[0] &= b[0] \Comment{Handle first element in main diagonal outside loop} \State $n \leftarrow$ length of $\vec{b}$
\For{$i = 1, ..., n-1$} \State $\vec{\hat{b}}$, $\vec{\hat{g}} \leftarrow$ vectors of length $n$.
\State d = \frac{a[i-1]}{b[i-1]} \State $\hat{b}_{1} \leftarrow b_{1}$ \Comment{Handle first element in main diagonal outside loop}
\State b -= d*(*sup_diag)(i-1); \For{$i = 2, 3, ..., n$}
(*g_vec)(i) -= d*(*g_vec)(i-1); \State $d \leftarrow \frac{a_{i}}{\hat{b}_{i-1}}$ \Comment{Calculating common expression}
\State $\hat{b}_{i} \leftarrow b_{i} - d \cdot c_{i-1}$
\State $\hat{g}_{i} \leftarrow g_{i} - d \cdot \hat{g}_{i-1}$
\EndFor \EndFor
\While{Some condition} \Return $\vec{\hat{b}}$, $\vec{\hat{g}}$
\State Do something more here \EndProcedure
\EndWhile
\State Maybe even some more math here, e.g $\int_0^1 f(x) \dd x$ \Procedure{Backward sweep}{$\vec{\hat{b}}$, $\vec{\hat{g}}$}
\State $n \leftarrow$ length of $\vec{\hat{b}}$
\State $\vec{v} \leftarrow$ vector of length $n$.
\State $v_{n} \leftarrow \frac{\hat{g}_{n}}{\hat{b}_{n}}$
\For{$i = n-1, n-2, ..., 1$}
\State $v_{i} \leftarrow \frac{\hat{g}_{i} - c_{i} \cdot v_{i+1}}{\hat{b}_{i}}$
\EndFor
\Return $\vec{v}$
\EndProcedure
\end{algorithmic} \end{algorithmic}
\end{algorithm} \end{algorithm}
Here the index i does not refer to the element in the vector, ie. $b_{i}$, but to the index in the vector where the element is found ie. $b[i] = b_{i+1}$.
\begin{align*}
\hat{b}_{1} &= b_{1} \\
\hat{b}_{2} &= b_{2} - \frac{a_{2}}{\hat{b}_{1}} \cdot c_{1} \\
\vdots & \\
\hat{b}_{i} &= b_{i} - \frac{a_{i}}{\hat{b}_{i-1}} \cdot c_{i-1} \\
\vdots & \\
\end{align*}
% as g_hat also make use of the ratio (ai/bi-1_hat) it only needs to be calculated once
% for n steps we have n+1 points, that is values of x to evaluate u(x) at, when boundary values are known
% we have to perform (n+1)-2 = n-1 calculations (equal number of col in matrix). Dealing
% with a quadratic matrix number of col = number of rows
\subsection*{b)} \subsection*{b)}
% Figure out FLOPs
% Figure it out Counting the number of FLOPs for the general algorithm by looking at one procedure at a time.
For every iteration of i in forward sweep we have 1 division, 2 multiplications, and 2 subtractions, resulting in $5(n-1)$ FLOPs.
For backward sweep we have 1 division, and for every iteration of i we have 1 subtraction, 1 multiplication, and 1 division, resulting in $3(n-1)+1$ FLOPs.
Total FLOPs for the general algorithm is $8(n-1)+1$.