In: Advanced Math
Exercise 3
Part 1. Solving a system Ax = b **Create a function in MATLAB that
begins with: function [C,N]=solvesys(A) [~,n]=size(A);
b=fix(10*rand(n,1)) format long We are using format long to display
a number in exponent format with 15 digit mantissas. If later on,
you will need to switch back to the default format, type and run
format The input is an matrix A. If A is invertible, the outputs
are the matrix C, whose 3 columns x1, x2, x3 are the solutions
obtained by the three methods described above, respectively, and N
is the vector whose 3 entries are the 2-norms of the vectors of the
differences between each two solutions.
**First, check if A is invertible. If A is not invertible, the function returns an empty matrix C=[ ] and an empty vector N=[ ]. Also, in this case, there are two possibilities for the system Ax = b: either “the system is inconsistent” or “the solution is not unique”. Use the command rank to check it on the two possible cases and program the two corresponding output messages. After that, terminate the program.
If A is invertible, the function solves the equation Ax = b using the three methods described above and gives the output vectors x1, x2, x3 for each of the methods (1)-(3), respectively. The vectors have to be the columns of the matrix C. Thus, we assign: C=[x1, x2, x3 ];
**The function [C,N]=solvesys(A) also returns a column vector N=[n1;n2;n3]; where n1=norm(x1-x2); n2=norm(x2-x3); n3=norm(x3-x1); The entries of the vector N are the 2-norms of the vectors of the differences between each two distinct solutions. Each entry is calculated by using a built-in MATLAB function norm, which is a square root of the sum of squares of the entries of the vector. The vector N gives an idea how “different” are the solutions obtained by various methods.
**Type the function solvesys in your Live Script.
**Run the function [C,N]=solvesys(A) for the following choices of the matrix A:
(a) A = magic(6); (b) A = magic(7); (c) A = eye(4); % Write a comment on the output for part (c) by comparing the solution with the vector b. (d) A = randi(20,4), (e) A = magic(3); (f) A = hilb(7)
Part 2. Condition numbers **Find the condition numbers of the matrices A=magic(7) and A=hilb(7) c1=cond(magic(7)) c2=cond(hilb(7))
% Compare c1 and c2 with number 1 and explain in your dairy file why the norms of the differences between the solutions for the coefficient matrix in part (f) are so big compared with the ones for the matrix in part (b).
**Explore the sensitivity of a badly conditioned matrix hilb(7): Input: A=hilb(7); Run the following: b=ones(7,1); x=A\b; b1=b+0.01; y=A\b1; norm(x-y) c3=rcond(A) %Using the output c3, which is the reciprocal condition number, explain why the system with the coefficient matrix hilb(7) is sensitive to perturbations.
**Re-run the code above for: A=magic(7);
%Comment on sensitivity to perturbations of magic(7) compared with hilb(7) by analyzing the corresponding outputs for the norm(x-y) and c3.
MATLAB CODE
1) file solvesys.m
function [C,N] = solvesys(A)
format long
%Storing the dimension of A in n and m
[n,m]=size(A);
%Calculating daterminant of A
magnitude=det(A);
%Calculating b
N=fix(10*rand(n,1))
%condition
if magnitude==0
disp('The system is either incosistent or the solution is not
unique');
C=[0;0;0]
N=[0;0;0]
else
%Calculating x1,x2,x3
C=A\N
end
%calculating n1,n2,n3
n1=norm(C(1)-C(2));
n2=norm(C(2)-C(3));
n3=norm(C(3)-C(1));
N=[n1; n2; n3]
end
2) File tests.m to call solvesys function
clc;
clear all;
%declaring the values of matrix A
disp('For A=magic(6)');
A=magic(6);
solvesys(A)
disp('For A=eye(6)');
solvesys(eye(5))
disp('For A=randi(20,4,4)')
solvesys(randi(20,4,4))
disp('For A=magic(3)')
solvesys(magic(3))
disp('For A=hilb(6)');
format rat, A=hilb(6);
solvesys(A)
OUTPUT:
Thanks!