Modify mySolve. m to make sure the inputs are valid. Your function should check
ID: 3792630 • Letter: M
Question
Modify mySolve. m to make sure the inputs are valid. Your function should check for each of the following cases: 1. A is not a square matrix; 2. b is not a column vector; 3. Ax and b do not have the same dimension. Submit your m-file and a diary showing how you tested the code. Only submit the m-file for mySolve. m. Do not submit the m-files for backward. m, forward. m, or MYLU. m. Test to show that each error code works and that the program still works using the follow tests. (1) To test the error of A not being a square matrix use A = (1 2 3 4 5 6), b = (1 2 3). To test the error of b not being a column vector use A = (1 2 3 4), b = (1 2). To test the error of Ax and b not having compatible dimensions use A = (1 2 3 4), b = (1 2 3 4). To test that the program still works given no errors use A = (1 2 3 4), b = (1 2). Then make sure that Ax = b.Explanation / Answer
The modified m-file mySolve.m
function x = mySolve(A,b)
% code to test the error of A not being a square matrix
if(size(A,1)~=size(A,2))
fprintf('Error: The Matrix A is not a square matrix ');
return;
% Code to test the error of b not being a column vector
elseif(size(b,1) == 1 && size(b,2)~=1)
fprintf('Error: The vector b is not a column vectror ');
return;
% Code to test the error of Ax and b not having compatible dimensions
elseif(size(A,1)~= size(b,1))
fprintf('Error: dimension mismatch, dimensions of A and b must be compatible ');
return
% Code to test the program still works given no errors
else
x = A; % Or add the original code of mySolve.m here to solve Ax =b
end
end
Testing code for the given test conditions
1)
>> A = [1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> b = [1;2;3]
b =
1
2
3
>> mySolve(A,b)
Error: The Matrix A is not a square matrix
2)
>> A = [1 2;3 4]
A =
1 2
3 4
>> b = [1 2]
b =
1 2
>> mySolve(A,b)
Error: The vector b is not a column vectror
3)
>> A = [1 2;3 4]
A =
1 2
3 4
>> b = [1;2;3;4]
b =
1
2
3
4
>> mySolve(A,b)
Error: dimension mismatch, dimensions of A and b must be compatible
4)
>> A = [1 2;3 4]
A =
1 2
3 4
>> b = [1;2]
b =
1
2
>> mySolve(A,b)
ans =
0
0.5000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.