Then create a general solver to solve any linear system of n equations and n unk
ID: 3789552 • Letter: T
Question
Then create a general solver to solve any linear system of n equations and n unknowns of the form AX = b. Allow the user to input the n×n matrix A and n×1 column vector b. Use X = A to solve the system in your script and display the solution to the user. Try your solver with A = randi(50, 10), b = randi(20, 10, 1) and A = randi(3, 500), b = randi(7, 500, 1). Solve the system of equations: z -2y 232 52 Define A as the matrix of coefficients, X y, zl and b is a column vector representing the right- hand side of these equations. 3 7 -61 4 1 2 23 Recall the matrix form of this equation is AX b with solution X A 1b 3 7 -6 1 -2 23 -3 La L-6 0 5
Explanation / Answer
ex.m
function x=y(A,B)
% Get the size of the matrix A and B
[m,n] = size(A);
[i,j] = size(B);
% Check if the dimension matches
if(n ~= i)
fprintf('Matrix dimension does not match ');
else
% Start the tic.
tic;
fprintf('Using A\B ');
x = AB
fprintf('Using inv(A)*B ');
x = inv(A)*B
timeToComplete = toc
end
end
OUTPUT:
>> A = [3 7 6;1 -2 23;-6 0 5];
>> B = [4; -3; 2 ];
>> ex(A,B)
Using AB
x =
-0.3726
0.7715
-0.0471
Using inv(A)*B
x =
-0.3726
0.7715
-0.0471
timeToComplete =
4.1055e-004
ans =
-0.3726
0.7715
-0.0471
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.