Question
Write a MATLAB function pvss that takes as input the coefficient matrix and right hand side of a linear system and returns the parametric vector
Write a MATLAB function pvss that takes as input the coefficient matrix and right hand side of a linear system and returns the parametric vector form of the solution in the following format:
function [sscount, p, V] = pvss(A,b)
sscount = -1 if the system is inconsistent (signaled by a pivot in the last column of rref([A b]).
sscount = 0 if the system has a unique solution (signaled by a lack of free variables)
sscount is the number of free variables of the system (number of columns of V) otherwise
p is the particular solution corresponding to all free variables being equal to zero (empty vector if sscount=-1),
V is the matrix whose columns are homogeneous solutions (i.e. solutins of ) corresponding to one of the free variables equal to 1 while the others are 0 (empty list of columns if sscount <= 0).
The provided template provides code to implement pvss except for the determination of V which can be implemented by completing four four lines in the code marked "UPDATE".
I ALREADY WROTE CODE THAT WORKS FOR THE MOST PART BUT THAT RETURNS THE WRONG VALUE FOR THE VARIABLE SSCOUNT WHEN THE INPUT IS A=[1 2 3; 4 5 6; 7 8 9] and b=[1; 1; 1], BUT IT DOES PRODUCE THE CORRECT OUTPUT WHEN THE INPUT IS A=[1 2 3; 4 5 6; 7 8 9] and b=[2; 1; 1].
below is the code I wrote that needs to be fixed:
function [sscount, p, V] = pvss(A,b)
% For the linear system Ax=b finds the parametric vector form of the solution.
% If no solutions sscount=-1; otherwise, sscount is number of free variables
% p is the particular solution with the free variables equal to zero (empty if sscount== -1)
% ith column of V is the homogeneous solution corresponding to the ith free variable (empty if sscount <= 0)
Augmented = [A b];
[m, n] = size(Augmented);
[R, pclist] = rref(Augmented); % rreduced row echelon form and pivot column list
r = size(pclist,2); % number of pivot columns (rank(A))
% CASE OF AN INCONSISTENT SYSTEM
if r>0 && pclist(r)==n
sscount = -1;
p = zeros(n-1,0);
V = zeros(n-1,0);
return
end
% OBTAIN THE PARTICULAR SOLUTION
p = zeros(n-1,1);
p(pclist) = R(1:r,n);
% OBTAIN THE HOMOGENEOUS SOLUTIONS CORRESPONDING TO EACH FREE VARIABLE (IF ANY)
sscount = n-r; % determine number of free variables (sscount)
if sscount <= 0
V = zeros(n-1, 0);
return
end
fvlist = setdiff(1:n-1, pclist); % find the list of free variables
V = zeros(n-1, sscount-1); % initialize V
V(pclist,1:r) = eye(r); % copy appropriately pivot rows of V from identity matrix
V(fvlist,:) = -R(r+1:m, pclist) * V(pclist,:); % set free rows of V appropriately
%sscount = sscount - 1;
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started