Main Content

null

Null space of matrix

Description

example

Z = null(A) returns an orthonormal basis for the null space of A.

example

Z = null(A,tol) also specifies a tolerance. Singular values of A less than tol are treated as zero, which can affect the number of columns in Z.

example

Z = null(A,"rational") returns a rational basis for the null space of A that is typically not orthonormal. If A is a small matrix with small integer elements, then the elements of Z are ratios of small integers. This method is numerically less accurate than null(A).

Examples

collapse all

Use the null function to calculate orthonormal and rational basis vectors for the null space of a matrix. The null space of a matrix contains vectors x that satisfy Ax=0.

Create a 3-by-3 matrix of ones. This matrix is rank deficient, with two of the singular values being equal to zero.

A = ones(3)
A = 3×3

     1     1     1
     1     1     1
     1     1     1

Calculate an orthonormal basis for the null space of A. Confirm that Ax1=0, within roundoff error.

x1 = null(A)
x1 = 3×2

   -0.5774   -0.5774
   -0.2113    0.7887
    0.7887   -0.2113

norm(A*x1)
ans = 9.6148e-17

Now calculate a rational basis for the null space. Confirm that Ax2=0.

x2 = null(A,"rational")
x2 = 3×2

    -1    -1
     1     0
     0     1

norm(A*x2)
ans = 0

x1 and x2 are similar but are normalized differently. While x1'*x1 is an identity matrix, x2'*x2 is not.

x1'*x1
ans = 2×2

    1.0000   -0.0000
   -0.0000    1.0000

x2'*x2
ans = 2×2

     2     1
     1     2

Orthogonality is often essential for accuracy of numerical computations. Therefore, the "rational" option should be used only when working on small all-integer matrices where it is useful for the output to be more readable.

When a matrix has small singular values, specify a tolerance to change which singular values are treated as zero.

Create a 7-by-7 Hilbert matrix. This matrix is full rank but has some small singular values.

H = hilb(7)
H = 7×7

    1.0000    0.5000    0.3333    0.2500    0.2000    0.1667    0.1429
    0.5000    0.3333    0.2500    0.2000    0.1667    0.1429    0.1250
    0.3333    0.2500    0.2000    0.1667    0.1429    0.1250    0.1111
    0.2500    0.2000    0.1667    0.1429    0.1250    0.1111    0.1000
    0.2000    0.1667    0.1429    0.1250    0.1111    0.1000    0.0909
    0.1667    0.1429    0.1250    0.1111    0.1000    0.0909    0.0833
    0.1429    0.1250    0.1111    0.1000    0.0909    0.0833    0.0769

s = svd(H)
s = 7×1

    1.6609
    0.2719
    0.0213
    0.0010
    0.0000
    0.0000
    0.0000

Calculate the null space of H. Because H is full rank, Z is empty.

Z = null(H)
Z =

  7x0 empty double matrix

Now, calculate the null space again, but specify a tolerance of 1e-4. This tolerance leads to null treating three of the singular values as zeros, so the null space is no longer empty.

Ztol = null(H,1e-4)
Ztol = 7×3

    0.0160   -0.0025    0.0002
   -0.2279    0.0618   -0.0098
    0.6288   -0.3487    0.0952
   -0.2004    0.6447   -0.3713
   -0.4970   -0.1744    0.6825
   -0.1849   -0.5436   -0.5910
    0.4808    0.3647    0.1944

Verify that H*Ztol has negligible elements compared to the specified tolerance.

norm(H*Ztol)
ans = 2.9386e-05

Find one particular solution to an underdetermined system, and then obtain the general form for all solutions.

Underdetermined linear systems Ax=b involve more unknowns than equations. An underdetermined system can have infinitely many solutions or no solution. When the system has infinitely many solutions, they all lie on a line. The points on the line are all obtained with linear combinations of the null space vectors.

Create a 2-by-4 coefficient matrix and use backslash to solve the equation Ax0=b, where b is a vector of ones. Backslash calculates a least-squares solution to the problem.

A = [1 8 15 67; 7 14 16 3]
A = 2×4

     1     8    15    67
     7    14    16     3

b = ones(2,1);
x0 = A\b
x0 = 4×1

         0
         0
    0.0623
    0.0010

The complete general solution to the underdetermined system has the form x=x0+Ny, where:

  • N is the null space of A.

  • y is any vector of proper length.

  • x0 is the solution computed by backslash.

Calculate the null space of A, and then use the result to construct another solution to the system of equations. Check that the new solution satisfies Ax=b, within roundoff error.

N = null(A)
N = 4×2

   -0.2977   -0.8970
   -0.6397    0.4397
    0.7044    0.0157
   -0.0769   -0.0426

x = x0 + N*[1; -2]
x = 4×1

    1.4963
   -1.5192
    0.7354
    0.0093

norm(A*x-b)
ans = 2.8908e-14

Input Arguments

collapse all

Input matrix.

Data Types: single | double
Complex Number Support: Yes

Singular value tolerance, specified as a real numeric scalar. Singular values of A less than the tolerance are treated as zero, which affects the number of null space vectors returned by null. The default tolerance is max(size(A)) * eps(norm(A)).

Output Arguments

collapse all

Null space basis vectors, returned in the columns of a matrix. Z satisfies the properties:

  • A*Z has negligible elements.

  • size(Z,2) is an estimate of the nullity of A.

If rank(A) (or rank(A,tol)) is equal to size(A,2), then Z is empty.

Algorithms

null(A) calculates the singular value decomposition of matrix A, such that A = U*S*V'. The columns of V corresponding to singular values equal to zero (within tolerance) form a set of orthonormal basis vectors for the null space.

The rational basis for the null space null(A,"rational") is obtained from the reduced row echelon form of A, as calculated by rref.

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also

| | |