% vim: set ts=2 sw=2 et tw=80:

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc} \usepackage[margin=2cm]{geometry}
\usepackage{amstext} \usepackage{amsmath} \usepackage{array}
\newcommand{\lra}{\Leftrightarrow}

\title{Howework 4 -- Introduction to Computational Science}

\author{Claudio Maggioni}

\begin{document} \maketitle 
\section*{Question 1}
$$L_0(x) = \prod_{j = 0, j \neq 0}^n \frac{x - x_j}{x_i - x_j} = 
\frac{x - (-0.5)}{(-1) - (-0.5)} \cdot \frac{x - 0.5}{(-1) - 0.5} \cdot 
\frac{x - 1}{(-1) - 1} =  -\frac{2}{3}x^3 + \frac{2}{3}x^2 
+\frac{1}{6} x - \frac{1}{6}$$

$$L_1(x) = \prod_{j = 0, j \neq 1}^n \frac{x - x_j}{x_i - x_j} = 
\frac{x - (-1)}{(-0.5) - (-1)} \cdot \frac{x - 0.5}{(-0.5) - 0.5} \cdot 
\frac{x - 1}{(-0.5) - 1} = \frac{4}{3}x^3 - \frac{2}{3}x^2 - \frac{4}{3}x + \frac{2}{3}$$

$$L_2(x) = \prod_{j = 0, j \neq 2}^n \frac{x - x_j}{x_i - x_j} = 
\frac{x - (-1)}{0.5 - (-1)} \cdot \frac{x - (-0.5)}{0.5 - (-0.5)} \cdot 
\frac{x - 1}{0.5 - 1} = -\frac{4}{3}x^3 - \frac{2}{3}x^2 + \frac{4}{3}x + \frac{2}{3}$$

$$L_3(x) = \prod_{j = 0, j \neq 3}^n \frac{x - x_j}{x_i - x_j} = 
\frac{x - (-1)}{1 - (-1)} \cdot \frac{x - (-0.5)}{1 - (-0.5)} \cdot 
\frac{x - 0.5}{1 - 0.5} = \frac{2}{3}x^3 + \frac{2}{3}x^2 -\frac{1}{6}x - \frac{1}{6}$$

$$p(x) = \sum_{i=0}^n y_i L_i(x) =
2 \cdot \left(-\frac{2}{3}x^3 + \frac{2}{3}x^2 
+\frac{1}{6} x - \frac{1}{6}\right) +
1 \cdot \left(\frac{4}{3}x^3 - \frac{2}{3}x^2 - \frac{4}{3}x + \frac{2}{3}\right) +
$$$$
0.5 \cdot \left(-\frac{4}{3}x^3 - \frac{2}{3}x^2 + \frac{4}{3}x + \frac{2}{3}\right) + 
0.4 \cdot \left(\frac{2}{3}x^3 + \frac{2}{3}x^2 -\frac{1}{6}x - \frac{1}{6}\right) = -\frac{2}{5}x^3 + \frac{3}{5}x^2 - \frac{2}{5}x + \frac{3}{5}$$

$$\frac{\max_{x \in [-1,1]} |f^{(n+1)}|}{4!} = 
\frac{\max_{x \in [-1,1]}|\frac{768}{|2x+3|^6}|}{24} =
\max_{x \in [-1,1]}\frac{32}{|2x+3|^6} = 32$$

$$\max_{x \in [-1,1]} \left|(x - 1)\left(x - \frac{1}{2}\right)
\left(x + \frac{1}{2}\right)(x+1)\right| = 
\max_{x \in [-1,1]} \left| x^4 - \frac{5}{4}x^2 + \frac{1}{4}\right| = \frac{1}{4}$$

$$\max_{x \in [-1,1]} |f(x) - p(x)| \leq \frac{\max_{x \in [-1,1]} |f^{(n+1)}|}{4!} \max_{x \in [-1,1]} \left|(x - 1)\left(x - \frac{1}{2}\right)
\left(x + \frac{1}{2}\right)(x+1)\right| =$$$$= 32 \cdot \frac{1}{4} = 8 \leq 8$$

The statement above is true so p satisfies the error estimate:

$$\max_{x \in [-1,1]} |f(x) - p(x)| \leq 8$$

\section*{Question 2}

We first use the Lagrange method:

$$L_1(x) = \prod_{j = 0, j \neq 1}^2 \frac{x - x_j}{x_i - x_j} = 
\frac{x-0}{1-0} \frac{x-3}{1-3} = -\frac{1}{2}x^2 + \frac{3}{2}x$$

$$L_2(x) = \prod_{j = 0, j \neq 2}^2 \frac{x - x_j}{x_i - x_j} = 
\frac{x-0}{3-0} \frac{x-1}{3-1} = \frac{1}{6}x^2 - \frac{1}{6}x $$

$$p(x) = (-3) \cdot \left(-\frac{1}{2}x^2 + \frac{3}{2}x\right) + 
1 \cdot \left(\frac{1}{6}x^2 - \frac{1}{6}x\right) =
\frac{5}{3}x^2 - \frac{14}{3}x$$

Then we use the Newtonian method:

$$a_0 = f[0] = 0, \hspace{2cm} f[1] = -3 \hspace{2cm} f[3] = 1$$
$$a_1 = f[0,1] = \frac{-3-0}{1-0} = -3, \hspace{2cm} f[1,3] = \frac{1-(-3)}{3-1} = 2 $$
$$a_2 = f[0,1,3] = \frac{2-(-3)}{3-0} = \frac{5}{3}$$

$$p(x) = \left(\frac{5}{3}(x-1)-3\right) x + 0 = \frac{5}{3}x^2 - \frac{14}{3}x$$

The interpolating polynomials are indeed equal.

Now we use the Horner method to compute $p(0.5)$:

$$y=a_n = a_2 = \frac{5}{3}$$

$$i = 1$$
$$y = y (x - x_1) + a_1 = \frac{5}{3} (0.5 - 1) + a_1 - 3 = -\frac{5}{6} - 3 = -\frac{23}{6}$$

$$i = 0$$
$$y = y (x - x_0) + a_0 = -\frac{23}{6} \cdot 0.5 + 0 = -\frac{23}{12}$$

\section*{Question 4}
\subsection*{Point a)}

The node coordinates to which fix the quadratic spline are $(1/2, y_0), (3/2, y_1),(5/2, y_2),(7/2, y_3)$.

Then, we can start formulating the equations for the linear system:

$$y_0 = s\left(\frac{1}{2}\right) = a_{-1}B_2\left(\frac{1}{2} + 1\right) + a_{0}B_2\left(\frac{1}{2}\right) + a_{1}B_2\left(\frac{1}{2} - 1\right) + $$$$
+ a_{2}B_2\left(\frac{1}{2} - 2\right)
+ a_{-1}B_2\left(\frac{1}{2} - 3\right)
+ a_{0}B_2\left(\frac{1}{2} - 4\right)
+ a_{1}B_2\left(\frac{1}{2} - 5\right)=$$$$
a_{-1} \cdot 0 + a_{0} \cdot \frac{1}{2} + a_{1} \cdot \frac{1}{2} + a_{2} \cdot 0 + a_{-1} \cdot 0 + a_{0} \cdot 0 + a_{1} \cdot 0 = \frac{a_0 + a_1}{2}$$ 

$$y_1 = s\left(\frac{3}{2}\right) = a_{-1}B_2\left(\frac{3}{2} + 1\right) + a_{0}B_2\left(\frac{3}{2}\right) + a_{1}B_2\left(\frac{3}{2} - 1\right) + $$$$
+ a_{2}B_2\left(\frac{3}{2} - 2\right)
+ a_{-1}B_2\left(\frac{3}{2} - 3\right)
+ a_{0}B_2\left(\frac{3}{2} - 4\right)
+ a_{1}B_2\left(\frac{3}{2} - 5\right)=$$$$
a_{-1} \cdot 0 + a_{0} \cdot 0 + a_{1} \cdot \frac{1}{2} + a_{2} \cdot \frac{1}{2} + a_{-1} \cdot 0 + a_{0} \cdot 0 + a_{1} \cdot 0 = \frac{a_1 + a_2}{2}$$ 

$$y_2 = s\left(\frac{5}{2}\right) = a_{-1}B_2\left(\frac{5}{2} + 1\right) + a_{0}B_2\left(\frac{5}{2}\right) + a_{1}B_2\left(\frac{5}{2} - 1\right) + $$$$
+ a_{2}B_2\left(\frac{5}{2} - 2\right)
+ a_{-1}B_2\left(\frac{5}{2} - 3\right)
+ a_{0}B_2\left(\frac{5}{2} - 4\right)
+ a_{1}B_2\left(\frac{5}{2} - 5\right)=$$$$
a_{-1} \cdot 0 + a_{0} \cdot 0 +  a_{1} \cdot 0 + a_{2} \cdot \frac{1}{2} + a_{-1} \cdot \frac{1}{2} + a_{0} \cdot 0 + a_{1} \cdot 0 = \frac{a_2 + a_{-1}}{2}$$ 

$$y_1 = s\left(\frac{7}{2}\right) = a_{-1}B_2\left(\frac{7}{2} + 1\right) + a_{0}B_2\left(\frac{7}{2}\right) + a_{1}B_2\left(\frac{7}{2} - 1\right) + $$$$
+ a_{2}B_2\left(\frac{7}{2} - 2\right)
+ a_{-1}B_2\left(\frac{7}{2} - 3\right)
+ a_{0}B_2\left(\frac{7}{2} - 4\right)
+ a_{1}B_2\left(\frac{7}{2} - 5\right)=$$$$
a_{-1} \cdot 0 + a_{0} \cdot 0 + a_{1} \cdot 0 + a_{2} \cdot 0 + a_{-1} \cdot \frac{1}{2} + a_{0} \cdot \frac{1}{2} + a_{1} \cdot 0  = \frac{a_{-1} + a_0}{2}$$ 

The linear system in matrix form is:

\[\frac{1}{2} \cdot
\begin{bmatrix}
0&1&1&0\\
0&0&1&1\\
1&0&0&1\\
1&1&0&0\\
\end{bmatrix}
\begin{bmatrix}
a_{-1}\\a_0\\a_1\\a_2\\
\end{bmatrix}
=
\begin{bmatrix}
y_0\\y_1\\y_2\\y_{3}\\
\end{bmatrix}\]

The determinant of that matrix is 0 so the matrix is singular.

\subsection*{Point b)}

The node coordinates to which fix the quadratic spline are $(0, y_0), (1, y_1),(2, y_2),(3, y_3)$.

Then, we can start formulating the equations for the linear system:
$$y_0 = s(0) = a_{-1}B_2\left(1\right) + a_{0}B_2\left(0\right) + a_{1}B_2\left(- 1\right) + $$$$
+ a_{2}B_2\left(- 2\right)
+ a_{-1}B_2\left(- 3\right)
+ a_{0}B_2\left(- 4\right)
+ a_{1}B_2\left(- 5\right)=$$$$
a_{-1} \cdot \frac{1}{8} + a_{0} \cdot \frac{3}{4} + a_{1} \cdot \frac{1}{8} + a_{2} \cdot 0 + a_{-1} \cdot 0 + a_{0} \cdot 0 + a_{1} \cdot 0 = 
\frac{1}{8} a_{-1} + \frac{3}{4} a_{0} + \frac{1}{8} a_{1}$$

$$y_1 = s\left(1\right) = a_{-1}B_2\left(1 + 1\right) + a_{0}B_2\left(1\right) + a_{1}B_2\left(1 - 1\right) + $$$$
+ a_{2}B_2\left(1 - 2\right)
+ a_{-1}B_2\left(1 - 3\right)
+ a_{0}B_2\left(1 - 4\right)
+ a_{1}B_2\left(1 - 5\right)=$$$$
a_{-1} \cdot 0 + a_{0} \cdot \left(\frac{1}{8}\right) + a_{1} \cdot \frac{3}{4} + a_{2} \cdot \left(\frac{1}{8}\right) + a_{-1} \cdot 0 + a_{0} \cdot 0 + a_{1} \cdot 0 = 
\frac{1}{8} a_{0} + \frac{3}{4} a_{1} + \frac{1}{8} a_{2}$$ 

$$y_2 = s\left(2\right) = a_{-1}B_2\left(2 + 1\right) + a_{0}B_2\left(2\right) + a_{1}B_2\left(2 - 1\right) + $$$$
+ a_{2}B_2\left(2 - 2\right)
+ a_{-1}B_2\left(2 - 3\right)
+ a_{0}B_2\left(2 - 4\right)
+ a_{1}B_2\left(2 - 5\right)=$$$$
a_{-1} \cdot 0 + a_{0} \cdot 0 + a_{1} \cdot \left(\frac{1}{8}\right) + a_{2} \cdot \frac{3}{4} + a_{-1} \cdot \left(\frac{1}{8}\right) + a_{0} \cdot 0 + a_{1} \cdot 0 = 
\frac{1}{8} a_{1} + \frac{3}{4} a_{2} + \frac{1}{8} a_{-1}$$

$$y_3 = s\left(3\right) = a_{-1}B_2\left(3 + 1\right) + a_{0}B_2\left(3\right) + a_{1}B_2\left(3 - 1\right) + $$$$
+ a_{2}B_2\left(3 - 2\right)
+ a_{-1}B_2\left(3 - 3\right)
+ a_{0}B_2\left(3 - 4\right)
+ a_{1}B_2\left(3 - 5\right)=$$$$
a_{-1} \cdot 0 + a_{0} \cdot 0 + a_{1} \cdot 0 + a_{2} \cdot \frac{1}{8} + a_{-1} \cdot \frac{3}{4} + a_{0} \cdot \frac{1}{8} + a_{1} \cdot 0 = 
\frac{1}{8} a_{2} + \frac{3}{4} a_{-1} + \frac{1}{8} a_{0}$$

The linear system in matrix form is:

\[\frac{1}{8} \cdot
\begin{bmatrix}
6&1&0&1\\
1&6&1&0\\
0&1&6&1\\
1&0&1&6\\
\end{bmatrix}
\begin{bmatrix}
a_0\\a_1\\a_2\\a_{-1}\\
\end{bmatrix}
=
\begin{bmatrix}
y_0\\y_1\\y_2\\y_{3}\\
\end{bmatrix}\]

\section*{Question 5}

$$1 = y_0 = s(x_0) = 0 = a_{-1}B_3(1) +a_0B_3(0)
+a_1B_3(-1)
+a_2B_3(-2)
+a_3B_3(-3) = \frac{1}{6}a_{-1} +  \frac{2}{3}a_0 + \frac{1}{6}a_1$$

$$5 = y_1 = s(x_1) = 1 = a_{-1}B_3(2) +a_0B_3(1)
+a_1B_3(0)
+a_2B_3(-1)
+a_3B_3(-2) = \frac{1}{6}a_{0} + \frac{2}{3}a_1 + \frac{1}{6}a_2$$

$$1 = y_2 = s(x_2) = 2 = a_{-1}B_3(3) +a_0B_3(2)
+a_1B_3(1)
+a_2B_3(0)
+a_3B_3(-1) = \frac{1}{6}a_{1} + \frac{2}{3}a_2 + \frac{1}{6}a_3$$

$$s''(0) =  a_{-1}B''_3(1) +a_0B''_3(0)
+a_1B''_3(-1)
+a_2B''_3(-2)
+a_3B''_3(-3) = a_{-1} - 2 a_0 + a_{1}$$

$$s''(2) =  a_{-1}B''_3(3) +a_0B''_3(2)
+a_1B''_3(1)
+a_2B''_3(-0)
+a_3B''_3(-1) = a_{1} - 2 a_2 + a_{3}$$

\[\frac{1}{6} \cdot
\begin{bmatrix}
1&-2&1&0&0\\
1&4&1&0&0\\
0&1&4&1&0\\
0&0&1&4&1\\
0&0&1&-2&1\\
\end{bmatrix}
\begin{bmatrix}
a_{-1}\\a_0\\a_1\\a_2\\a_3\\
\end{bmatrix}
=
\begin{bmatrix}
0\\y_0\\y_1\\y_2\\0\\
\end{bmatrix}\]

We then use Gaussian \textit{ellimination} to solve the system.

\[
\begin{array}{@{}ccccc|c@{}}
1&-2&1&0&0&0\\
1&4&1&0&0&6\\
0&1&4&1&0&30\\
0&0&1&4&1&6\\
0&0&1&-2&1&0\\
\end{array}
\qquad
\begin{array}{@{}ccccc|c@{}}
1&-2&1&0&0&0\\
0&6&0&0&0&6\\
0&1&4&1&0&30\\
0&0&1&4&1&6\\
0&0&1&-2&1&0\\
\end{array}
\qquad
\begin{array}{@{}ccccc|c@{}}
1&-2&1&0&0&0\\
0&1&4&1&0&30\\
0&6&0&0&0&6\\
0&0&1&4&1&6\\
0&0&1&-2&1&0\\
\end{array}
\]
\[
\begin{array}{@{}ccccc|c@{}}
1&0&9&2&0&60\\
0&1&4&1&0&30\\
0&0&-24&-6&0&-174\\
0&0&1&4&1&6\\
0&0&1&-2&1&0\\
\end{array}
\qquad
\begin{array}{@{}ccccc|c@{}}
1&0&9&2&0&60\\
0&1&4&1&0&30\\
0&0&1&4&1&6\\
0&0&-24&-6&0&-174\\
0&0&1&-2&1&0\\
\end{array}
\qquad
\begin{array}{@{}ccccc|c@{}}
1&0&0&-34&-9&6\\
0&1&0&-15&-4&6\\
0&0&1&4&1&6\\
0&0&0&90&24&-30\\
0&0&0&6&0&-6\\
\end{array}
\]
\[
\begin{array}{@{}ccccc|c@{}}
1&0&0&-34&-9&6\\
0&1&0&-15&-4&6\\
0&0&1&4&1&6\\
0&0&0&1&4/15&-1/3\\
0&0&0&6&0&-6\\
\end{array}
\qquad
\begin{array}{@{}ccccc|c@{}}
  1 & 0 & 0 & 0 & 1/15  & -16/3  \\
  0 & 1 & 0 & 0 & 0     & 1      \\
  0 & 0 & 1 & 0 & -1/15 & 22/3   \\
  0 & 0 & 0 & 1 & 4/15  & -1/3   \\
  0 & 0 & 0 & 0 & 8/5   & -8     \\
\end{array}
\qquad
\begin{array}{@{}ccccc|c@{}}
  1 & 0 & 0 & 0 & 0 & -5  \\
  0 & 1 & 0 & 0 & 0 & 1      \\
  0 & 0 & 1 & 0 & 0 & 7   \\
  0 & 0 & 0 & 1 & 0 & 1   \\
  0 & 0 & 0 & 0 & 1 & -5     \\
\end{array}
\]

Therefore, the coefficients are $a_{-1} = a_3 = -5$, $a_0 = a_2 = 1$, and $a_1 = 7$.
\end{document}
