Modify mySolve m to make sure the inputs are valid Your func
Solution
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\\b; % 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

