Null Space Basis Calculator for MATLAB


MATLAB Null Space Basis Calculator

Generate MATLAB code for calculating a basis for a null space of a given matrix A.


Enter matrix elements. Use commas (,) for columns and semicolons (;) for rows. Example: [1, 2; 3, 4]


Orthonormal is numerically stable. Rational is often cleaner for integer matrices.

Generated MATLAB Code



Intermediate Analysis

Input Matrix Dimensions: N/A

Required Command: N/A

What is Calculating a Basis for a Null Space Using MATLAB?

In linear algebra, the null space (or kernel) of a matrix A is the set of all vectors x that satisfy the equation Ax = 0. Essentially, it’s the collection of all vectors that are mapped to the zero vector by the linear transformation represented by A. A basis for the null space is a set of linearly independent vectors that span the entire null space. Calculating a basis for a null space using MATLAB involves using built-in functions to find this set of vectors efficiently.

This process is fundamental in understanding the properties of a matrix. The dimension of the null space, known as the nullity, provides critical information about the solution sets of linear systems. Students, engineers, and scientists use this calculation to analyze systems, understand dependencies in data, and solve complex problems in various fields.

The MATLAB `null()` Function Formula and Explanation

MATLAB provides the `null()` function to compute a basis for the null space of a matrix. The function has two primary forms, each serving a different purpose.

1. Orthonormal Basis: `Z = null(A)`

This command computes an orthonormal basis for the null space of matrix A. An orthonormal basis consists of vectors that are mutually orthogonal (perpendicular) and have a length (norm) of 1. This is the most common and numerically stable method, suitable for most engineering and scientific applications.

2. Rational Basis: `Z = null(A, ‘r’)`

This command computes a “rational” basis. The results are often simpler, involving integers or simple fractions, making it excellent for academic and pedagogical purposes where understanding the underlying structure is more important than numerical precision.

MATLAB `null()` Function Variables
Variable Meaning Unit / Type Typical Range
A The input matrix for which the null space basis is calculated. m x n matrix Any real or complex matrix.
'r' An optional flag to request a rational basis instead of the default orthonormal basis. Character string (unitless) Either present (‘r’) or absent.
Z The output matrix whose columns form the basis for the null space of A. n x k matrix If the null space is only the zero vector, Z is an empty matrix. Otherwise, its columns are the basis vectors.

Practical Examples

Example 1: A 2×3 Matrix

Consider the following matrix A:

A = [1, 2, 3; 4, 5, 6]

Inputs: The matrix A is a 2×3 matrix. We want to find a basis for its null space.

MATLAB Code (Rational):

A = [1, 2, 3; 4, 5, 6];
Z = null(A, 'r');
% Z will be a 3x1 matrix, e.g., a vector like [1; -2; 1]

Result Interpretation: The result `Z` is a single vector, meaning the null space has a dimension of one. Any scalar multiple of this vector `c * [1; -2; 1]` is a solution to `Ax = 0`.

Example 2: A 3×3 Singular Matrix

Consider a singular (non-invertible) matrix B:

B = [1, 1, 2; 1, 1, 2; 1, 1, 2]

Inputs: The matrix B has linearly dependent rows.

MATLAB Code (Orthonormal):

B = [1, 1, 2; 1, 1, 2; 1, 1, 2];
Z = null(B);
% Z will be a 3x2 matrix with two orthonormal columns.

Result Interpretation: The nullity of B is 2. The two columns of `Z` form an orthonormal basis for this two-dimensional null space. Any linear combination of these two vectors will be in the null space of B. For information on related topics, see this article on the Rank–nullity theorem.

How to Use This Null Space Basis Calculator

  1. Enter Your Matrix: Type or paste your matrix into the “Matrix A” text area. Follow the specified format: use commas between elements in a row and semicolons to separate rows.
  2. Choose Basis Type: Select either “Orthonormal” for most numerical work or “Rational” for teaching and learning purposes. The generated code will update automatically.
  3. Review the Code: The text box below will instantly show the correct MATLAB code. You can see the exact command (`null(A)` or `null(A, ‘r’)`) that will be used.
  4. Copy and Paste: Click the “Copy Code” button to copy the generated MATLAB script to your clipboard. You can then paste it directly into your MATLAB command window or an M-file.
  5. Interpret Results: After running the code in MATLAB, the resulting matrix `Z` will contain the basis vectors as its columns. The number of columns is the nullity of your matrix.

Key Factors That Affect the Null Space

  • Matrix Rank: The rank of a matrix is the dimension of its column space. The Rank-Nullity Theorem states that for an m x n matrix A, `rank(A) + nullity(A) = n` (number of columns). A higher rank implies a smaller null space.
  • Linear Independence: If all columns of a matrix are linearly independent, the rank equals the number of columns, and the nullity is 0. This means the null space contains only the zero vector.
  • Matrix Dimensions (m x n): The dimensions determine the maximum possible rank and influence the size of the null space. The vectors in the null space will have `n` components.
  • Singular vs. Non-singular (Square Matrices): A square matrix is non-singular (invertible) if and only if its nullity is 0. A singular matrix has a non-trivial null space (nullity > 0).
  • Numerical Precision: When using numerical software like MATLAB, floating-point inaccuracies can affect whether a singular value is treated as zero. The `null(A)` command uses a tolerance to handle this, which can be adjusted with `null(A, tol)`.
  • Data Redundancy: In data science applications, the null space can represent redundancies or conserved quantities in a dataset modeled by the matrix. For more on this, see this guide to the Kernel in Linear Algebra.

Frequently Asked Questions (FAQ)

What does it mean if the result is an empty matrix?

An empty matrix `[]` as output means the null space is trivial; it contains only the zero vector. This occurs when the matrix has full column rank (its columns are linearly independent), and its nullity is 0.

What is the difference between an orthonormal and a rational basis?

An orthonormal basis consists of vectors that are mutually perpendicular and have a unit length. A rational basis, as produced by `null(A, ‘r’)`, consists of vectors with “nice” rational numbers, which is helpful for understanding the structure without complex decimals.

Is the null space a vector space?

Yes. The null space of any matrix is a subspace of Rn (where n is the number of columns in the matrix). It always contains the zero vector and is closed under addition and scalar multiplication.

How does `calculating a basis for a null space using matlab` relate to solving `Ax=b`?

The null space is key to understanding the full solution set of `Ax=b`. If `x_p` is one particular solution to `Ax=b`, then the complete solution set is `x_p + x_n`, where `x_n` is any vector from the null space of A.

Can I use this for non-square matrices?

Absolutely. The concept of a null space and the `null()` function in MATLAB apply to any m x n matrix, regardless of whether it is square or rectangular.

Why does the rational basis `null(A, ‘r’)` look so different from the orthonormal one?

They are bases for the same space, but they “point” in different directions. The orthonormal basis vectors are normalized to have a length of 1 and be mutually perpendicular. The rational basis vectors are scaled to have simple integer or fractional components, but are typically not orthogonal or unit length. You can find more details in this MATLAB null function guide.

What is nullity?

Nullity is the dimension of the null space, which is simply the number of vectors in its basis. This calculator automatically finds the basis, and the number of columns in the resulting MATLAB matrix ‘Z’ is the nullity. You can explore this concept in depth through resources on the Rank-Nullity Theorem.

Does the order of rows or columns affect the null space?

Swapping rows does not change the null space. However, swapping columns corresponds to reordering the variables in the vector `x`, which will reorder the components of the resulting basis vectors.

Related Tools and Internal Resources

© 2026 SEO Tools Inc. All Rights Reserved.


Leave a Reply

Your email address will not be published. Required fields are marked *