You are on page 1of 3

Matlab/Mathematica Help

Solving Linear and Non-Linear Sytems of Equations


You've really go two ways to go here. Ill call them Method 1 and 2 for namesake. Applying Method 2 (for either
Matlab or Mathematica) to the solution of non-linear systems and linear systems is by far the easiest way to go.
However, in the hopes of being thorough and at the same time refreshing your memory about these concepts, I'll
present both ideas.
Matlab and Linear Systems (Method 1)
Matlab kicks butt at solving linear systems. Say I want to solve the linear system below:

1x + 2 y + 3 z = 2
5x + 8 y + 9 z = 1
3 x + 2 y + 1z = 5

which, in matrix form Ax = b , is equivalent to:

1 2 3 x 2
5 8 9 y = 1


3 2 1 z 5
So in Matlab, first define matrix A and vector b:

A=[1 2 3;5 8 9;3 2 1]


b=[2;1;5]
Then type:

A\b
You'll get one of three things. Either you'll get an answer, or you'll get:

Warning: Matrix is singular to working precision.


ans =
1/0
1/0
1/0
which means there are no solutions to the system, or you'll get:

Warning: Matrix is singular to working precision.


ans =
Inf
Inf
Inf
which means there are infinite solutions to the system. Now, if you get infinite solutions, you still need to find at
least one solution. Hopefully, you recall how to do this. J

Other helpful hints about solving linear systems include using Matlab's "rref" command. Try this out.

A=[1 2 3;5 8 9;3 2 1];


b=[2;1;5];
Aaugmentb=[A b]
rref(Aaugmentb)
Now if you don't remember how to do Reduced Row Echelon Form via Gauss-Jordan Row Reduction, check out
this command.

A=[1 2 3;5 8 9;3 2 1];


b=[2;1;5];
Aaugmentb=[A b]
rrefmovie(Aaugmentb)
This will show you Gauss-Jordan in action.
Matlab and Linear/Non-Linear Systems (Method 2)
Matlab has a "solve"command that can be used to solve systems of equations whether they are linear or not. The
solve command takes equations listed as text strings contained in single quotes. Consider the non-linear system
below:

1x 2 + 2 y = 0
5x + 8 y = 1
To get Matlab to solve this, you'd type:

[x,y]=solve('x^2+2*y=0','5*x+8*y=1','x,y')
The output would be:

x=
[ 1]
[ 1/4]
y=
[ -1/2]
[ -1/32]
which represents two simultaneous solutions: (x, y)=(1,-1/2) and (x, y)=(1/4, -1/32).
One special note about using the solve command in Matlab, the answers are always given in lexicographic order so
if you typed

[y,x]=solve('x^2+2*y=0','5*x+8*y=1','x,y')
or even

[y,x]=solve('x^2+2*y=0','5*x+8*y=1','y,x')
you'd get the solutions backwards for x and y since x is clearly ordered before y in the alphabet.

Mathematica and Linear Systems (Method 1)


Say I want to solve the linear system below:

1x + 2 y + 3 z = 2
5x + 8 y + 9 z = 1
3 x + 2 y + 1z = 5

which, in matrix form Ax = b , is equivalent to:

1 2 3 x 2
5 8 9 y = 1

3 2 1 z 5
So in Mathematica, first define matrix A and vector b:

A={{1, 2, 3},{5, 8, 9},{3, 2, 1}}


b={2,1,5}
If you want to check to make sure you did these right, just type:

MatrixForm[A]
MatrixForm[b]
To use Gauss Jordan Row Reduction algorithms, you'll need to load the appropriate package by typing:

<<`LinearAlgebra`GaussianElimination`
Now, those aren't normal apostrophes. They are backwards apostrophes, found on the tilde key on the upper left
part of your keyboard. It took me a week to figure that out the first I used Mathematica. ARGH! J Once you've
loaded the package, you can use the command "LinearSolve".

LinearSolve[A,b]
Mathematica and Linear/Non-Linear Systems (Method 2)
Mathematica also has a "solve"command that can be used to solve systems of equations whether they are linear or
not. Consider the non-linear system below:

1x 2 + 2 y = 0
5x + 8 y = 1
To get Mathematica to solve this, you'd type:

Solve[{x^2+2*y==0,5*x+8*y==1},{x,y}]
The output would be:
99y -

1
1
1
, x 1=, 9y , x ==
2
32
4

which represents two simultaneous solutions: (x, y)=(1,-1/2) and (x, y)=(1/4, -1/32).

You might also like