-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainFile.m
193 lines (159 loc) · 6.47 KB
/
mainFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
% 2019862s
% Assessed Coursework 1: Least Squares Fitting
%% Reduced QR via Householder triangularization
% Provide matrix A in the console
A=input('Enter the Matrix A: \n');
[houseQ,houseR] = reducedQRHouseholder(A);
%% Least Squares Question 5 - Run this to compare three methods
format long
m = 50;
n = 12;
t = linspace(0,1,m);
V = vander(t);
V = fliplr(V);
A = zeros(m,n);
for i=1:n,
A(:,i) = V(:,i);
end
b = sin(5*t')
%%%%%%%%%%%%% QR via Householder %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute reduced QR factorization by Householder
[houseQ, houseR] = reducedQRHouseholder(A);
% Conjugate transpose of reducedQ
conjQ = (conj(houseQ)');
% Perform backward substitution
householdery = (conjQ)*b;
householderx = backSubstitution(houseR,householdery)
%%%%%%%%%%%%% Gram-Schmidt %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute QR factorization by Gram-Schmidt
[gramSchmidtQ,gramSchmidtR]=modifiedGramSchmidt(A);
% Conjugate transpose of reducedQ
conjGramSchmidtQ = (conj(gramSchmidtQ'));
% Perform backward substitution
gramSchmidty = (conjGramSchmidtQ)*b;
gramSchmidtx = backSubstitution(gramSchmidtR,gramSchmidty)
%%%%%%%%%%%%% Normal Equations via Cholesky %%%%%%%%%%%%%%%%%%%%%%
% Compute Aconj(A) and conj(A)b
prodA = (conj(A'))*A;
choleskyy = (conj(A'))*b;
% Compute Cholesky factorization of A
[choleskyR] = (cholesky(prodA));
% Solve lower triangular system using forward substitution
w = forwSubstitution((conj(choleskyR')),choleskyy);
choleskyx = backSubstitution(choleskyR,w)
%% Absolute Error
m = 50;
t = linspace(0,1,m);
absoluteErrorHouseholder = abs(A*householderx-b);
absoluteErrorGramSchmidt = abs(A*gramSchmidtx-b);
absoluteErrorCholesky = abs(A*choleskyx-b);
fig1=figure
plot(t, absoluteErrorHouseholder,'r');
hold on
plot(t, absoluteErrorGramSchmidt,'k--');
plot(t, absoluteErrorCholesky,'k');
title('Absolute Errors vs. t for m=50, n=12');
xlabel('t');
ylabel('Absolute Error for each Method');
legend('Householder','Gram-Schmidt','Cholesky','Location','northwest');
print(fig1,'/Users/yanastaneva/Library/Mobile Documents/com~apple~CloudDocs/Year 5/Numerical Methods/Assignments/AC1/absoluteError.jpeg','-djpeg');
figure(fig1);
fig2=figure
semilogy(t, absoluteErrorHouseholder,'r');
hold on
semilogy(t, absoluteErrorGramSchmidt,'k--');
semilogy(t, absoluteErrorCholesky,'k');
title('Absolute Errors in log scale vs. t');
xlabel('t');
ylabel('Absolute Error in log scale for each Method');
legend('Householder','Gram-Schmidt','Cholesky','Location','northwest');
print(fig2,'/Users/yanastaneva/Library/Mobile Documents/com~apple~CloudDocs/Year 5/Numerical Methods/Assignments/AC1/absoluteErrorLog.jpeg','-djpeg');
figure(fig2);
%% Relative Error
format long
m = 50;
t = linspace(0,1,m);
b = sin(5*t');
V = vander(t);
V = fliplr(V);
for n = 5:30,
A = V(:,1:n);
%%%%%%%%%%%%% QR via Householder %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute reduced QR factorization by Householder
[houseQ, houseR] = reducedQRHouseholder(A);
% Conjugate transpose of reducedQ
conjQ = (conj(houseQ)');
% Perform backward substitution
householdery = (conjQ)*b;
householderx = backSubstitution(houseR,householdery);
%%%%%%%%%%%%% QR via Gram-Schmidt %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute QR factorization by Gram-Schmidt
[gramSchmidtQ,gramSchmidtR]=modifiedGramSchmidt(A);
% Conjugate transpose of reducedQ
conjGramSchmidtQ = (conj(gramSchmidtQ'));
% Perform backward substitution
gramSchmidty = (conjGramSchmidtQ)*b;
gramSchmidtx = backSubstitution(gramSchmidtR,gramSchmidty);
%%%%%%%%%%%%% Normal Equations via Cholesky %%%%%%%%%%%%%%%%%%%%%%
% Compute Aconj(A) and conj(A)b
prodA = (conj(A'))*A;
choleskyy = (conj(A'))*b;
% Compute Cholesky factorization of A
[choleskyR] = (cholesky(prodA));
% Solve lower triangular system using forward substitution
w = forwSubstitution((conj(choleskyR')),choleskyy);
choleskyx = backSubstitution(choleskyR,w);
relativeErrorHouseholder(n-4) = (norm((A*householderx-b),2))/(norm(b,2))
relativeErrorGramSchmidt(n-4) = (norm((A*gramSchmidtx-b),2))/(norm(b,2))
relativeErrorCholesky(n-4) = (norm((A*choleskyx-b),2))/(norm(b,2))
end
fig3=figure
plot(linspace(5,30,26), relativeErrorHouseholder,'r');
hold on
plot(linspace(5,30,26), relativeErrorGramSchmidt,'k--');
plot(linspace(5,30,26), relativeErrorCholesky,'k');
title('Relative Errors vs. n=[5,30]');
xlabel('n=[5,30]');
ylabel('Relative Error for each Method');
legend('Householder','Gram-Schmidt','Cholesky','Location','northwest');
print(fig3,'/Users/yanastaneva/Library/Mobile Documents/com~apple~CloudDocs/Year 5/Numerical Methods/Assignments/AC1/relativeError.jpeg','-djpeg');
fig4=figure
semilogy(linspace(5,30,26), relativeErrorHouseholder,'r');
hold on
semilogy(linspace(5,30,26), relativeErrorGramSchmidt,'k--');
semilogy(linspace(5,30,26), relativeErrorCholesky,'k');
title('Relative Errors in log scale vs. n=[5,30].');
xlabel('n=[5,30]');
ylabel('Relative Error in log scale for each Method');
legend('Householder','Gram-Schmidt','Cholesky','Location','northwest');
print(fig4,'/Users/yanastaneva/Library/Mobile Documents/com~apple~CloudDocs/Year 5/Numerical Methods/Assignments/AC1/relativeErrorLog.jpeg','-djpeg');
%% Condition number
format long
m = 50;
t = linspace(0,1,m);
b = sin(5*t');
V = vander(t);
V = fliplr(V);
for n = 5:30,
A = V(:,1:n);
conditionNumberA(n-4)=cond(A);
conditionNumberAStarA(n-4)=cond((conj(A))'*A);
end
fig5=figure
plot(linspace(5,30,26), conditionNumberA,'r');
hold on
plot(linspace(5,30,26), conditionNumberAStarA,'k--');
title('Condition number of A vs. n=[5,30].');
xlabel('n=[5,30]');
ylabel('Condition Number of A for each Method');
legend('Condition Number of A','Condition Number of A*A','Location','northwest');
print(fig5,'/Users/yanastaneva/Library/Mobile Documents/com~apple~CloudDocs/Year 5/Numerical Methods/Assignments/AC1/conditionNumber.jpeg','-djpeg');
fig6=figure
semilogy(linspace(5,30,26), conditionNumberA,'r');
hold on
semilogy(linspace(5,30,26), conditionNumberAStarA,'k--');
title('Condition number of A in log scale vs. n=[5,30].');
xlabel('n=[5,30]');
ylabel('Condition Number of A for each Method');
legend('Condition Number of A','Condition Number of A*A','Location','northwest');
print(fig6,'/Users/yanastaneva/Library/Mobile Documents/com~apple~CloudDocs/Year 5/Numerical Methods/Assignments/AC1/conditionNumberLog.jpeg','-djpeg');