hw4: done ex1

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-14 15:52:49 +02:00
parent d897a331eb
commit 7f310bd0de
3 changed files with 142 additions and 0 deletions

88
hw4/assignment4.m Normal file
View File

@ -0,0 +1,88 @@
%% Assignment 2
% Name: Claudio Maggioni
%
% Date: 19/3/2019
%
% 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
% by |%%|) and can be run in isolation by clicking into the particular section
% and pressing |Ctrl| + |Enter| (evaluate current section).
%
% 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)
%
%% Problem 3
format long
A = [4 3 2 1; 8 8 5 2; 16 12 10 5; 32 24 20 11];
[L,U,P] = pivotedOuterProductLU(A);
display(L);
display(U);
display(P);
%% Problem 6
[X,Y] = meshgrid((0:2000)/2000);
A = exp(-sqrt((X-Y).^2));
L = outerProductCholesky(A);
disp(norm(L*L'-A, 'fro'));
%% Problem 3 (continued)
function [L,U,P] = pivotedOuterProductLU(A)
dimensions = size(A);
n = dimensions(1);
p = 1:n;
L = zeros(n);
U = zeros(n);
for i = 1:n
values = A(:,i);
values(values == 0) = -Inf;
[~, p_k] = max(values);
k = find(p == p_k);
if k == -Inf
disp("Matrix is singular");
L = [];
U = [];
P = [];
return
end
p(k) = p(i);
p(i) = p_k;
L(:,i) = A(:,i) / A(p(i),i);
U(i,:) = A(p(i),:);
A = A - L(:,i) * U(i,:);
end
I = eye(n);
P = zeros(n);
for i = 1:n
P(:,i) = I(:,p(i));
end
P = transpose(P);
L = P * L;
end
%% Problem 6 (continued)
function [L] = outerProductCholesky(A)
if ~all(eig(A) >= 0)
disp("matrix is not positive definite");
L = [];
return;
end
dimensions = size(A);
n = dimensions(1);
L = zeros(n);
for i = 1:n
L(:, i) = A(:, i) / sqrt(A(i,i));
A = A - L(:, i) * transpose(L(:, i));
end
end

BIN
hw4/hw4.pdf Normal file

Binary file not shown.

54
hw4/hw4.tex Normal file
View File

@ -0,0 +1,54 @@
% 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)}|}{5!} =
\frac{\max_{x \in [-1,1]}|\frac{7680}{|2x+3|^6}|}{120} =
\max_{x \in [-1,1]}\frac{64}{|2x+3|^6} = 64$$
$$\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)}|}{5!} \max_{x \in [-1,1]} \left|(x - 1)\left(x - \frac{1}{2}\right)
\left(x + \frac{1}{2}\right)(x+1)\right| =$$$$= 64 \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$$
\end{document}