Write a function with header [N, x] = myNumSols (A, b), where A and b are a matr
ID: 3678999 • Letter: W
Question
Write a function with header [N, x] = myNumSols (A, b), where A and b are a matrix and compatibly-sized column vector, respectively; N is the number of solutions of the system Ax = b; and x is a solution to the same system. If there are no solutions to the system of equations, then x should be an empty matrix. If there is one or an infinite number of solutions, then myNumSols should return one using the methods described in the chapter. You may assume that b is a column vector and that the number of elements in b is the same as the number of rows in A. The output x should be a column vector. You may assume that if the system has an infinite number of solutions, then the A matrix will have more columns than rows; that is, A is fat. In this case, you should solve the system using x = pinv (A) *b.Explanation / Answer
matlab function file:
myNumSols.m
function [N,x]=myNumSols(A,b)
[r,c]=size(A);
if r==c
x=inv(A)*b;
N=1;
else
x=pinv(A)*b;
if isempty(x)
N=0;
else
N=Inf;
end
end
end
%instructions on command line of matlab
>> A=reshape(1:15,3,5);
>> b=[-5;-4;-3];
>> [N,x]=myNumSols(A,b)
N =
Inf
x =
1.0000
0.6000
0.2000
-0.2000
-0.6000
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.