\documentclass[a4paper]{article}

\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[plain]{algorithm}
\usepackage{algpseudocode}
\usepackage{enumitem}
\usepackage{framed}

\graphicspath{ {./images/} }

\usetikzlibrary{automata,positioning}

%
% Basic Document Settings
%

\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in

\linespread{1.1}

\pagestyle{fancy}
\lhead{\hmwkAuthorName}
\chead{\hmwkClass\ : \hmwkTitle}
\rhead{\firstxmark}
\lfoot{\lastxmark}
\cfoot{\thepage}

\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\setlength\parindent{0pt}

%
% Create Problem Sections
%

\newcommand{\enterProblemHeader}[1]{
    \nobreak\extramarks{}{Question \arabic{#1} continued on next page\ldots}\nobreak{}
    \nobreak\extramarks{Question \arabic{#1} (continued)}{Question \arabic{#1} continued on next page\ldots}\nobreak{}
}

\newcommand{\exitProblemHeader}[1]{
    \nobreak\extramarks{Question \arabic{#1} (continued)}{Question \arabic{#1} continued on next page\ldots}\nobreak{}
    \stepcounter{#1}
    \nobreak\extramarks{Question \arabic{#1}}{}\nobreak{}
}

\setcounter{secnumdepth}{0}
\newcounter{partCounter}
\newcounter{homeworkProblemCounter}
\setcounter{homeworkProblemCounter}{1}
\nobreak\extramarks{Question \arabic{homeworkProblemCounter}}{}\nobreak{}

\newenvironment{homeworkProblem}[1][-1]{
    \ifnum#1>0
        \setcounter{homeworkProblemCounter}{#1}
    \fi
    \section{Question \arabic{homeworkProblemCounter}}
    \setcounter{partCounter}{1}
    \enterProblemHeader{homeworkProblemCounter}
}{
    \exitProblemHeader{homeworkProblemCounter}
}

\newcommand{\hmwkClass}{INF 1004 Mathematics 2}
\newcommand{\hmwkTitle}{Tutorial\ \#9\ Solutions}
\newcommand{\hmwkAuthorName}{\textbf{Woon Jun Wei}}
\newcommand{\hmwkStudentID}{\textbf{2200624}}

\title{
    \vspace{2in}
    \textmd{\textbf{\hmwkClass \\ \hmwkTitle}}\\
    \vspace{3in}
}

\author{\hmwkAuthorName \\ \hmwkStudentID}
\date{\today}

\renewcommand{\part}[1]{\textbf{\large Part \arabic{partCounter}}\stepcounter{partCounter}\\}

%
% Various Helper Commands
%

% Useful for algorithms
\newcommand{\alg}[1]{\textsc{\bfseries \footnotesize #1}}

% For derivatives
\newcommand{\deriv}[1]{\frac{\mathrm{d}}{\mathrm{d}x} (#1)}

% For partial derivatives
\newcommand{\pderiv}[2]{\frac{\partial}{\partial #1} (#2)}

% Integral dx
\newcommand{\dx}{\mathrm{d}x}

% Alias for the Solution section header
\newcommand{\solution}{\textbf{\large My Solution\\}}
\newcommand{\answer}{\textbf{\large Sample Solutions\\}}

% Probability commands: Expectation, Variance, Covariance, Bias
\newcommand{\E}{\mathrm{E}}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\Cov}{\mathrm{Cov}}
\newcommand{\Bias}{\mathrm{Bias}}

\begin{document}

\maketitle
\pagebreak

\begin{homeworkProblem}
    Draw these affine spaces
    $$
    \begin{align*}
        \begin{bmatrix} 2\\-1 \end{bmatrix} \cdot x + 1.5 &= 0\\
        \begin{bmatrix} -5\\-1 \end{bmatrix} \cdot x + 6 &= 0\\
        \begin{bmatrix} -5\\-1 \end{bmatrix} \cdot x - 6 &= 0\\
    \end{align*}
    $$

    \solution

    \part

    Draw $x$ where $w\cdot x = 0$\\
    $\begin{pmatrix}2&-1\end{pmatrix} \cdot\begin{pmatrix}x_0&x_1\end{pmatrix} = 0\quad \text{thus } x_1=2x_0$

    Find $\frac{b}{w\cdot w}w = \frac{-1.5}{2^2+(-1)^2}\begin{pmatrix}2&1\end{pmatrix} = -0.3\begin{pmatrix}2&-1\end{pmatrix} = \begin{pmatrix}-0.6&0.3\end{pmatrix}$\\ 
    Shift the original line by $\frac{b}{w\cdot w}w$\\

    \begin{tikzpicture}
        \begin{axis}[xshift=9cm,
    xmin=-3,xmax=3,
    ymin=-3,ymax=3,
    grid=both,
    axis lines=middle,
    minor tick num=5,
    enlargelimits={abs=0.5},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
        % Draw line y = 2x + 1.5 with function
    \addplot[mark=none,thick,green] {2*x+1.5};
    \addplot[mark=none,thick,blue] {2*x};
    \addplot[mark=none,black] {-0.5 * x};
    % add an arrow at from 0,0, to (2,-1)
    \node (source) at (axis cs:0,0) {};
    \node (target) at (axis cs:2,-1) {};
    \draw[->,thick,red] (source) -- (target);
    \end{axis}
    \end{tikzpicture}

    Verify $\begin{pmatrix}2,-1\end{pmatrix} \cdot \begin{pmatrix}x_0,x_1\end{pmatrix} + 1.5$ is drawn\\
    $2 x_0-x_1 + 1.5  = 0$ or $x_1 = 2x_0 + 1.5$\\
    Looks like $y = 2x + 1.5$\\

    \pagebreak
    \part

    Draw $x$ where $w\cdot x = 0$\\
    $\begin{pmatrix}-5&-1\end{pmatrix} \cdot\begin{pmatrix}x_0&x_1\end{pmatrix} = 0\quad \text{thus } x_1=-5x_0$\\

    Find $\frac{b}{w\cdot w}w = \frac{-6}{(-5)^2+(-1)^2}\begin{pmatrix}-5&-1\end{pmatrix} = -\frac{6}{26}\begin{pmatrix}-5&-1\end{pmatrix} = \begin{pmatrix}\frac{15}{13} & \frac{3}{13}\end{pmatrix}$\\

    Shift the original line by $\frac{b}{w\cdot w}w$\\
    \begin{tikzpicture}
        \begin{axis}[xshift=9cm,
    xmin=-6,xmax=4,
    ymin=-2,ymax=6,
    grid=both,
    axis lines=middle,
    minor tick num=5,
    enlargelimits={abs=0.5},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

    \addplot[mark=none,thick,red] {-5 * x + 6};
    \addplot[mark=none,thick,blue] {-5 * x};
    \addplot[mark=none,black] {0.2 * x};
    % add an arrow at from 0,0, to (-5,-1)
    \node (source) at (axis cs:0,0) {};
    \node (target) at (axis cs:-5,-1) {};
    \draw[->,thick,red] (source) -- (target);
        \end{axis}
    \end{tikzpicture}

    Verify $\begin{pmatrix}-5,-1\end{pmatrix} \cdot \begin{pmatrix}x_0,x_1\end{pmatrix} + 6$ is drawn\\
    $-5 x_0-x_1 + 6  = 0$ or $x_1 = -5x_0 + 6$\\
    Looks like $y = -5x + 6$\\


        \part

        Draw $x$ where $w\cdot x = 0$\\
        $\begin{pmatrix}-5&-1\end{pmatrix} \cdot\begin{pmatrix}x_0&x_1\end{pmatrix} = 0\quad \text{thus } x_1=-5x_0$\\
            
        Find $\frac{b}{w\cdot w}w = \frac{6}{(-5)^2+(-1)^2}\begin{pmatrix}-5&-1\end{pmatrix} = \frac{6}{26}\begin{pmatrix}-5&-1\end{pmatrix} = \begin{pmatrix}-\frac{15}{13} & -\frac{3}{13}\end{pmatrix}$\\

    Shift the original line by $\frac{b}{w\cdot w}w$\\
    \begin{tikzpicture}
        \begin{axis}[xshift=9cm,
    xmin=-6,xmax=2,
    ymin=-6,ymax=2,
    grid=both,
    axis lines=middle,
    minor tick num=5,
    enlargelimits={abs=0.5},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

    \addplot[mark=none,thick,red] {-5 * x - 6};
    \addplot[mark=none,thick,blue] {-5 * x};
    \addplot[mark=none,black] {0.2 * x};
    % add an arrow at from 0,0, to (-5,-1)
    \node (source) at (axis cs:0,0) {};
    \node (target) at (axis cs:-5,-1) {};
    \draw[->,thick,red] (source) -- (target);
        \end{axis}
    \end{tikzpicture}
    
    Verify $\begin{pmatrix}-5,-1\end{pmatrix} \cdot \begin{pmatrix}x_0,x_1\end{pmatrix} - 6$ is drawn\\
    $-5 x_0-x_1 - 6  = 0$ or $x_1 = -5x_0 - 6$\\
    Looks like $y = -5x - 6$\\
\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}

    Find two non-parallel vectors $x$ solving
    $$
    \begin{align*}
        &w \cdot x= 3\\
        w &= \begin{bmatrix} 1\\-2\\4 \end{bmatrix}
    \end{align*}
    $$
    
    \solution

    $$
    \begin{align*}
        w\cdot x = 3 &\implies \begin{bmatrix} 1\\-2\\4 \end{bmatrix} \cdot \begin{bmatrix}x_0\\x_1\\x_2\end{bmatrix} = 3\\
                     &\implies x_0 + 2x_1 + 4x_2 = 3\\
                     &\implies x_0 =3+2x_1+4x_2 (=3+2s-4t)\\
                     &\implies x_1 = x_1 (=s)\\
                     &\implies x_2 = x_2 (=t)\\
    \end{align*}
    $$

    $$\begin{bmatrix}x_0\\x_1\\x_2\end{bmatrix} = \begin{bmatrix}3\\0\\0\end{bmatrix} + s\begin{bmatrix}2\\1\\0\end{bmatrix} - t\begin{bmatrix}-4\\0\\1\end{bmatrix}$$


    $$
    \begin{align*}
        s = 1, t = 0 &: \begin{bmatrix}x_0\\x_1\\x_2\end{bmatrix} = \begin{bmatrix}3\\0\\0\end{bmatrix} + \begin{bmatrix}2\\1\\0\end{bmatrix} = \begin{bmatrix}5\\1\\0\end{bmatrix}\\
        s = 0, t = 1 &: \begin{bmatrix}x_0\\x_1\\x_2\end{bmatrix} = \begin{bmatrix}3\\0\\0\end{bmatrix} - \begin{bmatrix}4\\0\\1\end{bmatrix} = \begin{bmatrix}-1\\0\\-1\end{bmatrix}\\
    \end{align*}
    $$
    They are clearly linear independent. There is no $c$ such that
    $$
    \begin{bmatrix}5\\1\\0\end{bmatrix} = c\begin{bmatrix}-1\\0\\-1\end{bmatrix}
    $$
    
    $$
    \boxed{
        \text{Answer: vectors are: } \begin{bmatrix}5\\1\\0\end{bmatrix} \text{ and } \begin{bmatrix}-1\\0\\-1\end{bmatrix}
    }
    $$

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}

    Show that the line given by
    $$
    \begin{align*}
        f(t) &= \begin{bmatrix} 2\\3\\1 \end{bmatrix} + t \begin{bmatrix} -1\\4\\2 \end{bmatrix}
    \end{align*}
    $$
    does not intersect the plane given by
    $$
    2x+z=9
    $$
    Note:
    $$
    f(t) = \begin{bmatrix} x(t)\\y(t)\\z(t) \end{bmatrix}
    $$

    \answer

    \begin{center}
        \fbox{\begin{minipage}{40em}
    Steps:
    \begin{itemize}
        \item Plug in $f(t)$ into your equation (system).
        \item Write the resulting equations as an affine system in $t: at = b$.
        \item Solve it for $t$.
    \end{itemize}
    \end{minipage}}
    \end{center}

    $2x+z=9$ take the $x$ and $z$ componenets
    $$
    \begin{align*}
        f(t) &= \begin{bmatrix} 2 + t(-1)\\3 + t(4)\\1 + t(2) \end{bmatrix}\\
        x(t) &=2+t(-1)\\
        z(t) &=1+2t\\
        \implies 2x+z &= 2(2-t)+(1+2t) = 4-2t+1+2t = 5\\
    \end{align*}
    $$
    Cannot satisfy $5=9$ for any $t$. It is an affine equation.

    Note: for the equation $2x+z = 5$ any $t$ solves.\\
    Therefore: The line lies inside the plane given by $2x+z=5$.
    \begin{center}
    \fbox{\begin{minipage}{25em}
            Answer: Showed that the line does not intersect plane.
    \end{minipage}}
    \end{center}

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}

    Show that the line given by
    $$
    \begin{align*}
        f(t) &= \begin{bmatrix} 1\\-3\\2 \end{bmatrix} + t \begin{bmatrix} 2\\3\\-5 \end{bmatrix}
    \end{align*}
    $$
    does not intersect the plane given by
    $$
    3x-2y+2z = 18
    $$
    Note:
    $$
    f(t) = \begin{bmatrix} x(t)\\y(t)\\z(t) \end{bmatrix}
    $$

    \answer

    $$
    \begin{align*}
        3x-2y+2z &= 18 \text{ take the x, y and z components}\\
        f(t) &= \begin{bmatrix} x(t)\\y(t)\\z(t) \end{bmatrix}\\
        x(t) &= 1+2t\\
        y(t) &= -3+3t\\
        z(t) &= 2-5t\\
        \implies 3x-2y+2z &= 3(1+2t)-2(-3+3t)+2(2-5t) = 3+6t+6-6t+4-10t = 13-10t =18\\
    \end{align*}
    $$
    Solve $13-10t = 18$ for $t$. It is an affine equation.\\
    $-10t=5$\\
    $t = -0.5$

    $$
    \implies t = -0.5 \implies \text{ plug it in }f(-0.5) = \begin{bmatrix} 1\\-3\\2\end{bmatrix} - 0.5 \begin{bmatrix} 2\\3\\-5\end{bmatrix} = \begin{bmatrix} 0\\-4.5\\4.5\end{bmatrix}
    $$

    Verify: $3x-2y+2z = 3\cdot 0-2\cdot(-4.5)+2\cdot4.5 = 18$

    \begin{center}
    \fbox{\begin{minipage}{25em}
            Answer: Showed that the line intersects plane at $t=-0.5$.
    \end{minipage}}
    \end{center}
\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
    Check whether the plane given by
    $$
    f(s,t) = \begin{bmatrix} 0\\1\\-3 \end{bmatrix} + t \begin{bmatrix} -2\\0\\1 \end{bmatrix} + s \begin{bmatrix} 1\\2\\-1 \end{bmatrix}
    $$
    has an intersection with the line given by
    $$
    \begin{align*}
        x+2y-z &= 3\\
        2x-y+z &= 6
    \end{align*}
    $$
    Note:
    $$
    f(s,t) = \begin{bmatrix} x(s,t)\\y(s,t)\\z(s,t) \end{bmatrix}
    $$

    \answer

    \begin{center}
        \fbox{\begin{minipage}{40em}
    Steps:
    \begin{itemize}
        \item Plug in $f(s,t)$ into your equation (system).
        \item Write the resulting equations as an affine system in vector $\begin{bmatrix} s\\t \end{bmatrix}: A\begin{bmatrix} s\\t \end{bmatrix} = b$.
        \item Solve it for $t$.
    \end{itemize}
    \end{minipage}}
    \end{center}

    $$
    \begin{align*}
        f(s,t) &= \begin{bmatrix} x(t)\\y(t)\\z(t) \end{bmatrix}\\
        x(s,t) &= 0+t(-2)+s(1)\\
        y(s,t) &= 1+t(0)+s(2)\\
        z(s,t) &= -3+t(1)+s(-1)\\
    \end{align*}
    $$

    $$
    \begin{align*}
        x+2y-z &= -2t+s+2(1+2s)-(-3+t-s)=5-2t-t+s+4s+s=5+6s-3t\\
        2x-y+z &= 2(-2t+s)-(1+2s)+-3+t-s = -4-4t+t+2s-2s-s=-4-s-3t\\
        5+6s - 3t &=3\\
        -4-s-3t &=6\\
    \end{align*}
    $$

    $$
    \left [
    \begin{array}{rr|r}
        6&-3&-2\\
        -1&-3&10
    \end{array}
    \right ] \rightarrow
    \left [
    \begin{array}{rr|r}
        1&3&-10\\
        6&-3&-2
    \end{array}
    \right ] \rightarrow
    \left [
    \begin{array}{rr|r}
        1&3&-10\\
        0 & -21 & 58
    \end{array}
    \right ] \rightarrow
    \left [
    \begin{array}{rr|r}
        1&3&-10\\
        0 & 1 & -58/21
    \end{array}
    \right ] \rightarrow
    \left [
    \begin{array}{rr|r}
        1&0&-\frac{12}{7}\\
        0 & 1 & -\frac{58}{21}
    \end{array}
    \right ] 

    $$

    \begin{center}
    \fbox{\begin{minipage}{25em}
            {\large Answer: Intersects where $s = -\frac{12}{7}$ and $t = -\frac{58}{21}$.}
    \end{minipage}}
    \end{center}

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
    Convert the plane equation into the form $Ax=b$ for
    $$
    \begin{bmatrix}x_0\\y_0\\z_0 \end{bmatrix} = \begin{bmatrix} 5\\-2\\-3 \end{bmatrix}
    + s \begin{bmatrix} 1\\2\\3 \end{bmatrix} + t \begin{bmatrix} 2\\-1\\2 \end{bmatrix}
    $$\\
    Steps:
    \begin{itemize}
        \item What is the dimensionality of the whole vector space in which these equations are defined?
        \item What is the dimensionality of the affine space spanned by the plane equation?
        \item How does the matrix B look like for which we seek solutions $x$ such that $Bx = 0$ ?
        \item Conclude based on the dimensionality of the whole vector space and the dimensionality of the plane, what is the dimensionality of solutions $x$ which we are searching for ?
        \item Find a basis for these solutions. Turn it into a matrix $A$
        \item Get the correct bias vector $b$ based the $A$ which you found
    \end{itemize}

    \part
    
    The whole space is 3 dim

    \part 
    
    $v_0 = \begin{bmatrix}1\\2\\3\end{bmatrix}$ and $v_1 = \begin{bmatrix}2\\-1\\2\end{bmatrix}$ \\
    are linearly independent, therefore they span a 2-dim space (k=2)

    \pagebreak

    \part

    Need $v_0 \cdot w = 0, v_1 \cdot w = 0$ \\
    $\begin{pmatrix}1&2&3\end{pmatrix} \cdot w = 0$ \\
    $\begin{pmatrix}2&-1&2\end{pmatrix} \cdot w = 0$ \\
    $$
    \begin{align*}
        x+2y+3z = 0\\
        2x+-y+2z = 0
    \end{align*}
    $$
    $$
    \begin{bmatrix}1&2&3\\2&-1&2\end{bmatrix} \cdot \begin{bmatrix}x\\y\\z\end{bmatrix} = \begin{bmatrix}0\\0\end{bmatrix}\\
    $$
    $$
    \left[
        \begin{array}{rrr|r}
            1&2&3&0\\
            2&-1&2&0
        \end{array}
    \right] \rightarrow
    \left[
        \begin{array}{rrr|r}
            1&0&\frac{7}{5}&0\\
            0&1&\frac{4}{5}&0
        \end{array}
    \right] 

    $$
    $$
    \begin{align*}
        x + \frac{7}{5}z &= 0\\
        y + \frac{4}{5}z &= 0
    \end{align*}
    $$

    $$
    \begin{bmatrix}x\\y\\z\end{bmatrix} = z\begin{bmatrix}-\frac{7}{5}\\-\frac{4}{5}\\1\end{bmatrix}
    $$

    \part

    We need to find one orthogonal vector $w = \begin{bmatrix}x\\y\\z\end{bmatrix}$ which is orthorgonal because then we have 3 vectors. 3 vectors span a 3-dim space. ($e-k=1$)

    \part

    Let $z=1$:

    $$
    w = \begin{bmatrix}x\\y\\z\end{bmatrix} = \begin{bmatrix}-\frac{7}{5}\\-\frac{4}{5}\\1\end{bmatrix}
    $$
    $Ax =b$ \\
    To find A and B 

    $$
    A = \begin{bmatrix}-\frac{7}{5}&-\frac{4}{5}&1\end{bmatrix}
        $$
        \part

        $$
            b = A\begin{bmatrix}5\\-2\\-3\end{bmatrix} = \begin{bmatrix}-\frac{7}{5} & -\frac{4}{5} & 1\end{bmatrix} \begin{bmatrix}5\\-2\\-3\end{bmatrix} = -8.4
        $$      \part

    $$
        b = A\begin{bmatrix}5\\-2\\-3\end{bmatrix} = \begin{bmatrix}-\frac{7}{5} & -\frac{4}{5} & 1\end{bmatrix} \begin{bmatrix}5\\-2\\-3\end{bmatrix} = -8.4
    $$  

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
    Convert the plane equation into the form $Ax=b$ for
    $$
    \begin{bmatrix}x_0\\y_0\\z_0 \end{bmatrix} = \begin{bmatrix} -1\\0\\1 \end{bmatrix}
    + s \begin{bmatrix} -3\\1\\6 \end{bmatrix} + t \begin{bmatrix} 2\\-4\\-4 \end{bmatrix}
    $$\\

    \answer

    We have a 2 dimensinal affine space, spanned by:
    $$
    \begin{bmatrix}-3\\1\\6\end{bmatrix} \quad \begin{bmatrix}2\\-4\\-4\end{bmatrix}
    $$

    There can be in 3 dimensionsonly one orthogonal vector $w$ which is orthogonal to both these vectors

    $
    \begin{bmatrix}-3&1&6\\2&-4&-4\end{bmatrix} \cdot w = 0
    $\\
    $
    \begin{bmatrix}-3&1&6\\2&-4&-4\end{bmatrix} \rightarrow
    \begin{bmatrix}1&0&-2\\0&1&0\end{bmatrix}
    $\\

    $ y = 0$\\
    $x-2z = 0$

    $$w = \begin{bmatrix}x\\y\\z\end{bmatrix} = z\begin{bmatrix}2\\0\\-1\end{bmatrix}$$

    $w = \begin{bmatrix}x\\y\\z\end{bmatrix} \text{ and thus } A = \begin{bmatrix}2&0&-1\end{bmatrix}$\\
    $ b= \begin{bmatrix}2&0&-1\end{bmatrix} \begin{bmatrix}-1\\0\\1\end{bmatrix} = -1$

\end{homeworkProblem}

\pagebreak

\begin{homeworkProblem}
    Plot 2d plandes in a 3d space using e.g. matplotlib

    \solution

\end{homeworkProblem}

\pagebreak

\end{document}
