Functions > Solving and Optimization > Example: Solving a Linear System of Equations
  
Example: Solving a Linear System of Equations
Solve a linear system of n equations in n unknowns using the lsolve function or using solve blocks.
Using lsolve
1. Assume that you have the following set of equations (defined using the equal to operator):
Click to copy this expression
2. Create a nonsingular matrix corresponding to the above system of equations:
Click to copy this expression
3. Create the vector of constants corresponding to the above system of equations:
Click to copy this expression
4. Use the lsolve function to find the solution:
Click to copy this expression
Click to copy this expression
Using Solve Blocks
You can also solve the above system of equations using a solve block.
* 
If you cast the system as a matrix times an unknown vector X, you must solve for all variables in the vector at once. You cannot hold any of the vector elements constant in this formulation.
1. Provide a guess values vector X:
Click to copy this expression
Click to copy this expression
2. Insert a solve block, then use the find function to solve the above system of equations:
Click to copy this expression
* 
The guess values vector X must be defined before and outside the solve block.
Solve Blocks - Solving for Matrices
1. Define the set of algebraic Riccati equation (from system and control theory):
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Define the initial guess matrix as an identity matrix:
Click to copy this expression
3. Use the Find function to solve the above set of equations:
Click to copy this expression
Units in Solve Blocks
1. Set V1, V2 and V3 to 1 volt:
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Define and solve the system of equations with three unknowns:
Click to copy this expression