MATLAB function that returns complete solution to Ax=b

时间:2016-02-03 04:09:57

标签: matlab

I have a homework assignment that tasks me with writing a MATLAB function and I'm worried that I've missed something in my current answer. The function returns the complete solution to a linear equation of the form Ax=b, where A is a square matrix, and b is a vector of the appropriate dimension. The first line of the function is

function [Bs, Ns] = a(A, b)

where Bs is the basic solution (a vector), and Ns is the null solution - a matrix whose columns are a basis of the null space of A. There are also a few considerations in terms of the code used:

  • Code will be marked based on a set of test cases.
  • Built-in functions may be used in the code but it must be my original work.
  • Code that produces an error or warning, such as for a singular matrix, will be assigned a failing mark.
  • It can be assumed the test set will contain matrices that are all zero, non-singular, and otherwise rank deficient (complete solution exists, but MATLAB will produce an error or warning).

The code I've written is below.

function [Bs, Ns] = a(A, b)
ncols = size(A, 2);
x = pinv(A)*b;
Bs = x;
if ncols == rank(A)
    Ns = zeros(ncols,1);
else
    Ns = null(A);
end
end

The simplicity of my function has me worried that I've missed something (assignment is worth 4% of final grade) - either in my interpretation of the listed considerations, or that there are test cases which will cause errors/warnings. Any input would be appreciated.

0 个答案:

没有答案