Prepare: Solving Systems of Equations in Mathematica

MATH 242 Modern Computational Mathematics

Run the following code before class on Friday, February 27. Then answer the preparation question for February 27 on Moodle.

Suppose we want to solve the system of equations 5a-4b=20 and 3a+2b=10. There are several ways to do this in Mathematica.

One option is to store the two equations in variables, as follows. Note that a single equal sign is an assignment operator that stores something in a variable, while a double equal sign is a comparison operator that can be used within an equation.

Clear[a,b];
eq1=5a-4b==20
eq2=3a+2b==10

Now we can use the Solve function to solve the system of equations, as follows. Note the curly brackets — this function requires a list of equations to solve and a list of variables to solve for.

Solve[{eq1,eq2},{a,b}]

Since our equations are linear equations, we could also write the system of equations using matrices/vectors like this: (({{5, -4}, {3, 2}}))(({{a}, {b}}))=(({{20}, {10}})). We could then solve for a and b using linear algebra. Recall from MATH 220 that we can enter and display matrices and vectors in Mathematica like this:

matrix={{5,-4},{3,2}};
MatrixForm[matrix]
vector={20,10};
MatrixForm[vector]

We can then solve the system by inverting the matrix and multiplying by the vector, like this:

Inverse[matrix].vector

Or, we could use Mathematica’s LinearSolve function, like this:

LinearSolve[matrix,vector]

Which method do you like best?