diff --git a/hw4/assignment4.m b/hw4/assignment4.m index c1e8c9c..0072451 100644 --- a/hw4/assignment4.m +++ b/hw4/assignment4.m @@ -1,7 +1,7 @@ %% Assignment 4 % Name: Claudio Maggioni % -% Date: 19/3/2019 +% Date: 2020-05-17 % % This is a template file for the first assignment to get started with running % and publishing code in Matlab. Each problem has its own section (delineated @@ -11,11 +11,13 @@ % To generate a pdf for submission in your current directory, use the following % three lines of code at the command window: % -% >> options.format = 'pdf'; options.outputDir = pwd; publish('assignment3.m', options) +% >> options.format = 'pdf'; options.outputDir = pwd; publish('assignment4.m', options) % %% Problem 3 -clear; +clear all; +clc; +clf reset; n=10; f = @(x) (exp(-(x^2)/2)/sqrt(2*pi)); @@ -36,12 +38,21 @@ p_10 = computePolypoints(f, xe, x_10, 10); p_5c = computePolypoints(f, xe, x_5c, 5); p_10c = computePolypoints(f, xe, x_10c, 10); -figure; -subplot(1,2,1); plot(xe, p_5, xe, p_10, xe, y); -subplot(1,2,2); +figure; plot(xe, p_5c, xe, p_10c, xe, y); +%% Question 6 + +y = [-0.0044; -0.0771; -0.2001; -0.3521; -0.3520; 0; 0.5741; 0.8673; ... + 0.5741; 0; 0.3520; -0.3521; 0.2001; -0.0771; -0.0213; -0.0044]; +figure; +alpha = b3interpolate(y); +x = (0:0.01:16)'; +y_c = spline_curve(alpha, x); +plot(x, y_c); + +%% Question 3 (continued) function x = computeEquidistantXs(n) x = zeros(2*n+1,1); for i = 1:2*n+1 @@ -88,3 +99,46 @@ function p = HornerNewton(N, x, xe) p = p .* (xe - x(i)) + N(i); end end + +%% Problem 6 (continued) + +% assuming x_i = i - 1 +function [alpha] = b3interpolate(y) + n = size(y, 1); + A = zeros(n+2); + B = [y; 0; 0]; + for x_i = 0:n-1 + for j = -1:n + %fprintf("A(%d, %d) = B3(%d - %d)\n", x_i + 1, j+2, x_i, j); + A(x_i + 1, j + 2) = B3(x_i - j); + end + end + A(n+1, 1:3) = [1 -2 1]; + A(n+2, n-1:n+1) = [1 -2 1]; + alpha = A \ B; +end + +function [v] = spline_curve(alpha, x) + n = size(alpha, 1) - 2; + v = zeros(size(x)); + for i = 1:size(x,1) + for j = -1:n + v(i) = v(i) + alpha(j + 2) * B3(x(i) - j); + end + end +end + +function [y] = B3(x) +y = zeros (size (x)); +i1 = find (-2 < x & x < -1); +i2 = find (-1 <= x & x < 1); +i3 = find (1 <= x & x < 2); + +y(i1) = 0.5 * (x(i1) + 2) .^ 3; +y(i2) = 0.5 * (3 * abs (x(i2)) .^ 3 - 6 * x(i2) .^ 2 + 4); +y(i3) = 0.5 * (2 - x(i3)) .^ 3; +y = y / 3; +end + + + diff --git a/hw4/assignment4.pdf b/hw4/assignment4.pdf new file mode 100644 index 0000000..b1929fb Binary files /dev/null and b/hw4/assignment4.pdf differ diff --git a/hw4/hw4.pdf b/hw4/hw4.pdf index 376cbc4..5023d69 100644 Binary files a/hw4/hw4.pdf and b/hw4/hw4.pdf differ diff --git a/hw4/hw4.tex b/hw4/hw4.tex index a2af726..71619c9 100644 --- a/hw4/hw4.tex +++ b/hw4/hw4.tex @@ -89,7 +89,7 @@ $$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), (1, y_1),(3/2, y_2),(2, y_3)$. +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: @@ -100,40 +100,38 @@ $$y_0 = s\left(\frac{1}{2}\right) = a_{-1}B_2\left(\frac{1}{2} + 1\right) + a_{0 + 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(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(\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) + $$$$ +$$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_3 = 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_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}{8} \cdot +\[\frac{1}{2} \cdot \begin{bmatrix} -4&4&0&0\\ -1&6&1&0\\ -0&4&4&0\\ -0&1&6&1\\ +0&1&1&0\\ +0&0&1&1\\ +1&0&0&1\\ +1&1&0&0\\ \end{bmatrix} \begin{bmatrix} -a_0\\a_1\\a_2\\a_{-1}\\ +a_{-1}\\a_0\\a_1\\a_2\\ \end{bmatrix} = \begin{bmatrix} @@ -155,9 +153,21 @@ $$y_0 = s(0) = a_{-1}B_2\left(1\right) + a_{0}B_2\left(0\right) + a_{1}B_2\left( 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) = \frac{1}{8} a_{0} + \frac{3}{4} a_{1} + \frac{1}{8} a_{2}$$ +$$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) = \frac{1}{8} a_{1} + \frac{3}{4} a_{2} + \frac{1}{8} a_{-1}$$ +$$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) @@ -184,7 +194,7 @@ a_0\\a_1\\a_2\\a_{-1}\\ y_0\\y_1\\y_2\\y_{3}\\ \end{bmatrix}\] -\subsection*{Question 5} +\section*{Question 5} $$1 = y_0 = s(x_0) = 0 = a_{-1}B_3(1) +a_0B_3(0) +a_1B_3(-1)