Question
Submit your m-file and a diary that shows how you tested the code. Modify csolve.m so that it outputs pivot and free. Then call upon
Submit your m-file and a diary that shows how you tested the code. Modify csolve.m so that it outputs pivot and free. Then call upon this function in csolvefull.m. Submit the m-file for csolvefull.m, but not csolve.m.
Create a function csolvefull.m with input matrix A and a column vector b and no output. The function should display the rank, the pivot and free variables of A, the particular solution to Ax=b, and the special solutions for A. Test it using the system below.
A= ?11 5 8 2 6
?4 0 2 ?2 ?5
?15 ?3 6 ?12 ?8
?5 1 3 ?1 7
b=5
6
2
9
Your display should be precisely the following:
>> csolvefull(A,b)
The rank of the coefficient matrix is 3.
Pivot variables: 1 2 5
Free variables: 3 4
The particular solution is:
xp =
-0.4413
-0.0829
0
0
0.3981
The special solutions are:
xs =
0.5000 -0.5000
-0.5000 -1.5000
1.0000 0
0 1.0000
0.0000 0.0000
CSOLVE.M CODE
% This function finds the rank, pivot variables, and free variables of a % matrix. % Input: m x n matrix A. % Output: Text stating the rank, pivot, and free variables of the matrix A. function csolve(A) [m,n] = size(A); pivot = []; % initialize "pivot" free = 1:1:n; % initialize "free" % We initialize by none of the variables being pivot, and all of them being % free. This is because we will find the pivots in the matrix R, which will % then tell us what indices to store as pivot. The indices that are not % pivot are free. R = rref(A); % rref(A) returns reduced row echlon form of A % Fix a row. Then go through the columns. % The first nonzero element will be a 1, and it is the pivot. % Once this is found, we should go to the next row. for i = 1:m % visit every row for j = 1:n % visit every column if R(i,j) == 1 % check for a leading 1 pivot = [pivot,j]; % save the pivot column break; % terminate the inner for loop end end end % Now "pivot" stores the indexes of all the pivot variables. % The indexes not in "pivot" are the free variables. % Remove the elements in "pivot" from "free". free([pivot]) = ''; % Print the results. fprintf('The rank of the coefficient matrix is %d. ', length(pivot)); fprintf('Pivot variables:'); for i = 1:length(pivot) fprintf(' %d', pivot(i)); end fprintf(' '); fprintf('Free variables:'); for i = 1:length(free) fprintf(' %d', free(i)); end fprintf(' '); end
HINT
Pseudo-code:
% Store the particular solution in xp
initialize xp % zero column vector with appropriate dimension
find pivot and free columns of A % use your csolve.m function
generate pA % pA consists of the pivot columns from A
tempa = pA\b; % actual MATLAB code. This solves the equation
% pA*tempa = b.
record tempa in the appropriate indexes of xp
% Store the special solutions in a matrix xs, with appropriate
% dimensions. The ith column of xs will store the solution to Ax=0,
% where the ith free variable is set equal to 1, and the others
% are set equal to 0.
% We already have pivot and free from above.
initialize xs % zero matrix with appropriate dimensions
% We already have pA from above.
visit every free variable
% set the ith free variable equal to 1 in the ith special solution
% and solve the resulting equation
tempb = -(the ith free column of A); % This is the right hand side
% when the ith free variable
% is 1.
xs(...) = pA\tempb; % actual MATLAB code. This solves the equation
% pA*xs(...) = b. Store the solution in
% the appropriate indexes in xs.
xs(...) = 1; % set the ith free variable equal to one. The other
% free variables are already 0 due to the
% initialization.
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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