51 lines
1.0 KiB
Matlab
51 lines
1.0 KiB
Matlab
%% Homework 2 - Optimization Methods
|
|
% Author: Claudio Maggioni
|
|
%
|
|
% Sources:
|
|
|
|
clear
|
|
clc
|
|
close all
|
|
|
|
[A, b] = build_poisson(4);
|
|
disp(A)
|
|
disp(b)
|
|
|
|
% 1.1
|
|
function [A,b] = build_poisson(n)
|
|
A = diag(2 * ones(1,n));
|
|
|
|
A(1,1) = 1;
|
|
A(n,n) = 1;
|
|
|
|
|
|
for i = 2:n-1
|
|
A(i, i+1) = -1;
|
|
A(i, i-1) = -1;
|
|
end
|
|
|
|
h = 1 / (n - 1);
|
|
|
|
b = h^2 * ones(n, 1);
|
|
b(1) = 0;
|
|
b(n) = 0;
|
|
end
|
|
|
|
%% 1.1 (check this)
|
|
|
|
% Answer is a energy function does not exist. Since A is not symmetric
|
|
% (even if it is pd), the minimizer used for the c.g. method
|
|
% (i.e. (1/2)x^TAx - b^x) won't work
|
|
% since x^TAx might be negative and thus the minimizer does not point to
|
|
% the solution of Ax=B necessairly
|
|
|
|
%% 1.2
|
|
|
|
% we already enforce x(1) = x(n) = 0, since b(1) = b(n) = 0 and thus
|
|
% A(1, :) * x = b(0) = 0 and same for n can be solved only for x(1) = x(n)
|
|
% = 0
|
|
%
|
|
% The objective is therefore \phi(x) = (1/2)x^T\overline{A}x - b^x with a and b
|
|
% defined above, gradient is = \overline{A}x - b, hessian is = \overline{A}
|
|
|