﻿ 20-sim webhelp > Language Reference > Functions > Matrix > linsolve

# linsolve

## Syntax

x = linsolve(A,b [,method]);

## Description

This function solves the equation

A*x = b;

Where A is a square matrix of arbitrary size n and x and b are vectors of size n.

Returns the inverse of square non-singular matrix A.

The last argument is a string that specifies the desired method to use.

 method description lu LU decomposition (default method) qr QR factorization cholesky Cholesky factorization gmres Generalized Minimum Residual

The gmres method allows further steering of the method by specifying method parameters:

method parameter

description

tol

Set the desired tolerance to use

maxiter

Set the maximum number of iterations to use

ortho

Set the method of Gramm-Schmidt orthogonalization:

 1 modified Gramm-Schmidt 2 iterative Gramm-Schmidt 3 classical Gramm-Schmidt 4 iterative classical Gramm-Schmidt

The lu, qr and gmres methods allow a non-square matrix A to be entered yielding a pseudoinverse.

## Examples

A = [1,2;0,1+ramp(1)];

b = [1;5];

x = linsolve(A,b);

x2 = linsolve (A, b, 'qr');

x3 = linsolve (A, b, 'gmres tol=1e-8 ortho=4');

## Limitations

A must be a non-singular square matrix.

## Note

The following equations

A*x = b;

x = inverse(A)*b;

x = (A^-1)*b;

all lead in the calculation of the inverse of A and will give the same result as

x = linsolve(A,b);

For the inverse calculation, Cramers rule is used. This is a method which is fast for small matrix sizes. The linsolve function with the gmres method is superior to Cramers rule for larger matrix sizes.