MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Solving Equations

Contents

Symbolic Solutions vs. Numerical Solutions

We can ask MATLAB to try to solve equations two different ways. MATLAB can sometimes obtain a symbolic solution by manipulating the symbols in the equation(s) much like you would do with pencil and paper in an introductory math class. This approach works well for some problems. Unfortunately there is a large class of problems that will defy attempts to solve them this way. For example try solving (by hand) the equation

ln(x)+x+1=0

It looks easy to solve but it isn't! An equation like this one can be solved using a numerical method which will yield a very good approximation to the precise answer. The numerical approach usually involves taking an initial estimate of the correct solution and then repeating some kind of calculation over and over to gradually improve the estimate.

Solving One Equation Symbolically

Suppose you want to find the solutions to the quadratic equation

x^2-5x=14

Matlab can solve this with the solve command. First we symbolically define our variable x and then apply the command. Try this:

syms x
solve ('x^2 - 5 * x = 14')
 
ans =
 
 -2
  7
 

That was easy! MATLAB found both of the solutions. You can also ask MATLAB to solve equations that involve arbitrary constants. A nice example is to try to find the general solutions to the generalized form of the quadratic equation ax^2+bx+c=0. What do you think the solutions should look like? Let's see if you're right...

syms x a b c
solve('a * x ^ 2 + b * x + c=0')
 
ans =
 
 -(b + (b^2 - 4*a*c)^(1/2))/(2*a)
 -(b - (b^2 - 4*a*c)^(1/2))/(2*a)
 

Do those solutions look familiar? (You may have to mentally re-arrange them to make them look the way you're used to seeing them!)

Important Note 1

How does Matlab know which variable to solve for? It tries to follow common-sense respecting what we usually solve for. Since the variable x is so common it solves for that one first. Suppose we wished to solve ax^2+bx+c=0 for a; we'd have to tell Matlab as follows:

solve('a*x^2+b*x+c=0','a')
 
ans =
 
-(c + b*x)/x^2
 

Important Note 2

If we're interested in simply having the equation equal to zero we can drop the =0 portion. For example:

solve('2*x+3')
 
ans =
 
-3/2
 

Important Note 3

Consider the following:

a=2;b=3;
solve('a*x+b')
 
ans =
 
-b/a
 

Oh dear, what happened? Because of the single quotes around the expression a*x+b Matlab treats this expression symbolically, meaning it treats the letters a and b as letters, not as variables. The values we assigned to them are completely ignored. There are two ways around this, one is to simply drop the single quotes - this forces Matlab to treat the expression not as a string:

solve(a*x+b)
 
ans =
 
-3/2
 

The other way is to use the subs command which we'll learn later. A warning though, the method above, dropping the single quotes, can only be done if the expression is equal to 0.

Solving a System of Equations Symbolically

You can use the solve command for a whole system of equations as well. For example, suppose we are trying to find the solution to the following system of equations:

3x + 4y + z - 7 = 0
x - y -15 = 0
6x - 2y - 5z + 11 = 0

We can try to use the solve command to do this by feeding it all of the equations at once, separating them with commas:

syms x y z
solve (3 * x + 4 * y + z - 7, x - y - 15, 6 * x - 2 * y - 5 * z + 11)
ans = 

    x: [1x1 sym]
    y: [1x1 sym]
    z: [1x1 sym]

Hmmmm. That output is a bit disappointing! To enable us to see the actual answers conveniently, we can introduce an intermediate variable that will store the whole "vector" of answers. Then we can view the values individually. Here's how that might look:

syms x y z
MyAnswers = solve (3 * x + 4 * y + z - 7, x - y - 15, 6 * x - 2 * y - 5 * z + 11)
MyAnswers = 

    x: [1x1 sym]
    y: [1x1 sym]
    z: [1x1 sym]

Now we can ask to see each of the three values individually, as follows:

MyAnswers.x
 
ans =
 
98/13
 
MyAnswers.y
 
ans =
 
-97/13
 
MyAnswers.z
 
ans =
 
185/13
 

Another way to accomplish the same thing is by assigning the value of the computation to a vector comprised of variables like this:

[x y z] = solve (3 * x + 4 * y + z - 7, x - y - 15, 6 * x - 2 * y - 5 * z + 11)
 
x =
 
98/13
 
 
y =
 
-97/13
 
 
z =
 
185/13
 

Notice that the answers are displayed as fractions rather decimal expansions. Does that surprise you? It actually makes sense because Matlab solved the equations symbolically. If you solved these equations by hand, the last operation you would do in calculating z would be to take 185 and divide it by 13. Although you could try to evaluate this as a decimal why should you? The expression 185/33 is exact. If this were written as a decimal it would be 14.230769230769230769230769.... We could only write down an approximation this way and besides -- it looks really hideous!

Warning!!!

The solve command returns a vector with entries in alphabetical order. For example

solve(x+y,x-y+2)

will return the vector whose first entry is x and whose second entry is y. If you do something like

[y x]=solve(x+y,x-y+2)

then what happens is that y gets assigned to the x solution and x gets assigned to the y solution. This is not what you want.

To unconfuse things make sure that your entries in the vector on the left are in alphabetical order! For example if your variables are dogs and cats then don't do:

[dogs,cats] = ...

instead do

[cats,dogs] = ...

Self-Test

  1. We are about to use the variables x and y in some formulas -- declare them as "symbolic".
  2. Write a MATLAB command that will find the solutions to the equation x^2 - x = 7.
  3. Use MATLAB to find solutions to the system of equations consisting of: 2x + 3y = 60 and -x = 2y + 20

Answers to Self-Test

Next Topic: Solving Equations Numerically