9 Commits

Author SHA1 Message Date
ca774e434f Fix spelling 2023-09-10 12:51:46 +02:00
b4325ec185 Remove loose subsection 2023-09-10 12:50:18 +02:00
Janita Willumsen
5cc45d0d02 Finish algo and FLOPs 2023-09-09 17:27:04 +02:00
Janita Willumsen
31eb457614 Fixed the initial condition of forward sweep 2023-09-09 17:26:30 +02:00
Janita Willumsen
4919f488d4 Finished exercise 6 2023-09-08 22:14:52 +02:00
Janita Willumsen
1ab0a1a490 More stuff 2023-09-08 13:22:31 +02:00
Janita Willumsen
fc492b5cbf Stuff 2023-09-08 13:18:39 +02:00
Janita Willumsen
8ebd561f0d Fixed the explanation for renaming f 2023-09-08 13:15:32 +02:00
3cfee4ebdc Merge pull request #18 from FYS3150-G2-2023/coryab/implement-problem-2
Coryab/implement problem 2
2023-09-08 13:13:42 +02:00
4 changed files with 97 additions and 7 deletions

Binary file not shown.

View File

@@ -2,7 +2,7 @@
% Show that each iteration of the discretized version naturally creates a matrix equation. % Show that each iteration of the discretized version naturally creates a matrix equation.
The value of $u(x_{0})$ and $u(x_{1})$ is known, using the discretized equation we can approximate the value of $f(x_{i}) = f_{i}$. This will result in a set of equations The value of $u(x_{0})$ and $u(x_{1})$ is known, using the discretized equation we solve for $\vec{v}$. This will result in a set of equations
\begin{align*} \begin{align*}
- v_{0} + 2 v_{1} - v_{2} &= h^{2} \cdot f_{1} \\ - v_{0} + 2 v_{1} - v_{2} &= h^{2} \cdot f_{1} \\
- v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\ - v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\
@@ -10,7 +10,7 @@ The value of $u(x_{0})$ and $u(x_{1})$ is known, using the discretized equation
- v_{m-2} + 2 v_{m-1} - v_{m} &= h^{2} \cdot f_{m-1} \\ - v_{m-2} + 2 v_{m-1} - v_{m} &= h^{2} \cdot f_{m-1} \\
\end{align*} \end{align*}
Rearranging the first and last equation, moving terms of known boundary values to the RHS where $v_{i} = v(x_{i})$ and $f_{i} = f(x_{i})$. Rearranging the first and last equation, moving terms of known boundary values to the RHS
\begin{align*} \begin{align*}
2 v_{1} - v_{2} &= h^{2} \cdot f_{1} + v_{0} \\ 2 v_{1} - v_{2} &= h^{2} \cdot f_{1} + v_{0} \\
- v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\ - v_{1} + 2 v_{2} - v_{3} &= h^{2} \cdot f_{2} \\
@@ -40,5 +40,5 @@ We now have a number of linear eqations, corresponding to the number of unknown
\right. \right.
\right] \right]
\end{align*} \end{align*}
where $g_{i} = h^{2} f_{i}$. An augmented matrix can be represented as $\boldsymbol{A} \vec{x} = \vec{b}$. In this case $\boldsymbol{A}$ is the coefficient matrix with a tridiagonal signature $(-1, 2, -1)$ and dimension $n \cross n$, where $n=m-2$. Since the boundary values are equal to $0$ the RHS can be renamed $g_{i} = h^{2} f_{i}$ for all $i$. An augmented matrix can be represented as $\boldsymbol{A} \vec{x} = \vec{b}$. In this case $\boldsymbol{A}$ is the coefficient matrix with a tridiagonal signature $(-1, 2, -1)$ and dimension $n \cross n$, where $n=m-2$.

View File

@@ -1,9 +1,47 @@
\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}$
\begin{align*}
\vec{a} &= [a_{2}, a_{3}, ..., a_{n-1}, a_{n}] \\
\vec{b} &= [b_{1}, b_{2}, b_{3}, ..., b_{n-1}, b_{n}] \\
\vec{c} &= [c_{1}, c_{2}, c_{3}, ..., c_{n-1}] \\
\end{align*}
Following Thomas algorithm for gaussian elimination, we first perform a forward sweep followed by a backward sweep to obtain $\vec{v}$
\begin{algorithm}[H]
\caption{General algorithm}\label{algo:general}
\begin{algorithmic}
\Procedure{Forward sweep}{$\vec{a}$, $\vec{b}$, $\vec{c}$}
\State $n \leftarrow$ length of $\vec{b}$
\State $\vec{\hat{b}}$, $\vec{\hat{g}} \leftarrow$ vectors of length $n$.
\State $\hat{b}_{1} \leftarrow b_{1}$ \Comment{Handle first element in main diagonal outside loop}
\State $\hat{g}_{1} \leftarrow g_{1}$
\For{$i = 2, 3, ..., n$}
\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
\Return $\vec{\hat{b}}$, $\vec{\hat{g}}$
\EndProcedure
\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{algorithm}
\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$.

View File

@@ -1,3 +1,55 @@
\section*{Problem 9} \section*{Problem 9}
% Show the algorithm, then calculate FLOPs, then link to relevant files \subsection*{a)}
% Specialize algorithm
The special algorithm does not require the values of all $a_{i}$, $b_{i}$, $c_{i}$.
We find the values of $\hat{b}_{i}$ from simplifying the general case
\begin{align*}
\hat{b}_{i} &= b_{i} - \frac{a_{i} \cdot c_{i-1}}{\hat{b}_{i-1}} \\
\hat{b}_{i} &= 2 - \frac{1}{\hat{b}_{i-1}}
\end{align*}
Calculating the first values to see a pattern
\begin{align*}
\hat{b}_{1} &= 2 \\
\hat{b}_{2} &= 2 - \frac{1}{2} = \frac{3}{2} \\
\hat{b}_{3} &= 2 - \frac{1}{\frac{3}{2}} = \frac{4}{3} \\
\hat{b}_{4} &= 2 - \frac{1}{\frac{4}{3}} = \frac{5}{4} \\
\vdots & \\
\hat{b}_{i} &= \frac{i+1}{i} && \text{for $i = 1, 2, ..., n$}
\end{align*}
\begin{algorithm}[H]
\caption{Special algorithm}\label{algo:special}
\begin{algorithmic}
\Procedure{Forward sweep}{$\vec{b}$}
\State $n \leftarrow$ length of $\vec{b}$
\State $\vec{\hat{b}}$, $\vec{\hat{g}} \leftarrow$ vectors of length $n$.
\State $\hat{b}_{1} \leftarrow 2$ \Comment{Handle first element in main diagonal outside loop}
\State $\hat{g}_{1} \leftarrow g_{1}$
\For{$i = 2, 3, ..., n$}
\State $\hat{b}_{i} \leftarrow \frac{i+1}{i}$
\State $\hat{g}_{i} \leftarrow g_{i} + \frac{\hat{g}_{i-1}}{\hat{b}_{i-1}}$
\EndFor
\Return $\vec{\hat{b}}$, $\vec{\hat{g}}$
\EndProcedure
\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} + v_{i+1}}{\hat{b}_{i}}$
\EndFor
\Return $\vec{v}$
\EndProcedure
\end{algorithmic}
\end{algorithm}
\subsection*{b)}
% Find FLOPs
For every iteration of i in forward sweep we have 2 divisions, and 2 additions, resulting in $4(n-1)$ FLOPs.
For backward sweep we have 1 division, and for every iteration of i we have 1 addition, and 1 division, resulting in $2(n-1)+1$ FLOPs.
Total FLOPs for the special algorithm is $6(n-1)+1$.