Solve linear system of equations (2024)

Main Content

Solve linear system of equations

collapse all in page

Syntax

X = linsolve(A,B)

X = linsolve(A,B,opts)

[X,r] = linsolve(___)

Description

example

X = linsolve(A,B) solves the linear system AX = B using one of these methods:

  • When A is square, linsolve uses LU factorization with partial pivoting.

  • For all other cases, linsolve uses QR factorization with column pivoting.

linsolve warns if A is ill conditioned (for square matrices) or rank deficient (for rectangular matrices).

example

X = linsolve(A,B,opts) uses an appropriate solver as determined by the options structure opts. The fields in opts are logical values describing properties of the matrix A. For example, if A is an upper triangular matrix, you can set opts.UT = true to make linsolve use a solver designed for upper triangular matrices. linsolve does not test to verify that A has the properties specified in opts.

example

[X,r] = linsolve(___) also returns r, which is the reciprocal of the condition number of A (for square matrices) or the rank of A (for rectangular matrices). You can use any of the input argument combinations in previous syntaxes. With this syntax, linsolve does not warn if A is ill conditioned or rank deficient.

Examples

collapse all

Solve Linear System

Open Live Script

Solve a linear system with both mldivide and linsolve to compare performance.

mldivide is the recommended way to solve most linear systems of equations in MATLAB®. However, the function performs several checks on the input matrix to determine whether it has any special properties. If you know about the properties of the coefficient matrix ahead of time, then you can use linsolve to avoid time-consuming checks for large matrices.

Create a 10000-by-10000 magic square matrix and extract the lower triangular portion. Set the LT field of the opts structure to true to indicate that A is a lower triangular matrix.

A = tril(magic(1e4));opts.LT = true;

Create a vector of ones for the right-hand side of the linear equation Ax=b. The number of rows in A and b must be equal.

b = ones(size(A,2),1);

Solve the linear system Ax=b using mldivide and time the calculation.

t1 = 0.0551

Now, solve the system again using linsolve. Specify the options structure so that linsolve can select an appropriate solver for a lower triangular matrix.

ticx2 = linsolve(A,b,opts);t2 = toc
t2 = 0.0226

Compare the execution times to see how much faster linsolve is. As with any timing comparison, the results can vary between different computers and releases of MATLAB.

speedup = t1/t2
speedup = 2.4402

Suppress Matrix Condition Warnings

Open Live Script

Solve a linear system using linsolve with two outputs to suppress matrix conditioning warnings.

Create a 20-by-20 Hilbert test matrix. This matrix is nearly singular, with the largest singular value being about 2e18 larger than the smallest.

A = hilb(20);

Solve a linear system involving A with linsolve. Since A is nearly singular, linsolve returns a warning.

b = ones(20,1);x = linsolve(A,b);
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 5.628781e-20.

Now, solve the same linear system, but specify two outputs to linsolve. MATLAB® suppresses the warning, and the second output r contains the reciprocal condition number of A. You can use this syntax to handle ill-conditioned matrices with special cases in your code, without the code producing a warning.

[x,r] = linsolve(A,b)
x = 20×1109 × -0.0000 0.0000 -0.0004 0.0071 -0.0592 0.2819 -0.7821 1.1830 -0.7030 -0.1061 ⋮
r = 5.6288e-20

Input Arguments

collapse all

ACoefficient matrix
matrix

Coefficient matrix. A appears in the system of linear equations on the left as AX = B. The number of rows in A must equal the number of rows in B.

A cannot be sparse. To solve a linear system involving a sparse matrix, use mldivide or decomposition instead.

Data Types: single | double
Complex Number Support: Yes

BInput array
vector | matrix

Input array, specified as a vector or matrix. B appears in the system of linear equations on the right as AX = B. If B is a matrix, then each column in the matrix represents a different vector for the right-hand side.

The number of rows in A must equal the number of rows in B.

Data Types: single | double
Complex Number Support: Yes

optsCoefficient matrix properties
structure

Coefficient matrix properties, specified as a structure. Use this structure to specify properties of A that linsolve uses to select an appropriate solver for the linear system. The fields in the structure contain true/false values to indicate whether A has each property. By default all fields in the structure are assumed to be false. This table lists the possible fields in opts and their corresponding matrix properties.

FieldMatrix Property

LT

Lower triangular (nonzero values appearing only on or below the main diagonal)

UT

Upper triangular (nonzero values appearing only on or above the main diagonal)

UHESS

Upper Hessenberg (all zero values below the first subdiagonal)

SYM

Real symmetric or complex Hermitian (matrix equal to its transpose)

POSDEF

Positive definite (all positive eigenvalues)

RECT

Rectangular matrix (different number of rows and columns)

TRANSA

Conjugate transpose — Specifies whether the function solves A*X = B (when opts.TRANSA = false) or the transposed problem A'*X = B (when opts.TRANSA = true)

Example: opts.UT = true specifies that A is upper triangular.

Example: opts.SYM = true, opts.POSDEF = true sets two fields to specify that A is symmetric and positive definite.

Valid Combinations

The rows of this table list all combinations of field values in opts that are valid for linsolve. Empty cells are the default value of false, and a true/false entry indicates that linsolve accepts either value.

LT

UT

UHESS

SYM

POSDEF

RECT

TRANSA

A is lower triangular

true

true/false

true/false

A is upper triangular

true

true/false

true/false

A is upper Hessenberg

true

true/false

A is symmetric

true

true/false

true/false

A is rectangular

true/false

true/false

Notes on Usage

  • If A has the properties in opts, then linsolve is faster compared to mldivide, because linsolve invokes the appropriate solver immediately and does not perform any tests to verify that A has the specified properties.

  • If A does not have the properties that you specify in opts, then linsolve returns incorrect results and does not always return an error message. Therefore, if you are unsure whether A has the specified properties, use mldivide or decomposition instead.

Data Types: struct

Output Arguments

collapse all

X — Linear system solution
vector | matrix

Linear system solution, returned as a vector or matrix that satisfies AX = B (or ATX = B if opts.TRANSA = true). The size of X depends on whether opts.TRANSA = true:

  • If A is m-by-n and B is m-by-k, then X is n-by-k and is the solution to AX = B.

  • If opts.TRANSA = true, then A is m-by-n and B is n-by-k. In this case, X is m-by-k and is the solution to ATX = B.

r — Reciprocal condition number or rank
scalar

Reciprocal condition number or rank, returned as a scalar.

  • If A is a square matrix, then r is the reciprocal condition number of A.

  • If A is a rectangular matrix, then r is the rank of A.

  • If opts is specified, then r is the reciprocal of the condition number of A unless RECT is true and both LT and UT are false, in which case, r gives the rank of A.

Tips

  • The speed benefit of linsolve can vary depending on the matrix structure and the relative optimization of the underlying algorithms. In some cases (such as with small matrices) there might not be any speed-up compared to mldivide. The speed benefit with linsolve arises by avoiding costly checks on the properties of large matrices, or by choosing an algorithm that is better suited to the input than the choice that mldivide makes.

Extended Capabilities

Version History

Introduced before R2006a

See Also

mldivide | decomposition | lsqminnorm

Topics

  • Systems of Linear Equations

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.

Solve linear system of equations (1)

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:

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
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Solve linear system of equations (2024)

FAQs

How do you solve a system of linear equations? ›

To solve a system of equations using substitution:
  1. Isolate one of the two variables in one of the equations.
  2. Substitute the expression that is equal to the isolated variable from Step 1 into the other equation. ...
  3. Solve the linear equation for the remaining variable.

How do I solve for a linear equation? ›

To solve linear equations graphically, first graph both equations in the same coordinate system and check for the intersection point in the graph. For example, take two equations as 2x + 3y = 9 and x – y = 3. Now, to plot the graph, consider x = {0. 1, 2, 3, 4} and solve for y.

What is the easiest way to solve a linear system? ›

Methods for solving systems of linear equations

All we need to do is graph the two lines on a plane to find our solution. This method works best if we suspect that the two lines intersect at integer coordinates.

What is the solution of the linear system of equations? ›

The solution of a linear equation is defined as the points, in which the lines represent the intersection of two linear equations. In other words, the solution set of the system of linear equations is the set of all possible values to the variables that satisfies the given linear equation.

What is the system of linear equation for dummies? ›

Linear Equations

To solve a linear equation, you perform a series of opposites: If a number is added to the term containing x, you subtract that number from both sides of the equation. If a number is subtracted from the term containing the variable, you add. If a number multiplies the variable, you divide.

How do you solve a system of linear equations without graphing? ›

To solve a system of linear equations without graphing, you can use the substitution method. This method works by solving one of the linear equations for one of the variables, then substituting this value for the same variable in the other linear equation and solving for the other variable.

What is the trick to solving linear equations? ›

The steps for solving linear equations are: Simplify both sides of the equation and combine all same-side like terms. Combine opposite-side like terms to obtain the variable term on one side of the equal sign and the constant term on the other. Divide or multiply as needed to isolate the variable.

What is the rule to solve linear equations? ›

If given a linear equation of the form ax+b=c, then we can solve it in two steps. First, use the appropriate equality property of addition or subtraction to isolate the variable term. Next, isolate the variable using the equality property of multiplication or division.

What is the formula for calculating a linear equation? ›

The standard form of linear equations in two variables is expressed as, Ax + By = C; where A, B and C are any real numbers, and x and y are the variables.

How do you solve a system of linear equations without solution? ›

A system of two linear equations has no solution if the lines are parallel. Parallel lines on a coordinate plane have the same slope and different y-intercepts (see figure 3 for an example of this). If the lines look parallel, confirm it by checking that they have the same slope.

What is an example of a system of linear equations? ›

The system of linear equations in two variables is the set of equations that contain only two variables. For example, 2x + 3y = 4; 3x + 5y = 12 are the system of equations in two variables. There are several methods of solving linear equations in two variables, such as: Graphical method.

What are 4 methods of solving linear systems? ›

The four steps for solving linear systems are;
  • Graphical method.
  • By substitution method.
  • By elimination method.
  • By augmented matrix.
Nov 5, 2020

How to solve linear equations step by step? ›

Solve linear equations using a general strategy.
  1. Simplify each side of the equation as much as possible. ...
  2. Collect all the variable terms on one side of the equation. ...
  3. Collect all the constant terms on the other side of the equation. ...
  4. Make the coefficient of the variable term equal to 1. ...
  5. Check the solution.
May 6, 2020

How to find a solution to a linear equation? ›

How Do You Find the Solution of Two Linear Equations?
  1. Solve one of the two equations for one of the variables in terms of the other.
  2. Substitute the expression for this variable into the second equation, then solve for the remaining variable.

How to solve a system of equations? ›

To Solve a System of Equations by Elimination
  1. Write both equations in standard form. ...
  2. Make the coefficients of one variable opposites. ...
  3. Add the equations resulting from Step 2 to eliminate one variable.
  4. Solve for the remaining variable.
  5. Substitute the solution from Step 4 into one of the original equations.

What is the system of linear equations formula? ›

The system of linear equations in two variables is the set of equations that contain only two variables. For example, 2x + 3y = 4; 3x + 5y = 12 are the system of equations in two variables. There are several methods of solving linear equations in two variables, such as: Graphical method.

What are the three methods for solving a system of linear equations? ›

There are three ways to solve a system of linear equations: graphing, substitution, and elimination. The solution to a system of linear equations is the ordered pair (or pairs) that satisfies all equations in the system. The solution is the ordered pair(s) common to all lines in the system when the lines are graphed.

What are the basic solutions of system of linear equations? ›

basic solution: For a system of linear equations Ax = b with n variables and m ≤ n constraints, set n − m non-basic variables equal to zero and solve the remaining m basic variables. basic feasible solutions (BFS): a basic solution that is feasible. That is Ax = b, x ≥ 0 and x is a basic solution.

Top Articles
10 Stocks With Low P/E Ratios | Bankrate
Low PE Growth Stocks
Benchmark Physical Therapy Jobs
Incredibox Deluxe
Wal-Mart 140 Supercenter Products
Orange County's diverse vegan Mexican food movement gains momentum
Tyrones Unblocked Games Basketball Stars
Craigslist Free En Dallas Tx
Savage X Fenty Wiki
My.doculivery.com/Crowncork
Ebony Ts Facials
Humidity Yesterday At My Location
Congdon Heart And Vascular Center
Joe Jonas Lpsg
Hannaford Weekly Flyer Manchester Nh
The Center Breakfast, Lunch & Snack Menus September 2024
Stepmom Full Video Hd
Praxis für Psychotherapie und Coaching Rhein-Neckar
Can You Put Elvie Stride Parts In Sterilizer
Tuition Fee Compensation
A Man Called Otto Showtimes Near Palm Desert
123Movies Evil Dead
Dna Profiling Virtual Lab Answer Key
Kp Scheduling
Bustime B8
Eddie Murphy Cast Of Elemental
Handshoe's Flea Market & Salvage Llc Photos
Maven 5X30 Scope
Conan Exiles Meteor Shower Command
St Cloud Rants And Raves
Current Time In Maryland
Nikki Porsche Girl Head
Best Places To Eat In Winter Park Fl
Family Violence Prevention Program - YWCA Wheeling
Labcorp.leavepro.com
Plastic Bench Walmart
Chipotle Digital Kitchen Briggs Chaney
Media Press Release | riversideca.gov
Tapana Telugu Movie Download Kuttymovies
Franco Loja Net Worth
Heavenly Delusion Gif
https://www.hulu.com/series/amish-haunting-96e9c592-7006-47d6-bb8f-265e9ef174ec
Scarabaeidae), with a key to related species – Revista Mexicana de Biodiversidad
Bfads 2022 Walmart
Realidades 2 Capitulo 2B Answers
Tapana Movie Online Watch 2022
WHAT WE HAVE | Arizona Tile
Do Diversity Visa Lottery Winners Need Affidavit Of Support With Green Card Application Is Affidavit
1Wangrui4
Basis Phoenix Primary Calendar
Craigslist Pgh Furniture
The Ultimate Guide to Newquay Surf - Surf Atlas
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 6350

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.