\documentclass[11pt, a4paper]{article}
\usepackage{TMA4215-en}
\begin{document}
\prosjekt{1}
The project is about developing, testing and implementing an algorithm for
solving two-point boundary value problems of the form
\begin{equation}
  \label{eq:bvp}
  u''+p(x)u'+q(x)u = r(x), \qquad x\in[a,b], \qquad u(a) = u_a, \quad
  u(b)=u_b 
\end{equation}
numerically. 

The numerical algorithm is as follows:
\begin{enumerate}
\item Choose $n+1$ distinct nodes in $[a,b]$ with $x_0=a$ and $x_n=b$.
\item Find a polynomial~$v\in\mathbb{P}_{n}$ satisfying the boundary conditions
  in addition to the differential equation in all inner nodes. This means that
  $v$ must satisfy
  \begin{subequations} \label{eq:coll}
  \begin{align}
     &v(a)=u_a, \qquad v(b)=u_b  \label{eq:b} \\
     &v''(x_i)+p(x_i)v'(x_i)+q(x_i)v(x_i) = r(x_i), \qquad
    i=1,\dotsc,n-1. \label{eq:colla}
  \end{align}
  \end{subequations}
\end{enumerate}
Thus, we can use $v(x)$ as a numerical approximation to the solution~$u(x)$ of 
\eqref{eq:bvp}. The polynomial~$v(x)$ can be written on Lagrange form, i.e.\ 
\[ v(x)=\sum_{k=0}^n v_k \ell_k(x), \qquad
\ell_k(x)=\prod_{\substack{i=0\\i\not=k}}^n\frac{x-x_i}{x_k-x_i}. \]
This means that $v(x)$ is determined by~$v_i$, $i=0,\dotsc,n$, which is given
by the conditions~\eqref{eq:coll}.

\setcounter{Oppg}{-1}
\begin{oppgave}
  \emph{This Task is meant as an introduction to the problem. You should do it,
    but it is \emph{not} to be handed in.}
  
  Given the equation
  \[ u'' + u = 2\cos(x), \qquad u(0)=0,\; u(\pi/2)=\pi/2, \]
  with exact solution $u=x\sin(x)$.
  \begin{punkt}
    Choose nodes $x_0=0, \; x_1=\pi/4$ and $x_2=\pi/2$, and find the
    polynomial~$v(x)$ with the recipe above. Compare $v$ and the exact
    solution~$u$ (make a plot).
  \end{punkt}
  \begin{punkt}
    Add the nodes $x_3=\pi/8$ and $x_4=3\pi/8$, and repeat the experiment.
  \end{punkt}
\end{oppgave}

\newpage
\begin{oppgave}
  The condition~\eqref{eq:colla} demands that it is possible to calculate the
  derivative of the polynomial~$v(x)$ in the nodes. We can write this more
  generally as:
  \begin{itemize}
  \item Let $p\in\mathbb{P}_n$ be given by $p(x_i)=v_i$,
  $i=0,1,\dotsc,n$.
  \item Let $w_i = p'(x_i)$, $i=0,1,\dotsc,n$.
  \end{itemize}
  This is a linear operation that can be written as
  \[ w = D_n v \]
  where $D_n\in\mathbb{R}^{(n+1)\times(n+1)}$, $v=[v_0,\dotsc,v_n]^T$, and
  $w=[w_0,\dotsc,w_n]^T$. 
  \begin{punkt}
     Find $D_2$ from the nodes $x_0=0, \; x_1=\pi/4$ and $x_2=\pi/2$. 
  \end{punkt}
  \begin{punkt}
     Given an arbitrary set of discrete nodes~$(x_i)_{i=0}^n$. Show that $D_n$
     is given by
     \[ D_{ik} =
     \begin{cases}
       \displaystyle \frac{a_i}{a_k(x_i-x_k)} & \text{ when } i\not=k \\[3mm]
       \displaystyle \sum_{\substack{j=0\\j\not= k}}^n \frac{1}{x_k-x_j} & \text{ for } i=k, 
     \end{cases}
     \]
     where $\displaystyle a_k=\prod_{\substack{j=0\\j\not=k}}^n(x_j-x_k)$. 
  \end{punkt}
  \begin{punkt}
    Write a \textsc{Matlab} function that calculates the differentiation
    matrix~$D_n$ for a given set of nodes~$x$. 
  \end{punkt}
  \begin{punkt}
    Let $p$ interpolate a given function~$f(x)$ in the nodes~$(x_i)_{i=0}^n$.
    In this case, we can use $w_i=p'(x_i)\approx f'(x_i)$ as an approximation to
    the derivative in the node. Test this on the functions
    \begin{eqnarray*}
      i) && f(x) = e^x\sin(5x) \\
      ii) && f(x) = 1/(1+x^2) \\
      iii) && f(x) = \lvert x^3\rvert \\
      iv) && f(x) = x^{10}
    \end{eqnarray*}
    on the interval~$x \in [-1,1]$ with nodes given by
    \begin{equation}
      \label{eq:cheb}
      x_i = -\cos(i\pi/n), \qquad i=0,\dotsc,n. 
    \end{equation}
    Find the error~$E_n = \max_{i=0,\dotsc,n} \lvert f'(x_i)-w_i\rvert$
    numerically and plot this as a function of $n$, $n=1,\dotsc,50$. Comment
    the result.
  \end{punkt}
  \begin{punkt}
    Let $\bar{w}_i=p''(x_i)$, $i=0,\dotsc,n$. Convince yourself that these
    coefficients are given by
    \[ \bar{w} = D_n^2 v. \]
  \end{punkt}
\end{oppgave}
\newpage

\begin{oppgave}
\begin{punkt}
    Show that the conditions~\eqref{eq:coll} make up a system of linear
    equations
    \[ Mv = b. \]
    Explain how $M$ can be expressed utilising the differentiation matrix~$D_n$
    in Task 1.
  \end{punkt}
  \begin{punkt}
   Write a \textsc{Matlab} script which solves an arbitrary two-point boundary
   value problem~\eqref{eq:bvp} numerically by using \eqref{eq:coll}. Use the
   node distribution given by \eqref{eq:cheb}, perhaps moving them over to the
   interval~$[a,b]$. The script must be of the form shown in
   Figure~\ref{fig:code}.
   \begin{figure}[t]
     \begin{center}
     \begin{verbatim}
     % Define the equation:
     p = ...
     q = ...
     r = ...

     % Boundary conditions
     a = ...
     b = ...
     ua = ...
     ub = ...

     % Number of nodes - 1
     n = ...

       :
       :

     % Plot the solution. 
     \end{verbatim}
     \end{center}
     \caption{Example of the \textsc{Matlab} code in Task 2.}
     \label{fig:code}
  \end{figure}

   Test the program on the problem in Task 0 first. Choose $n=10$ and $n=20$
   and plot both the solution and the error.

   Then use the program to solve the following equations:
   \begin{align*}
     i) & && u'' + 10u' +u = 1, && u(-1)=u(1)=0. \\
     ii) & && u'' - \frac{2x}{x^2+1}u'+\frac{2}{x^2+1}u = x^2+1, &&
     u(0)=2,\; u(1)=5/3.
  \end{align*}
  You are free to choose more equations.
 \end{punkt}
\end{oppgave}
\end{document}
