Mainly need help with (a) in setting up both the matrix A and the vector x in MA
ID: 3142072 • Letter: M
Question
Mainly need help with (a) in setting up both the matrix A and the vector x in MATLAB, but answering part (b) would also be nice if not too difficult to verify the work we did in the lecture:
(Mostly MATLAB) An n x n matrix A has entries a (a) Design a Matlab routine myA.m that constructs for arbitrary n the n x n matrix A, a column vector of size n with all of its entries equal to 1, and another column vector b defined by b Aa. (With b so defined, the exact solution to Aa b is the column vector a of ones (b) For each value of n in the sequence n 5.6 do the following. i. Call myA m to generate the corresponding A and b ii. Use the built-in Matlab command arc A b to obtain the computational solution arc to the system Aar b and display it iii. Using infinity norms throughout, compute and display the norms of the residual b acc and of the error E acc ac, where ar is the n-vector of ones and is the exact solution iv. Estimate and display the condition number K(A) by using the Matlab command condest GA,Inf) v. Compute and display the relative error IE and the relative residual Ir Allzcl) Does the error satisfy the relations IEI SK(A) A derived in class? How large can you take n before the error is 100%, i e., no significant digits remain in the solution? As n varies, how does the number of correct digits in the computed solution relate to the condition number of the matrix A? NOTE: Do not allow n to exceed 20Explanation / Answer
a) Filename: myA.m
function [A,x,b] = myA(n)
%MYA creates a square matrix of size n, A = (aij)
% aij = 5/(i+2j-1)
% x - column vector of size n with ones
% b = Ax
B = ones(n,1)*[1:n];
x = ones(n,1);
% aij = 5/(i+2j-1)
A = 5./(B'+2*B-ones(size(B)));
b = A*x;
end
(b) i) Using the routine myA.m
>> [A,b] = myA(5)
A =
2.5000 1.2500 0.8333 0.6250 0.5000
1.6667 1.0000 0.7143 0.5556 0.4545
1.2500 0.8333 0.6250 0.5000 0.4167
1.0000 0.7143 0.5556 0.4545 0.3846
0.8333 0.6250 0.5000 0.4167 0.3571
b =
5.7083
4.3911
3.6250
3.1090
2.7321
>> [A,b] = myA(6)
A =
2.5000 1.2500 0.8333 0.6250 0.5000 0.4167
1.6667 1.0000 0.7143 0.5556 0.4545 0.3846
1.2500 0.8333 0.6250 0.5000 0.4167 0.3571
1.0000 0.7143 0.5556 0.4545 0.3846 0.3333
0.8333 0.6250 0.5000 0.4167 0.3571 0.3125
0.7143 0.5556 0.4545 0.3846 0.3333 0.2941
b =
6.1250
4.7757
3.9821
3.4423
3.0446
2.7365
ii) Obtain computational solution xc = A
>> [A,b] = myA(5)
A =
2.5000 1.2500 0.8333 0.6250 0.5000
1.6667 1.0000 0.7143 0.5556 0.4545
1.2500 0.8333 0.6250 0.5000 0.4167
1.0000 0.7143 0.5556 0.4545 0.3846
0.8333 0.6250 0.5000 0.4167 0.3571
b =
5.7083
4.3911
3.6250
3.1090
2.7321
>> xc = A
xc =
1.0000
1.0000
1.0000
1.0000
1.0000
>> [A,b] = myA(6)
A =
2.5000 1.2500 0.8333 0.6250 0.5000 0.4167
1.6667 1.0000 0.7143 0.5556 0.4545 0.3846
1.2500 0.8333 0.6250 0.5000 0.4167 0.3571
1.0000 0.7143 0.5556 0.4545 0.3846 0.3333
0.8333 0.6250 0.5000 0.4167 0.3571 0.3125
0.7143 0.5556 0.4545 0.3846 0.3333 0.2941
b =
6.1250
4.7757
3.9821
3.4423
3.0446
2.7365
>> xc = A
xc =
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
>> [A,b] = myA(10)
A =
2.5000 1.2500 0.8333 0.6250 0.5000 0.4167 0.3571 0.3125 0.2778 0.2500
1.6667 1.0000 0.7143 0.5556 0.4545 0.3846 0.3333 0.2941 0.2632 0.2381
1.2500 0.8333 0.6250 0.5000 0.4167 0.3571 0.3125 0.2778 0.2500 0.2273
1.0000 0.7143 0.5556 0.4545 0.3846 0.3333 0.2941 0.2632 0.2381 0.2174
0.8333 0.6250 0.5000 0.4167 0.3571 0.3125 0.2778 0.2500 0.2273 0.2083
0.7143 0.5556 0.4545 0.3846 0.3333 0.2941 0.2632 0.2381 0.2174 0.2000
0.6250 0.5000 0.4167 0.3571 0.3125 0.2778 0.2500 0.2273 0.2083 0.1923
0.5556 0.4545 0.3846 0.3333 0.2941 0.2632 0.2381 0.2174 0.2000 0.1852
0.5000 0.4167 0.3571 0.3125 0.2778 0.2500 0.2273 0.2083 0.1923 0.1786
0.4545 0.3846 0.3333 0.2941 0.2632 0.2381 0.2174 0.2000 0.1852 0.1724
b =
7.3224
5.9044
5.0497
4.4551
4.0080
3.6551
3.3670
3.1260
2.9206
2.7429
>> xc = A
xc =
1.0000
1.0000
1.0000
1.0000
0.9999
1.0004
0.9993
1.0008
0.9995
1.0001
iii) Compute infinity norm of r = b-Axc and E = xc-x
>> r = b-A*xc
r =
1.0e-15 *
0
0
-0.8882
0
0.8882
0
0
0.4441
0.4441
0
norm(r,inf)
ans =
8.8818e-16
E = xc- ones(size(10))
E =
1.0e-03 *
-0.0000
0.0000
-0.0015
0.0187
-0.1133
0.3755
-0.7168
0.7853
-0.4583
0.1104
>> norm(E,inf)
ans =
7.8535e-04
iv) Dispay condition number of A
>> k = condest(A)
k =
1.6894e+14
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.