\section{Equations of Order One}

\subsection{Separation of Variables}

We begin with equations of the form

\begin{equation}
    Mdx+Ndy=0
\end{equation}
where $M$ and $N$ can be multivariate of $x,y$.

It is separable iff
\begin{equation}
    A(x)dx+B(y)dy=0.
\end{equation}

Then find a function $F$ with total differential being the LHS of above, so $F=c$.

\subsection{Homogeneity}

\begin{definition}[Homogeneity of polynomials]
    Polynomials where all terms are of the same degree are homogeneous.
\end{definition}

Homoegeneity of functions is analogous to assigning physical dimensions (e.g. length) to all of the variables.
If the function has the length dimension to the $k$th power, then it is homogeneous of degree $k$.

\begin{example}
    If $x,y$ are lengths, then the following is homogeneous of degree 3.
    \begin{equation}
        f(x,y)=2y^3\exp(\frac{y}{x})-\frac{x^4}{x+3y}
    \end{equation}
\end{example}

Alternate definition also suffices for generality.

\begin{definition}[Homogeneous function]
    $f(x,y)$ is homogeneous of degree $k$ iff $f(\lambda x, \lambda y)=\lambda^kf(x,y)$.
\end{definition}

\begin{definition}[Alternate definition of homogeneity]
    If $f(x,y)$ can be rewritten as $f(\frac{y}{x})$ or $f(\frac{x}{y})$ then it is homogeneous.
\end{definition}

\subsection{Homogeneous Differential Equations}

\begin{theorem}[Homogeneous DEs]
    If $M(x,y)$ and $N(x,y)$ are homogeneous and of same degree, then $M(x,y)dx+N(x,y)dy=0$
    is a homogeneous DE.
\end{theorem}

\begin{theorem}[Homogeneous DEs]
    $M(x,y)/N(x,y)$ is homogeneous of degree 0.

    \begin{proof}
        If $M,N$ are homogeneous of some degree $n$, then
        \begin{eqnarray}
            M(x,y)=M(\lambda x,\lambda y)=\lambda ^nM(x,y)\\
            M(x,y)=N(\lambda x,\lambda y)=\lambda ^nN(x,y)
        \end{eqnarray}
        So for $M/N$,
        \begin{eqnarray}
            \frac{M(x,y)}{N(x,y)}=\lambda^0\frac{M(1,v)}{N(1,v)}
        \end{eqnarray}
        which is homogeneous of degree 0.
    \end{proof}
\end{theorem}

\begin{theorem}[Homogeneous DEs]
    If $f(x,y)$ is homogeneous of degree 0 in $x,y$, then $f(x,y)$ is a function of $y/x$ alone.
    \begin{proof}
        Let $y=vx$. Then, $f$ must be proven to be of $v$ alone. Substitute so that
        \begin{equation}
            f(x,y)=f(x,vx)=x^0f(1,v)=f(1,v)
        \end{equation}
    \end{proof}
\end{theorem}

\subsection{Homogeneous Coefficients}

Suppose coefficients $M,N$ in the equation

\begin{equation}
    M(x,y)dx+N(x,y)dy=0
\end{equation}

are both homogeneous of same degree. So $M/N$ is a function of $y/x$ alone.
Can put in form using $y=vx$.
\begin{eqnarray}
    \frac{dy}{dx}+g(\frac{y}{x})=0\\
    x\frac{dv}{dx}+g(v)=0
\end{eqnarray}

This last equation is separable.

\subsection{Exact Equations}

If there exists an equation of the form $A(x)dx+B(y)dy=0$, the solution is a function with differential $A(x)dx+B(y)dy$.
Idea works for equations of form

\begin{equation}
    dF=Mdx+Ndy.
\end{equation}

So, $F(x,y)=c\implies dF=0$ and

\begin{equation}
    Mdx+Ndy=0.
\end{equation}

If there's a function $F$ such that $Mdx+Ndy$ is the \textbf{total differential}
of $F$, then Eq. 5 is an \textit{exact equation} by definition. Can rewrite the total differential from the chain rule:

\begin{equation}
    dF=\frac{\partial F}{\partial x}dx+\frac{\partial F}{\partial y}dy.
\end{equation}

So $M=\frac{\partial F}{\partial x},N=\frac{\partial F}{\partial y}$. We can take 2nd derivative to show these are equal because the partials are continuous (Clairaut's theorem).

\begin{eqnarray}
    \frac{\partial M}{\partial y}=\frac{\partial^2 F}{\partial y\partial x}\\
    \frac{\partial N}{\partial x}=\frac{\partial^2 F}{\partial y\partial x}.
\end{eqnarray}

\begin{definition}[Exactness]
    \begin{equation}
        \frac{\partial M}{\partial y}=\frac{\partial N}{\partial x}.
    \end{equation}
\end{definition}

\begin{proof}
    Let $\phi(x,y)$ be a function where $\frac{\partial \phi}{\partial x}=M$. $\phi$ is the function you get from integrating $Mdx$ wrt $x$ and holding $y$. Then

    \begin{equation}
        \frac{\partial^2\phi}{\partial y\partial x}=\frac{\partial M}{\partial y}=\frac{\partial N}{\partial x}
    \end{equation}

    Integrating both sides wrt $x$:

    \begin{equation}
        \frac{\partial \phi}{\partial x}=N+B'(y)
    \end{equation}

    where $B'(y)$ is the integration constant. Let

    \begin{equation}
        F=\phi(x,y)-B(y)
    \end{equation}

    such that

    \begin{align}
        dF&=\frac{\partial \phi}{\partial x}dx+\frac{\partial \phi}{\partial y}dy-B'(y)dy\\
        &=Mdx+\left[N+B'(y)\right]dy-B'(y)dy\\
        &=Mdx+Ndy
    \end{align}
\end{proof}

\begin{example} We have the DE
    \begin{equation}
        3x(xy-2)dx+(x^3+2y)dy=0.
    \end{equation}

    Then,

    \begin{equation}
        \frac{\partial M}{\partial y}=3x^2,\frac{\partial N}{\partial y}=3x^2
    \end{equation}

    The DE is exact, and $F=c$ is the solution.

    \begin{eqnarray}
        \frac{\partial F}{\partial x}=M=3x^2y-6x\\
        \frac{\partial F}{\partial y}=N=x^3+2y
    \end{eqnarray}

    Try to find $F$ from 18, integrate both sides wrt $x$ with an integration constant $T(y)$.

    \begin{equation}
        F=x^3y-3x^2+T(y)
    \end{equation}

    Using Eq. 19, can can find $\frac{\partial F}{\partial y}$ from Eq. 20 and equate:

    \begin{equation}
        x^3+T'(y)=x^3+2y\implies T'(y)=2y
    \end{equation}

    Because $F=c$ is the I.C., can conclude

    \begin{equation}
        T(y)=y^2
    \end{equation}

    Thus,

    \begin{equation}
        F=x^3y-3x^2+y^2\Leftrightarrow x^3y-3x^2+y^2=c
    \end{equation}
\end{example}

\subsection{Linear Equations of Order 1}

If an equation is not exact, can attempt to do so by multiplying DE by an integrating factor.

\begin{definition}[Linear DE of order 1]
    \begin{equation}
        A(x)\frac{dy}{dx}+B(x)y=C(x)
    \end{equation}
\end{definition}

Divide each side by $A(x)$ to obtain

\begin{equation}
    \frac{dy}{dx}+P(x)y=Q(x)
\end{equation}

Suppose there exists for Eq. 25 a I.F. $v(x)>0$. Then,

\begin{equation}
    v(x)\left[\frac{dy}{dx}+P(x)y\right]=v(x)Q(x)
\end{equation}

becomes exact, or of form $Mdx+Ndy=0$. Here,

\begin{eqnarray}
    M=vPy-vQ\\
    N=v
\end{eqnarray}

Because the requirement is $\frac{\partial M}{\partial y}=\frac{\partial N}{\partial x}$,

\begin{align}
    vP&=\frac{dv}{dx}\\
    Pdx&=\frac{dv}{v}\\
    \ln v&=\int Pdx\\
    v&=\exp(\int Pdx)
\end{align}

We can then multiply both sides of the DE by this I.F. One side of this eqn will be of the product rule form, the derivative of $y\exp (\int Pdx)$:

\begin{equation}
    \exp(\int Pdx)\frac{dy}{dx}+P\exp(\int Pdx)y=Q\exp(\int Pdx)
\end{equation}

\subsection{General Solution of a Linear Equation}

Given the original form

\begin{equation}
    \frac{dy}{dx}+P(x)y=Q(x)
\end{equation}

suppose $P$ and $Q$ are continuous on $x\in (a,b)$ and $x=x_0$ is such a number. $y=y_0$ satisfies the initial condition.
This sol'n satisfies Eq. 34 for all $x$ in the interval. Multiplying Eq. 34 by integrating factor $\exp(\int Pdx)$ gives

\begin{equation}
    yv=\int vQ\:dx + c
\end{equation}

Because $v\neq 0$,

\begin{equation}
    y=v^{-1}\int vQ\: dx + cv^{-1}
\end{equation}

Given any $x_0,y_0$ in the interval, can find $c$ s.t. the DE is satisfied.
Every eqn of above form will have $P,Q$ with common interval of continuity and a unique set of solutions with one I.C. obtained by using the integrating factor.
These solutions are unique, so any other method yields a solution that aligns with the general solution–all possible solutions satisfying the DE on $x\in (a,b)$.

\subsection{Application of Mixing Problem}

Strategy is to determine the differential equation describing rate of change of a certain quantity, then finding the particular solution with some trivial IC.

\begin{example}
    100 liter tank contains 10 kg salt mixed with 60 liter water. Sol'n with concentration 0.1 $\frac{\mathrm{kg}}{\mathrm{liter}}$ flows in at rate 5 liters/min.
    Solution is well stirred (asume equal distribution), outflow rate of 3 liters/min. Need to find salt in tank when it is full.

    Note that the tank will become full, as $\mathrm{in}-\mathrm{out}>0$. Let $x$ be kg of salt.
    Then, inflow rate is $0.1 \frac{\mathrm{kg}}{\mathrm{liter}}\cdot 5\frac{\mathrm{liter}}{\mathrm{min}}=0.5\frac{\mathrm{kg}}{\mathrm{min}}$. Out is
    $x \frac{\mathrm{kg}}{60\;\mathrm{liter}}\cdot 3\frac{\mathrm{liter}}{\mathrm{min}}=\frac{x}{20}\frac{\mathrm{kg}}{\mathrm{min}}$. We then express the DE as

    \begin{equation}
        \frac{dx}{dt}=0.5-\frac{x}{20}
    \end{equation}

    Then just express in linear form, solve with I.F. method.
\end{example}

\begin{example}
    Initially 50 gallons of brine, 10 lb dissolved salt. Inflow of 2 lb salt/gal at 5 gal/min, outflow of 3 gal/min, but \textbf{mixture kept uniform}.

    Inflow is thus 10 lb/min, outflow is $\frac{3x}{50+2t}$ lb/min. Key here is that mixture concentration on outflow does not change, so the volume dynamically adapts for changing amount of salt.
    50 gallons initially, influx of 5 gal - 3 gal out $\implies 50+(5-3)t$.
    DE is thus

    \begin{equation}
        \frac{dx}{dt}=10-\frac{3x}{50+2t}
    \end{equation}
\end{example}

\subsection{Integrating Factor by Inspection}

By recognizing differentials in a problem, can find the integrating factor by inspection.

\begin{example}
    Given
    \begin{equation}
        ydx+(x+x^3y^2)dy=0
    \end{equation}
    the terms can be grouped by like degree so
    \begin{equation}
        (ydx+xdy)+x^3y^2dy=0.
    \end{equation}
    Can be rewritten as
    \begin{equation}
        d(xy)+x^3y^2dy=0
    \end{equation}
    then divide by $(xy)^3$ for it does not affect integrability of $d(xy)$ term but keeps function of $y$ with $dy$ term,
    \begin{equation}
        \frac{d(xy)}{(xy)^3}+\frac{dy}{y}=0.
    \end{equation}
    Integrating:
    \begin{align}
        \int (xy)^{-3}d(xy)+\int \frac{dy}{y}&=0\\
        \frac{(xy)^{-2}}{-2}+\ln|y|=C\\
        Cy=\frac{(xy)^{-2}}{2}\\
        Cy(xy)^2=1
    \end{align}
\end{example}

\subsection{Determining Complex Integrating Factors}

Let there be the DE

\begin{equation}
    Mdx+Ndy=0.
\end{equation}

Suppose $\exists u$, possibly of both $x,y$ that is an integrating factor such that

\begin{equation}
    uMdx+uNdy=0
\end{equation}

and for it to be exact,

\begin{equation}
    \frac{\partial}{\partial y}(uM)=\frac{\partial}{\partial x}(uN)
\end{equation}

so $u$ satisfies

\begin{eqnarray}
    u\frac{\pr M}{\pr y}+M\frac{\pr u}{\pr y}=u\frac{\partial N}{\partial x}+N\frac{\pr u}{\pr x}\\
    u(\frac{\pr M}{\pr y}-\frac{\pr N}{\pr x})=N\frac{\pr u}{\pr x}-M\frac{\pr u}{\pr y}.
\end{eqnarray}

This does not lead anywhere, so let $u$ be a function of $x$. Thus, $\pr u/\pr y=0,\pr u/\pr x=du/dx$.
So the above reduces to

\begin{equation}
    u(\frac{\pr M}{\pr y}-\frac{\pr N}{\pr x})=N\frac{du}{dx}\Leftrightarrow \frac{1}{N}(\frac{\pr M}{\pr y}-\frac{\pr N}{\pr x})dx=\frac{du}{u}
\end{equation}

and integrating factor is the following, assuming LHS of above is a function of $x$ or $y$ alone
\begin{eqnarray}
    u=\exp\left[\int f(x)dx\right]\;(\text{for $x$})\\
    u=\exp\left[\int -g(y)dy\right]\;(\text{for $y$})
\end{eqnarray}


\subsection{Introduction}

bart causal is different to causal tree

In stuff now two problems:
+ non random but constant effect
+ Random but heterogenous effect

causal trees can find heterogenous treatment effects

Approaches:
We have treated and untreated. X and y
Estimate y|x for treated, and untreated separately.
Then take differnece for a given x to be the estimated treatment effect

2nd approach:
have treatment as input
diffence is again y|x - y|x treatment minus no treatment

3rd approach: (type of single tree)
split not by predictive power, but by treatment effect differnece

4th approach:
cross validation
at each leaft we note the sample average treatment effect
goal is to choose hyper parameters which minimise sum of diffence between these and cross valid data

Once we have the trees from the last one, calculate the effect using test data. nb: separate creating of tree to estimation of treatment effect

\subsection{Instrumental forests}

Estimate LATE

like causal forest, but do IV regression on leaf.


\section{Addition}

\begin{outcome}
  \begin{enumerate}
  \item Perform the operations of matrix addition and subtraction.
  \item Identify when these operations are not defined.
  \item Apply the algebraic properties of matrix addition to
    manipulate an algebraic expression involving matrices.
  \end{enumerate}
\end{outcome}

To add two matrices, the matrices have to be of the same size. The
addition works by simply adding corresponding entries of the matrices.

\begin{definition}{Addition of matrices}{addition-of-matrices}
  Let $A=\mat{a_{ij}}$ and $B=\mat{b_{ij}}$ be two
  $m\times n$-matrices. Then $A+B=C$%
  \index{matrix!addition}%
  \index{sum|see{addition}}%
  \index{addition!of matrices} where $C$ is the $m\times n$-matrix
  $C=\mat{c_{ij}}$ defined by
  \begin{equation*}
    c_{ij}=a_{ij}+b_{ij}
  \end{equation*}
\end{definition}

\begin{example}{Addition of matrices}{same-size-matrix-addition}
  Add the following matrices.
  \begin{equation*}
    A = \begin{mymatrix}{rrr}
      1 & 2 & 3 \\
      1 & 0 & 4
    \end{mymatrix},\quad
    B = \begin{mymatrix}{rrr}
      5 & 2 & 3 \\
      -6 & 2 & 1
    \end{mymatrix}
  \end{equation*}
\end{example}

\begin{solution}
  Notice that both $A$ and $B$ are of size $2\times 3$.
  Since $A$ and $B$ are of the same size, the addition is possible. Using Definition~\ref{def:addition-of-matrices},
  the addition is done as follows.
  \begin{equation*}
    A + B = \begin{mymatrix}{rrr}
      1 & 2 & 3 \\
      1 & 0 & 4
    \end{mymatrix}
    +
    \begin{mymatrix}{rrr}
      5 & 2 & 3 \\
      -6 & 2 & 1
    \end{mymatrix}
    =\allowbreak
    \begin{mymatrix}{ccc}
      1+5 & 2+2 & 3+3 \\
      1+(-6) & 0+2 & 4+1
    \end{mymatrix}
    =
    \begin{mymatrix}{rrr}
      6 & 4 & 6 \\
      -5 & 2 & 5
    \end{mymatrix}.
  \end{equation*}
\end{solution}

On the other hand, the matrices
\begin{equation*}
  \begin{mymatrix}{rr}
    1 & 2 \\
    3 & 4 \\
    5 & 2
  \end{mymatrix}
  \quad\mbox{and}\quad
  \begin{mymatrix}{rrr}
    -1 & 4 & 8 \\
    2 & 8 & 5
  \end{mymatrix}
\end{equation*}
cannot be added, because one has size $3\times 2$ while the other has size $2\times 3$.

\begin{definition}{The zero matrix}{zero-matrix}
  The \textbf{$m\times n$ zero matrix}%
  \index{zero matrix}%
  \index{matrix!zero matrix} is the $m\times n$-matrix in which all
  entries are equal to zero. It is denoted by $0$.
\end{definition}

Note there is a zero matrix for every size. For example, there is a
$2\times 3$ zero matrix, a $3\times 4$ zero matrix, and so on.

\begin{example}{The zero matrix}{zero-matrix}
  The $2\times 3$ zero matrix is $0= \begin{mymatrix}{rrr}
    0 & 0 & 0 \\
    0 & 0 & 0
  \end{mymatrix}$.
\end{example}

\begin{definition}{Negative of a matrix and subtraction}{matrix-negative}
  The \textbf{negative}%
  \index{matrix!negative}%
  \index{negative!of a matrix} of a matrix $A=\mat{a_{ij}}$ is defined
  to be $-A = [-a_{ij}]$. In other words, it is obtained by negating
  every entry of $A$. To \textbf{subtract}%
  \index{matrix!subtraction}%
  \index{subtraction!of matrices} two matrices, we simply add the
  negative of the second matrix to the first one, i.e.,
  $A-B = A+(-B)$. This is just the same as componentwise subtraction.
\end{definition}

\begin{example}{Subtraction}{matrix-subtraction}
  Subtract the following matrices.
  \begin{equation*}
    A = \begin{mymatrix}{rrr}
      1 & 2 & 3 \\
      1 & 0 & 4
    \end{mymatrix},\quad
    B = \begin{mymatrix}{rrr}
      5 & 2 & 3 \\
      -6 & 2 & 1
    \end{mymatrix}
  \end{equation*}
\end{example}

\begin{solution}
  \begin{equation*}
    A-B =
    \begin{mymatrix}{rrr}
      1 & 2 & 3 \\
      1 & 0 & 4
    \end{mymatrix}
    - \begin{mymatrix}{rrr}
      5 & 2 & 3 \\
      -6 & 2 & 1
    \end{mymatrix}
    =
    \begin{mymatrix}{ccc}
      1-5 & 2-2 & 3-3 \\
      1-(-6) & 0-2 & 4-1
    \end{mymatrix}
    =
    \begin{mymatrix}{rrr}
      -4 & 0 & 0 \\
      7 & -2 & 3
    \end{mymatrix}.
  \end{equation*}
\end{solution}

Addition of matrices obeys the same properties as addition of vectors.

\begin{proposition}{Properties of matrix addition}{properties-of-addition}
  Let $A,B$ and $C$ be matrices of the same size. Then, the following
  properties%
  \index{matrix!properties of addition}%
  \index{matrix!addition!properties}%
  \index{properties of addition!matrices} hold.

  \begin{itemize}
  \item The commutative law of addition
    \begin{equation*}
      A+B=B+A.
    \end{equation*}
  \item The associative law of addition
    \begin{equation*}
      (A+B)+C=A+(B+C).
    \end{equation*}
  \item The existence of an additive unit
    \begin{equation*}
      \begin{array}{c}
        A+0=A.
      \end{array}
    \end{equation*}
  \item The existence of an additive inverse
    \begin{equation*}
      \begin{array}{c}
        A+(-A) = 0.
      \end{array}
    \end{equation*}
  \end{itemize}
\end{proposition}

\begin{proof}
  To prove the commutative law of addition, let $A$ and $B$ be
  matrices of the same size. We want to show that $A+B=B+A$. To do so,
  we use the definition of matrix addition given in Definition~\ref{def:addition-of-matrices}.  We have
  \begin{equation*}
    A+B = \mat{a_{ij}+b_{ij}} = \mat{b_{ij}+a_{ij}} = B+A.
  \end{equation*}
  The proof of the other properties are similar, and are left as an
  exercise.
\end{proof}


\section{Magnetism}
    This is very similar to electricity. Magnetic fields are created by moving electric charges. Magnetic fields are represented as $\vec{B}$. The force that a $\vec{B}$ field exerts on a charge is $\vec{F}=q\vec{v}\times\vec{B}$, where $q$ is the charge, $\vec{v}$ is the velocity, and $\vec{B}$ is the field the charge is in.
    \subsubsection{Example 1}
    An electron is moving with speed v to the right. An external field, $\vec{B}$, points into the page. Find the force on the charge. We are going to be using an x-y axis with z not shown, but it points into the picture. (Figure 6.1)
    \begin{align*}
        \vec{v}&=v(-\hat{i})=-v\hat{i}\\
        \vec{B}&=B\hat{k}\\
        \vec{F}&=-e\vec{v}\times\vec{B}\\
        \vec{F}&=F(-\hat{i})\\
        \shortintertext{Also, let's compute $\vec{F}$}\\
        \vec{F}&=q\vec{v}\times\vec{B}\\
        &=(-e)(-\vec{v}\hat{i})\times(B\hat{k})\\
        &= ev\hat{i}\times B\hat{k}\\
        &=evB(\hat{i}\times\hat{k})\\
        &=evB(-\hat{j})\\
    \end{align*}
    \subsubsection{Example 2}
    Find the period of motion of a positive charge Q in an external magnetic field $\vec{B}$. Let $\vec{v} = v\hat{i}$ and $\vec{B}=B\hat{k}$. (Figure 6.2)
    \begin{align*}
        \vec{F}=Q\vec{v}\times\vec{B}\\
        \shortintertext{Cyclotron motion is the circular motion of a charge particle in a $\vec{B}$ field. This is \underline{uniform} circular motion. This means you cannot use a magnetic field to change speed. Magnetic fields do \underline{no work}.}
        F&=QvBsin\theta\\
        F&=QvB\\
        \shortintertext{Let's equate this force with $\vec{F}=ma$}\\
        QvB&=ma\\
        QvB&=m\frac{v^2}{R}\\
        \frac{Q}{m}&=\frac{v}{BR}
        \shortintertext{The period, $T$, of the field is given by $T=\frac{2\pi R}{v}$}\\
        T&=\frac{2\pi R}{BR\frac{Q}{m}}\\
        \alignedbox{T}{=2\pi\frac{m}{BQ}}\\
        f&=\frac{1}{2\pi}B\frac{Q}{m}\\
        \shortintertext{This is the cyclotron period and the cyclotron frequency.}\\
    \end{align*}

    \subsection{Creating Magnetic Fields}
    In the Biot Savart we know that electric chages that are moving create electric fields. In order to determine the direction we still must do the right hand rule. This is easier to visualize with a current (describes electromagnets.) Instead we are going to talk about a piece of charge:
    \begin{align*}
        \alignedbox{\vec{B}}{=\frac{\mu_0}{4\pi}\frac{q\vec{v}\times\hat{r}}{r^2}}\\
        \alignedbox{d\vec{B}}{=\frac{\mu_0}{4\pi}\frac{dq\vec{v}\times\hat{r}}{r^2}}\\
        \shortintertext{This is the Biot-Savart Law}
    \end{align*}
    \subsubsection{Example 1}
    Find magnetic field $\vec{B}$ a distance d from an infinitely long wire carrying a current I make sure you always place the $d\vec{l}$ so it creates a triangle.
    \begin{align*}
        d\vec{B}&=\frac{\mu_0}{4\pi}\frac{dq\frac{d\vec{l}}{dt}\times\hat{r}}{r^2}\\
        &=\frac{\mu_0}{4\pi}\frac{\frac{dq}{dt}d\vec{l}\times\hat{r}}{r^2}\\
        &=\frac{\mu_0}{4\pi}\frac{Id\vec{l}\times\hat{r}}{r^2}\\
        \shortintertext{Because the current changes over time, $\frac{dq}{dt}=I$. Current is moving charges so current moving on a wire thats dl long in a certain direction creates $d\vec{B}$. This is analagous to electric fields and solving for the electric field.}\\
        \vec{B}&=\frac{\mu_0}{4\pi}I\int_{-\infty}^\infty\frac{d\vec{l}\times\hat{r}}{r^2}\\
        \shortintertext{We took the right hand rule to find the direction of $\vec{B}$ but this does not tell us the magnitude of the vectors because $d\vec{l}$ is changing. We are going to use $sin\theta$ where theta is the angle between $d\vec{l}$ and $\hat{r}$ to make this easier to computate. $\hat{r}$ is a unit vector so its magnitude is zero. It is only there to help us find the direction.}\\
        \vec{B}&=\frac{\mu_0}{4\pi}I\int_{-\infty}^\infty\frac{dlsin(\theta)}{r^2}\\
        \shortintertext{Now we are going to take the $sin\theta$ and change it into the coordinates we are using (xyz). We can figure out that $r^2=x^2+d^2$ and that $sin\theta=\frac{d}{r}$}\\
        &=\frac{\mu_0}{4\pi}I\int_{-\infty}^\infty\frac{dx}{x^2+d^2}\frac{d}{\sqrt{x^2+d^2}}\\
        \shortintertext{d is a constant of integration because the current doesn't change along d at all.}
        &=\frac{\mu_0}{4\pi}Id\int_{-\infty}^\infty\frac{dx}{(x^2+d^2)^\frac{3}{2}}\\
        &=\frac{\mu_0}{4\pi}Id\left[\frac{x}{d^2}\frac{1}{\sqrt{x^2+d^2}}\right]_{x=-\infty}^{x=\infty}\text{ out of page}\\
        \shortintertext{We are going to divide both the top and bottom by x because infinity cannot be at the top of a fraction.}\\
        d\vec{B}&=\frac{\mu_0}{4\pi}Id\left[\frac{1}{d^2}\frac{1}{\sqrt{1+\frac{d^2}{x^2}}}\right]_{x=-\infty}^{x=\infty}\\
        \shortintertext{This is a problem because if we evaluate it now, we will get 0 as our magnetic field. Because of this we must do the following:}
        \vec{B}&=2\frac{\mu_0}{4\pi}\frac{I}{d}\frac{1}{\sqrt{1+\frac{d^2}{x^2}}}\Big|_{x=-\infty}^{x=\infty}\\
        \vec{B}&=\frac{\mu_0}{2\pi}\frac{I}{d}\left[1-0\right]\\
        \alignedbox{\vec{B}}{=\frac{\mu_0I}{2\pi d}\text{ out of page}}\\
    \end{align*}
    There is an additional right hand rule. Put your thumb in the direction of the current wire, then your wrapped fingers will give the direction of $\vec{B}$.
    \subsubsection{Example 2}
    Remember that magnetic force is written as $\vec{F}=q\vec{v}\times\vec{B}$. In scenario 1 we are going to have a positive charge that is going to move initially to the right with some velocity v. It is about to enter a region with a magnetic charge that points into the page. What electric field can we add to the region to prevent the moving charge from deflecting? By the right hand rule, the particle is going to feel a force upward.
    \begin{align*}
        \vec{F_B}&=q\vec{v}\times\vec{B}\\
        \vec{F_B}&=qvB \text{, up}\\
        \shortintertext{We need $\vec{E}$ to cause an equal force down so that $\vec{F}_{net}=0$}\\
        \vec{F_E}&=q\vec{E}\\
        \shortintertext{$\vec{E}$ must point downward.}\\
        F_E&=F_B\\
        qE&=qvB\\
        E&=vB\\
    \end{align*}
    Now in scenario 2 we have a negative charge that is going to move initially to the right with a magnetic region pointing into the charge. With a negative charge and right hand rule, we must flip our hand over because of the negativity.
    \begin{align*}
        \vec{F_B}&=qvB\text{, Down. we need}\\
        \vec{F}&=q\vec{E} \text{ to point up.}\\
    \end{align*}
    We actually get the same answer as before. This is because they both depend on q, and if the sign on q changes, it changes on both sides of the equation.\newline\newline
    In scenario 3, our charged particle will be positive and heading right, but the magnetic field is in line with the charge's movement.
    \begin{align*}
        \vec{F_B}&=q\vec{v}\times\vec{B}=0\\
        \to &=qvBsin(0)=0\\
    \end{align*}
    In this case we don't need an $\vec{E}$ field at all to keep the charge from deflecting.\newline\newline
    With scenario 4, we have a positive charge moving to the right into a magnetic field that has a diagonal vector.
    \begin{align*}
        \vec{F_B}&=q\vec{v}\times\vec{B}\\
        F_B&=qvB_{\perp}\\
        \shortintertext{We must cross with the vertical component of B because the horizontal is parallel and gives 0}\\
        \vec{F_B}&=qvBsin(\theta)\text{, out of page}\\
        \text{$\vec{E}$ is into the page}\\
        qE&=qvBsin(\theta)\\
        \to E&=vBsin(\theta)\\
    \end{align*}
    \subsubsection{Example 3}
    Let $\vec{B}=B(-\hat{j})=-B\hat{j}$ and $\vec{v}$ that initially looks like $\vec{v}=v_{0_y}\hat{j}+v_{0_z}\hat{k}$. What will the motion of q look like?
    \begin{align*}
        \vec{F}&=q\vec{v}\times\vec{B}\\
        &=q(v_{0_y)}\hat{k}+v_{0_z)}\hat{k})\times(-B\hat{j})\\
        &=-q(v_{0_y}B(\hat{j}\times\hat{j})+v_{0_z}B(\hat{k}\times\hat{j}))\\
        &=-qv_{0_z}B(\hat{k}\times\hat{j})\\
        &=-qv_{0_z}B(-\hat{i})\\
        \alignedbox{\vec{F_0}}{=qv_{0_z}B\hat{i}}\\
    \end{align*}
    This will cause a helical motion: linear in $\hat{j}$ and cyclotron in the $\hat{i}$ and $\hat{k}$ directions. Basically it creates a spring.
    \subsubsection{Example 4}
    Force on a current.
    \begin{align*}
        \vec{F}&=q\vec{v}\times\vec{B}
        \shortintertext{Imagine a small amount of charge dq. The force exerted on this small amount of charge dq by an external field $\vec{B}$ is:}\\
        d\vec{F}&=dq\vec{v}\times\vec{B}\\
        d\vec{F}&=dq\frac{d\vec{l}}{dt}\times\vec{B}\\
        d\vec{F}&=\frac{dq}{dt}d\vec{l}\times\vec{B}\\
        \alignedbox{d\vec{F}}{=Id\vec{l}\times\vec{B}}\\
        \shortintertext{If everything is uniform, }\\
        \int d\vec{F}&=\int Id\vec{l}\times\vec{B}\\
        \vec{F}&=I\vec{l}\times\vec{B}\\
    \end{align*}
    \subsubsection{Example 5}
    For a uniform external field, $\vec{B}=4mT$ into the page, which way will a free wire move if the current as shown is $I=2A$ CCW. Let $a=10cm$. The force will be \underline{to the right.}
    \begin{align*}
        \vec{F}&=I\vec{l}\times\vec{B}\\
        F&=IlB\\
        &=(2A)(10cm)(4mT)\\
        &=(2A)(0.1m)(4\times10^{-3}T)\\
        \alignedbox{\vec{F}}{=8\times10^{-4}T}\\
    \end{align*}

    \subsection{Holl Effect}
    Consider a metal plate. We will drive a current through this metal plate (figure 6.3). Let's add $\vec{B}$ into the page. The current will feel a force acting upwards due to the right hand rule. This is because our fingers go to the right and curl to inside the page, the thumb gives us up which is the direction in which the force will move. These two figures are almost the same exact setup, but there is a change in the measured voltage. \newline\newline
    If the measured voltage is positive, the current is made up of positively charged particles. If the measured voltage is negative, the current is made up of negative charged particles. It turns out that the charge carriers are negative (electrons).
    \subsubsection{Example 1}
    This is a Biot Savart example.
    \begin{align*}
        d\vec{B}&=\frac{\mu_0}{4\pi}\frac{Id\vec{l}\times\hat{r}}{r^2}\\
        \shortintertext{Let's find $\vec{B}$ at the center of an arc of current (figure 6.5). Remember that the cross product of $d\vec{l}\times\vec{B}$. For magnetic field, we put our thumb in the direction of the current and wrap our hands around to the direction of the field. The current is going to be going along the rod clockwise. $\hat{r}$ is always going to be perpendicular to $d\vec{l}$ because I is always going to be pointing directly out of the circle, while r always points inward.}\\
        d\vec{B}&=\frac{\mu_0}{4\pi}I\frac{dl*1*sin(90)}{r^2}\text{ into page}\\
        \int d\vec{B}&=\int\frac{\mu_0}{4\pi}I\frac{dl}{r^2}\text{ into page}\\
        \vec{B}&=\frac{\mu_0}{4\pi}I\int\frac{dl}{R^2}\text{ into page}\\
        \vec{B}&=\frac{\mu_0}{4\pi}\frac{I}{R^2}\int dl\\
        \vec{B}&=\frac{\mu_0}{4\pi}\frac{I}{R^2}\int Rd\theta\\
        &=\frac{\mu_0}{4\pi}\frac{I}{R}\int d\theta\\
        \alignedbox{\vec{B}}{=\frac{\mu_0I}{4\pi R}\theta\text{ into page}}\\
        \shortintertext{For a full loop of current, $\theta=2\pi$}\\
        \vec{B}&=\frac{\mu_0I}{4\pi R}2\pi\\
        \alignedbox{\vec{B}}{=\frac{\mu_0I}{2R}\text{ into page}}\\
        \shortintertext{If I was variable, then we couldn't pull it out of the integral, but the direction would remain the same.}
    \end{align*}

    \subsection{Ampere's Law}
    \begin{align*}
        \shortintertext{Figure 6.6}\\
        \alignedbox{\oint\vec{B}\cdot d\vec{l}}{=\mu_0I_{th}}\\
        \shortintertext{Very similar to Gauss' law. We must choose a path in which we can nicely handle the expression above.}\\
        \oint Bdl&=\mu_0I_{th}\\
        B\oint dl &=\mu_0I\\
        B2\pi r &=\mu_0 I\\
        \alignedbox{\vec{B}}{\frac{\mu_0I}{2\pi r}\text{ CCW}}\\
        \shortintertext{If you were to pick $d\vec{l}$ to go to the wrong direction, the dot product results in zero so you get: $-\oint Bdl=\mu_0(-I)$. The minuses cancel and you end up getting the same answer: $\vec{B}=\frac{\mu_0I}{2\pi r}$}\\
    \end{align*}
    A solenoid is a coil of wire in the shape of a slinky (it stays in place) this is shown in figure 6.7. Magnetic field lines never start or stop and they never diverge, meaning they are always parallel. An ideal solenoid has a length much larger than it's radius ($L>>R$). Solenoids are used in MRI machines, hence the name (Magnetic resistance imaging). They are the tube that people are pushed into.
    \subsubsection{Example 1}
    Imagine an infinite perfect solenoid (figure 6.8). Let's find $\vec{B}$ for an ideal solenoid. The $d\vec{l}$ loop must encapsulate some form of current in order to be useful with this equation
    \begin{align*}
        \oint \vec{B}\cdot d\vec{l}&=\mu_0 I_{th}\\
        \shortintertext{It is easiest to split this up into multiple loops because there are four sides for the rectangle we chose. We labeled the sides (1, 2, 3, 4) so we could label our integrals.}\\
        \int\vec{B}\cdot d\vec{l_{1}}+\int\vec{B}\cdot d\vec{l_{2}}+\int\vec{B}\cdot d\vec{l_{3}}+\int\vec{B}\cdot d\vec{l_{4}}&=\mu_0I_{th}\\
        \shortintertext{For the first integral, $\vec{B}\cdot d\vec{l_1}$ gives us $bl$. For the second and fourth integrals, the dot product equals zero. For the third segment, $\vec{B}=0$ so we cannot do anything with that. The amount of current all along the loop is the same throughout. I is the current through each of the wires so the current through one wire is going to be I.}\\
        Bl+0+0+0&=\mu_02I\\
        \shortintertext{In a more general case, let's say that $N$ currents pierce throguh the Amperian loop.}\\
        Bl&=\mu_0NI\\
        \shortintertext{If for the whole solenoid length L:}\\
        BL_{tot}&=\mu_0N_{tot}I\\
        B&=\mu_0\frac{N_{tot}}{L_{tot}}I\\
        \alignedbox{B}{=\mu_0nI}\text{ Where $n$ is turns per length}\\
    \end{align*}
    If you want a bigger field you can either increase the current or put more turns in the current.

    \subsection{Magnetic Inductance}
    We will first consider Faraday's Law.
    \begin{align*}
        \Phi_B&=\vec{B}\cdot d\vec{A}\\
        \shortintertext{Where $\Phi_B$ is magnetic flux.}\\
        \alignedbox{E_{ind}}{=-\frac{d}{dt}\Phi_B}\\
        \shortintertext{$E_{ind}$ is also known as E.M.F. which is an induced voltage or potential.}\\
    \end{align*}
    \subsubsection{Example 1}
    A $\vec{B}$ field is into the page and increasing linearly in time: $B=b_0\frac{t}{t_0}$. Find the direction and magnitude of current induced on a circular loop of wire with radius, r, and resistance, R, in plane with the page (figure 6.9). Electricity and magnetism are two different expressions of the same thing (electromagnetism). If a voltage is induced on a wire where we can calculate current we can just use Ohm's Law. We need a magnetic field that fluctuates in time. If the flux doesn't change in time then there is no induced electric field. While magnetic field is into the page everywhere, the resistor only cares about the magnetic field that is on the wire. It is okay that our B value does not have area dependence because we are calculating the flux of the magnetic field. We only need to discuss the area of magnetism when we get the flux.
    \begin{align*}
        \Phi_B&=\int\vec{B}\cdot d\vec{A}\\
        &=\int \left(B_0\frac{t}{t_0}\hat{k}\right)\cdot\left(dA\hat{k}\right)\\
        &=B_0\frac{t}{t_0}\int dA\\
        &=B_0\frac{t}{t_0}\pi r^2\\
        \shortintertext{Always find the flux before taking the time derivative. Now we are moving on to the time derivative:}\\
        \to\frac{t}{dt}\Phi_B&=\frac{d}{dt}B_0\frac{t}{t_0}\pi r^2\\
        &=B_0\frac{\pi r^2}{t_0}\\
        \shortintertext{Recall that $E_{ind}=-\frac{d}{dt}\Phi_B$. The negative sign is because of Lenz's law. This basically just helps determind the direction of the magnitude of $E_{ind}$. \underline{Lenz's Law:} The induced current $I_{ind}=\frac{E_ind}{R}$ has a direction such that $B_{ind}$ opposes the change in flux, that is, $\frac{d}{dt}\Phi_B$. For this case, $I_{ind}$ must flow counter-clockwise because it must oppose the change in the flux.}\\
        |E_{ind}|=|\frac{d}{dt}\Phi_B|=|B_0\frac{\pi r^2}{t_0}|\\
        \alignedbox{I_{ind}}{=\frac{1}{R}B_0\frac{\pi r^2}{t_0}\text{, ccw}}\\
    \end{align*}
    Here are some other Lenz's law cases (figure 6.10). The equation for $\vec{B}$ in this case is $\vec{B}=B\frac{t}{t_0}\hat{k}$. The flux is \underline{decreasing} into the page so the $B_{ind}$ is going to point into the page, which would mean that our $I_{ind}$ is going clockwise. You \underline{oppose the change in the flux}. You do \underline{not} oppose the direction of the magnetic field.\newline\newline
    Let's now look at figure 6.11. This figure has a square loop in the middle and we know that $I_{int}$ goes clockwise because the magnetic force is going into the page and not out of the page. With $B=B_0e^\frac{t}{t_0}$, there is no real change in this problem compare to the last. We must also understand that we deal with a square loop the same way we dealt with a circular
    one.

    \subsection{Eddy Currents}
    Similar to how if you're rowing a boat. As you pull your oar through the water you get little eddy's around the stick. These are magnetically induced currents that appear when Farade's law results in the slowing down of an object. This is a type of induced current. An example of this is sorting recyclables. You have a platform with a bunch of material heading down a slope. If the box is cardboard, then it will have an induced $E_{mf}$, but won't feel an induced current. If the recyclables are made from aluminum such as cans, then they will continue moving downward.
    \subsubsection{Motional Emf (E)}
    This goes along with figure 6.12. What direction will the force be in?
    \begin{align*}
        \vec{F}&=I\vec{l}\times\vec{B}\\
        &=I_{ind}\vec{l}\times\vec{B_{ext}}\\
        \shortintertext{Say we pull such that the speed of the loop is constant. Because we already know the direction we are going to just use the magnitude to determine what $E_{ind}$ is.}\\
        E_{ind}&=-\frac{d}{dt}\Phi_B\\
        |E_{ind}|&=|-\frac{d}{dt}\Phi_B|\\
        |E_{ind}|&=\frac{d}{dt}(BA)\\
        |E_{ind}|&=B\frac{d}{dt}A+A\frac{d}{dt}B\\
        &=B\left(L\frac{dx}{dt}+x\frac{dL}{dt}\right)\\
        &=BL\frac{dx}{dt}\\
        \shortintertext{We know that the speed is not changing so we can come to the conclusion that:}\\
        \alignedbox{|E_{ind}|}{=B_{ext}Lv}\\
        \shortintertext{From Ohm's law:}\\
        E_{ind}&=I_{ind}R\\
        I_{ind}&=\frac{B_{ext}Lv}{R}\\
        P&=I_{ind}E_{ind}\\
        \alignedbox{P}{=\frac{B_{ext}^2L^2v^2}{R}}\\
    \end{align*}

    \subsection{Induced Electric Field}
    Farade's Law: $E_{ind}=-\frac{d}{dt}\Phi_B$ (Figure 6.13). This means that $E_{ind}$ points the same direction as $I_{ind}$. This gives us that $\oint\vec{E_{ind}}\cdot d\vec{l}=-\frac{d}{dt}\Phi_B$. These charged particles must be in a loop in order to come to this conclusion. Farade's law helps us further relate the previous equation.
    \begin{align*}
        \oint\vec{E_{ind}}\cdot d\vec{l}&=-\frac{d}{dt}\int\vec{B}\cdot d\vec{a}\\
    \end{align*}
    A time varying magnetic flux induces an electric field. Electric and magnetic fields are frame dependent. They depend on what inertial frame they are in. An inertial frame is one in which there is no acceleration.
    \subsubsection{Induction Applied to Circuits}
    Let's consider a solenoid. If we apply a current then there will be a magnetic field throughout the inside of the solenoid. The best way to solve for the magnetic field within a solenoid is by using Ampere's law, $B=\mu_0nI$. The flux through the solenoid is:
    \begin{align*}
        \Phi_B&=\int\vec{B}\cdot d\vec{a}\\
        \Phi_B&=Ba\\
        \shortintertext{We can say that because the flux is proportional to the magnetic field, the flux is proportional to the current as well. We now introduce inductance L.}\\
        \Phi_B&=LI\\
        \shortintertext{L depends on the solenoid's geometry.}\\
        N\Phi_B&=LI\\
        \shortintertext{Where N is the number of turns in the solenoid}\\
        \alignedbox{L}{=N\frac{\Phi_B}{I}}\\
        \shortintertext{Let's find the $E_{ind}$ of the solenoid}\\
        E_{ind}=-\frac{d}{dt}\Phi_B&=-\frac{d}{dt}\left(\frac{LI}{N}\right)\\
        E_{ind}&=-\frac{L}{N}\frac{d}{dt}I
    \end{align*}
    For a self induced emf:
    \begin{align*}
        E_{ind}&=-L\frac{dI}{dt}
    \end{align*}
    \subsubsection{Example 1}
    Let's find the amount of energy stored in the magnetic field of a solenoid. First let's find the power.
    \begin{align*}
        |P|&=IV=|-LI\frac{dI}{dt}|\\
        \shortintertext{We know that power is the change of energy over the change in time, this is why we are able to jump to the next step.}\\
        U&=\int Pdt\\
        &=\int LI\frac{dI}{dt}dt\\
        &=\int LIdI\\
        \alignedbox{U_B}{=\frac{1}{2}LI}\\
        \shortintertext{Recall from electricity, $U_E=\frac{1}{2}CV^2$. In this case the charges are moving. With the magnetic energy equation, we are changing the energy for already moving charges.}\\
    \end{align*}

    \subsection{List of Maxwell's Equations:}
    \begin{align*}
        \int\vec{E}\cdot d\vec{a}=\frac{Q_{enc}}{\epsilon_0}\\
        \oint\vec{B}\cdot d\vec{l}=\mu_0I_{th}\\
        \text{also }\oint\vec{B}\cdot d\vec{a}=0\\
        \shortintertext{There are no magnetic charges/monopoles. Magnetic fields never terminate.}\\
        \shortintertext{We have recently used Farade's law}\\
        E_{ind}=-\frac{d}{dt}\Phi_B\\
        \oint\vec{E}\cdot d\vec{l}=-\frac{d}{dt}\int\vec{B}\cdot d\vec{a}\\
        \shortintertext{We will update Ampere's law to be:}\\
        \oint\vec{B}\cdot d\vec{l}=\mu_0I_{th}+\mu_0I_{disp}\\
        \text{or }\oint\vec{B}\cdot d\vec{l}=\mu_0\int\vec{J}\cdot d\vec{a}+\mu_0\epsilon_0\frac{d}{dt}\Phi_E\\
        \shortintertext{Where $I_{disp}$ and $\epsilon_0\frac{d}{dt}\Phi_E$ are the displacements of the currents}
    \end{align*}
    Example with the new Ampere's Law:
    Current changing with a capacitor (figure 6.13). We are allowing a bubble to encapsulate the leftmost plate where there is no loop. There must be a changing electric flux to resolve this issue.
    \begin{align*}
        \text{Gauss's Law:}\\
        \oint\vec{E}\cdot d\vec{a}=\frac{Q_{enc}}{\epsilon_0}\\
        \oint\vec{B}\cdot d\vec{a}=0\\
        \text{Farade's Law:}\\
        \oint\vec{E}\cdot d\vec{l}=-\frac{d}{dt}\int\vec{B}\cdot d\vec{a}\\
        \text{Ampere-Maxwell:}\\
        \oint\vec{B}\cdot d\vec{l}=\mu_0\int\vec{J}\cdot d\vec{a}+\mu_0\epsilon_0\frac{d}{dt}\int\vec{E}\cdot d\vec{a}\\
        \text{Lorenty Force Law:}\\
        \vec{F}=q\vec{E}+q\vec{v}\times\vec{B}
    \end{align*}
    Here's the differential form of all of these equations:
    \begin{align*}
        \vec{\nabla}&=\frac{\partial}{\partial x}\hat{i}+\frac{\partial}{\partial y}\hat{j}+\frac{\partial}{\partial z}\hat{k}\\
        \vec{\nabla}\cdot\vec{E}&=\frac{\rho}{\epsilon_0}\\
        \vec{\nabla}\cdot\vec{B}&=0\\
        \vec{\nabla}\times\vec{E}&=-\frac{\partial}{\partial t}\vec{B}\\
        \vec{\nabla}\times\vec{B}&=\mu_0\vec{J}+\mu_0\epsilon_0\frac{\partial}{\partial t}\vec{E}\\
    \end{align*}
\newpage


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

%%% \documentclass{article}
%%% \usepackage{graphicx}
%%% \usepackage{color}

%%% \sloppy
%%% \definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}

%%% \begin{document}

\subsection*{Four parameter sine wave fitting}

\begin{par}
Example for algorithm FPNLSF.
\end{par} \vspace{1em}
\begin{par}
FPNLSF is an algorithm for estimating the frequency, amplitude, and phase of the sine waveform. The algorithm use least squares method. Algorithm requires good estimate of frequency.
\end{par} \vspace{1em}

\subsubsection*{Contents}

\begin{itemize}
\setlength{\itemsep}{-1ex}
   \item Generate sample data
   \item Call algorithm
   \item Display results
\end{itemize}

\subsubsection*{Generate sample data}

\begin{par}
Two quantities are prepared: \lstinline{t} and \lstinline{y}, representing 1 second of sinus waveform of nominal frequency 1 kHz, nominal amplitude 1 V, nominal phase 1 rad and offset 1 V sampled at sampling frequency 10 kHz.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
DI = [];
Anom = 2; fnom = 100; phnom = 1; Onom = 0.2;
DI.t.v = [0:1/1e4:1-1/1e4];
DI.y.v = Anom*sin(2*pi*fnom*DI.t.v + phnom) + Onom;
\end{lstlisting}
\begin{par}
Lets make an estimate of frequency 0.2 percent higher than nominal value:
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
DI.fest.v = 100.2;
\end{lstlisting}

\subsubsection*{Call algorithm}

\begin{par}
Use QWTB to apply algorithm \lstinline{FPNLSF} to data \lstinline{DI}.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
CS.verbose = 1;
DO = qwtb('FPNLSF', DI, CS);
\end{lstlisting}

        \begin{lstlisting}[style=output]
QWTB: no uncertainty calculation
Fitting started

Local minimum found.

Optimization completed because the size of the gradient is less than
the default value of the function tolerance.

Fitting finished
\end{lstlisting} \color{black}

\subsubsection*{Display results}

\begin{par}
Results is the amplitude, frequency and phase of sampled waveform.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
A = DO.A.v
f = DO.f.v
ph = DO.ph.v
O = DO.O.v
\end{lstlisting}

        \begin{lstlisting}[style=output]

A =

    2.0000

f =

   100

ph =

    1.0000

O =

    0.2000

\end{lstlisting} \color{black}
    \begin{par}
Errors of estimation in parts per milion:
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
Aerrppm = (DO.A.v - Anom)/Anom .* 1e6
ferrppm = (DO.f.v - fnom)/fnom .* 1e6
pherrppm = (DO.ph.v - phnom)/phnom .* 1e6
Oerrppm = (DO.O.v - Onom)/Onom .* 1e6
\end{lstlisting}

        \begin{lstlisting}[style=output]

Aerrppm =

   4.8894e-07

ferrppm =

     0

pherrppm =

   4.8850e-09

Oerrppm =

  -1.1102e-08

\end{lstlisting} \color{black}

%%% \end{document}


\documentclass[11pt]{article}

\usepackage{amsmath,amsthm}
\usepackage[headings]{fullpage}
\usepackage[utopia]{mathdesign}
\usepackage{color}

\pagestyle{myheadings}
\markboth{Matrix conditioning}{}

\input{../../fncextra}

\begin{document}

\begin{center}
  \bf Terms and conditions
\end{center}

Previously you have seen how to blur an image, represented as an
$m\times n$ pixel intensity matrix $\mX$, through multiplying by a matrix on each side:
\begin{equation}
  \label{eq:2}
   \mZ=\mV\, \mX \, \mH,
\end{equation}
where $\mV = (\mB_m)^k$, $\mH = (\mB_n)^k$,
$B_i$ is the $i\times i$ matrix made by your function
\texttt{blurmatrix}, and $k$ is a positive integer. You have also seen that deblurring can be accomplished by multiplying by matrix inverses on each side of $\mZ$ (as computed equivalently by solving linear systems). However, while the restoration seems perfect for $k=1$, it fails completely for larger $k$.

Matrix condition numbers explain these observations. The blurred matrix $\mZ$ is perturbed by an amount comparable to machine precision. For $k=1$, the condition numbers of $\mV$ and $\mH$ are not large enough to amplify this error up to the same order of magnitude as $\mX$ itself, so the noise is not perceived. But at some $k>1$, the condition numbers are so large that the noise is amplified enough to overwhelm the expected result.

Let $\bfx$ be a single column of $\mX$. For the case of vertical blurring only, we have $\bfz=\mV\bfx$ as a column of $\mZ$. We solve $\mV \bfy = \bfz$ for $\bfy$, which is mathematically the same as $\bfx$. Due to machine precision, though, the perturbation to $\bfz$ causes an error satisfying
\begin{equation}
	\label{bound}
	\frac{\|\bfy-\bfx\|}{\|\bfx\|} \le \kappa(\mV)\macheps,
\end{equation}
with $\kappa$ being the matrix condition number.

\subsection*{Preparation}

Read Section~2.8. Make sure your working \texttt{blurmatrix.m} is available.

\subsection*{Goals}

You will compute condition numbers of blur matrix powers and compare them to the errors of repeated blur/deblur operations.

\subsection*{Procedure}

\begin{enumerate}
\item The condition number of a matrix has two factors, $\|\mA\|$ and $\|\mA^{-1}\|$. First you will show that the $\|\mA\|$ term makes no trouble. For $n=50,100,150,\ldots,800$, plot $\|\mB_n\|$ versus $n$.

\item For the same $n$ as in step 1, plot $\kappa(\mB_n)$ versus $n$. You should use a log-log scale for this graph and get essentially a straight line. This implies that $\log \kappa \approx a \log n + b$, or $\kappa \approx C n^p$.

\item Let $\mV=\mB_{100}$. For $k=1,2,\ldots,8$, plot $\kappa(\mV^k)$ as a function of $k$. This time the graph is straight on a semi-log scale, which implies $\kappa \approx C q^k$.

\item Let $\bfx$ be a random vector of length 100. For $k=1,2,\ldots,8$, let $\bfz=\mV\bfx$ and then solve $\mV\bfy=\bfz$ for $\bfy$. Record the relative error in the result. Then make a table showing both sides of the inequality~\eqref{bound}.

\end{enumerate}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: