CSCI 2227, Introduction to Scientific Computation
Prof. Alvarez
First steps in linear equations in MATLAB

MATLAB makes it easy to work with linear equations. Here I describe commands with which you can set up and solve systems of linear equations in MATLAB. We will discuss much more material in class.


Setting up a linear system

MATLAB uses matrix (vector, array) variables to represent data. You therefore need to cast a linear system of equations as a matrix equation in order to manipulate it in MATLAB. I assume here that the system of interest is fairly small (say fewer than a half-dozen variables and equations), so that inputting it manually is practical.

Entering the rows of the system

Given a system such as the following:
	20 x - y + 12 z = 2
	5 x + 3y - 10z = 30
	x + 5z = -100
the coefficient matrix a may be read off by ignoring the variable labels. Note that the order in which the variables appear must be the same in all of the equations. For example, the variable order in this example is x, y, z, and the coefficient matrix is:
	20 	-1 	12 
	5 	3 	-10
	1 	0 	5 
Note that there is a 0 in the y position in the bottom row. We may enter this system into MATLAB one row at a time, by typing the following commands at the MATLAB prompt:
	a(1,:) = [20 	-1 	12] (press enter)
	a(2,:) = [5 	3 	-10] (press enter)
	a(3,:) = [1 	0 	5] (press enter)
An alternative is to express the entire coefficient matrix in terms of its columns:
	a = [ [20 5 1]' [-1 3 0]' [12 -10 5]' ]  (press enter)
The single quote character transposes a given row matrix, turning it into a column matrix as needed here.

The right-hand sides of the equations should be entered into a separate column vector (matrix) b:

	b = [2 30 -100]'

Matrix form of the system

Having entered the linear system as described above, by defining the matrices a and b, the matrix form of the system is simply:
	a*v = b
where a and b are the given matrices and v is the column matrix formed by the unknowns x, y, z.


Solving the linear system

To solve the system represented by the matrices a and b as described above, just type the following at the MATLAB command prompt:
	v = a\b
This will load the solutions into the column matrix v. The solution procedure used by Matlab is Gaussian elimination with partial pivoting, as discussed in class.