Assignment 3 Scripts

 

P1

% Script P1
% Tests MaybeChol

n = 5;
randn('seed',0);
clc
A = randn(n,n); A = A'*A;

disp('A Positive Definite Case:')
format short
G = MaybeChol(A)

disp(' ')
disp('Nonpositive Definite Cases:')
disp(' ')
disp(' v''*A*v    norm v    v(1)     v(2)     v(3)     v(4)     v(5)')
disp('-----------------------------------------------------------------')
for k=1:n
   A0 = A; A0(k,k)=0;
   v = MaybeChol(A0);
   disp(sprintf('%8.4f ',v'*A0*v,norm(v),v))
end

P2

% Script P2
% Tests SpecialPD

randn('seed',0);
n = 100;
p = 5;
U = randn(n,p);
b = randn(n,1);

x = SpecialPD(U,b);
x0 = (eye(n,n)+U*U')\b;
err = norm(x-x0);
clc 
disp( sprintf('norm(SpecialPD(U,b) - (I+UU'')^{-1}b) = %10.3e',err))

disp(' ')
disp('Fill in the blanks. (Pencil is OK)')
disp(' ')
disp('1. My SpecialPD requires  O(n^___ times p^____) flops')
disp(' ')
disp(' ')
disp('2. If the largest and smallest singular values of U are 10 and 10^6, then')
disp('   I expect SpecialPD to produce an x with about ______ correct digits.')

P3

% Script P3
% Tests NearToep

n = 5;
randn('seed',0);
clc

disp('A well-conditioned example...')
r = randn(n,1); b = randn(n,1); x = randn(n,1);
s = NearToep(r,x,b);
e = norm(toeplitz(s+r)*x - b);
disp(' ')
disp('s'' = ')
disp(sprintf('   %10.6f',s))
disp(' ')
disp(sprintf('norm(T(r+s)*x - b) = %15.6e',e))

disp(' ')
disp(' ')
disp('A rank deficient example...')
r = randn(n,1); b = randn(n,1); x = ones(n,1);
s = NearToep(r,x,b);
e = norm(toeplitz(s+r)*x - b);
disp(' ')
disp('s'' = ')
disp(sprintf('   %10.6f',s))
disp(' ')
disp(sprintf('norm(T(r+s)*x - b) = %15.6e',e))

P4

% Script P4
% Tests DiagPiv

clc
randn('seed',0)

% Case 1
% A positive definite example. Only 1-by-1 pivots

disp('A positive definite example...')
disp(' ')
n = 6; A = randn(n,n); A = A'*A;
[L,D,P] = DiagPiv(A);
L = L
D = D
P = P
error = norm(P*A*P' - L*D*L')

% Case 2
% An indefinite example

A = randn(n,n); A = A + A';
disp('A nonpositive definite example...')
disp(' ')
[L,D,P] = DiagPiv(A);
L = L
D = D
P = P
error = norm(P*A*P' - L*D*L')