# null

Null space of matrix

## Description

## Examples

### Null Space of Matrix

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 $\mathit{x}$ that satisfy $\mathrm{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 $\mathit{A}{\mathit{x}}_{1}=0$, within roundoff error.

x1 = null(A)

`x1 = `*3×2*
0.8165 -0.0000
-0.4082 -0.7071
-0.4082 0.7071

norm(A*x1)

ans = 4.2999e-16

Now calculate a rational basis for the null space. Confirm that $\mathit{A}{\mathit{x}}_{2}=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.

### Specify Tolerance for Null Space

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

### General Solution of Underdetermined System of Equations

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

Underdetermined linear systems $\mathrm{Ax}=\mathit{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 $\mathit{A}{\mathit{x}}_{0}=\mathit{b}$, where $\mathit{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 $\mathit{x}={\mathit{x}}_{0}+\mathrm{Ny}$, where:

$\mathit{N}$ is the null space of $\mathit{A}$.

$\mathit{y}$ is any vector of proper length.

${\mathit{x}}_{0}$ 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 $\mathrm{Ax}=\mathit{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

`A`

— Input matrix

matrix

Input matrix.

**Data Types: **`single`

| `double`

**Complex Number Support: **Yes

`tol`

— Singular value tolerance

scalar

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

`Z`

— Null space basis vectors

matrix

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

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Generated code might return a different basis than MATLAB

^{®}.Code generation does not support the rational basis option (second input).

Code generation does not support sparse matrix inputs for this function.

### Thread-Based Environment

Run code in the background using MATLAB® `backgroundPool`

or accelerate code with Parallel Computing Toolbox™ `ThreadPool`

.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes and limitations:

The syntax

`Z = null(A,"rational")`

is not supported.

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

### Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

Usage notes and limitations:

The syntax

`Z = null(A,"rational")`

is not supported.

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

## Version History

**Introduced before R2006a**

### R2022a: Specify tolerance

Use the `tol`

argument to specify a tolerance threshold for the
singular values used to form the null space. Singular values of the input matrix less than
the tolerance are treated as zero.

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)