You are on page 1of 21

Matlab Programming

Course
FIME UANL
Profesor MC Manuel Munguia Macario
Matlab Programming Course
Week Five: Solving Algebraic Equations and Other Symbolic Tools
we will begin to look at using MATLAB to solve equations. We start
with simple algebraic equations considering solutions for single
variables and solving systems of equations. Then we will look at
working with transcendental, trig, and hyperbolic functions. Finally
we will see how MATLAB handles complex numbers
Matlab Programming Course
Week Five: Solving Basic Algebraic Equations
Suppose that we wanted to use It is possible to include multiple symbols in the
equation you pass to solve. For instance, we might
MATLAB to find the value of x that want to have a constant included in an equation like
solves: this:
x+8=0 ax + 5 = 0
it isnt necessary to include the If we enter the equation in MATLAB, it seems to just
assume that we want to solve for x (by default):
right-hand side of the equation.
MATLAB assumes that when you >> solve('a*x+5')
pass x + 8 to solve that you mean ans =
x + 8 = 0. To verify this, we run this 5/a
command line:
We can tell it what symbol we want it to solve for
>> x = solve('x+8')
solve(equation, variable)
x =
>> solve('a*x + 5','a')
8
MATLAB responds with the output:
So enter the equations whichever ans =
way you want
5/x
Matlab Programming Course
Week Five: Solving Quadratic Equations
Lets consider the equation:
x - 6x - 12 = 0
It is possible to assign an equation to a
variable and then pass the variable to
2

We could solve it by hand. You can complete the


square or just apply the quadratic formula. To
solve it using MATLAB, we write: solve:
>> s = solve('x^2 -6*x -12 = 0') >> d = 'x^2 + 9*x -7 = 0';
MATLAB responds with the two roots of the
equation: Now we call solve this way:
s = >> solve(d)
3+21^(1/2)
3-21^(1/2) MATLAB correctly tells us the two roots
the roots are stored as s(1) and s(2). So we can of the equation:
use one of the roots to defi ne a
ans =
new quantity:
-9/2+1/2*109^(1/2)
>> y = 3 + s(1)
y = -9/2-1/2*109^(1/2)
6+21^(1/2)
Matlab Programming Course
Week Five: Plotting Symbolic Equations
MATLAB allows us to generate
plots of symbolic equations weve
entered. This can be done using
the ezplot command. Lets do that
for this example. First lets create
the string to represent the
equation:
>> d = 'x^2 6*x 12';
Now we call ezplot:
>> ezplot(d)
Matlab Programming Course
Week Five: Plotting Symbolic Equations
We can specify what we want
by
specifying the domain with the
following syntax:
ezplot(f, [x1, x2])
This plots f for x < x < x .
1 2

Returning to the previous


example, lets say we
wanted to plot it for -2 < x < 8.
We can do that using the
following command:
>> d = 'x^2 -6*x - 12';
>> ezplot(d,[-2,8])
Matlab Programming Course
Week Five: Plotting Symbolic Equations
lets say we wanted to plot:
x+3=0
-4 < x < 4, -2 < y < 2
We can do this by typing (in this
case matlab DO NOT accept
x+3=0):
>> ezplot('x+3',[-4,4,-2,2])
Matlab Programming Course
Week Five: Plotting Symbolic Equations
Find the roots of x2 + x (2)1/2 = 0 and
plot the function. Determine the
numerical value of the roots.
>> eq = 'x^2 + x - sqrt(2)';
Or if you like, you could write:
>> eq = 'x^2 + x - 2^(1/2)';
Next we call solve to find the roots:
>> s = solve(eq)
s =
-1/2+1/2*(1+4*2^(1/2))^(1/2)
-1/2-1/2*(1+4*2^(1/2))^(1/2)
Matlab Programming Course
Week Five: Plotting Symbolic Equations
To determine the numerical value of
the roots, we need to extract them
from the array and convert them into
type double. This is done by simply
passing them to the double(.)
command. For example, we get the
first root out by writing:
>> x = double(s(1))
x =
0.7900
And the second root:
>> y = double(s(2))
y =
-1.7900
>> ezplot(eq)
Matlab Programming Course
Week Five: Solving Higher Order Equations
Lets try a cubic. Find the roots of the fourth order
Suppose we are told that: equation
(x + 1)2 (x - 2) = 0 X4-5x3 + 4x2- 5x + 6 = 0
Solving an equation like this is no and plot the function for -10 < x < 10
different than what weve done so far. >> eq1 = 'x^4-5*x^3+4*x^2-5*x+6';
We find that the roots are: >> s = solve(eq1);
>> eq='(x+1)^2*(x-2)' >> a=s(1)
eq = (x+1)^2*(x-2) a=

>> solve(eq) 4.258759762224535350135371


ans = 8744479
2 >> a=double(s(1))
-1 a=
-1 4.2588
Matlab Programming Course
Week Five: Solving Higher Order Equations
>> a=double(s(2))
a=
1.1164
>> a=double(s(3))
a=
-0.1876 - 1.1076i
>> a=double(s(4))
a=
-0.1876 + 1.1076i
>> ezplot(eq1,[-10 10])
Matlab Programming Course
Week Five: Solving Higher Order Equations
Find the roots of x3 + 3x2 2x 6
and plot the function for 8 < x < 8,
8 < y < 8. Generate the plot with a
grid.
>> f = 'x^3+3*x^2-2*x-6';
>> s = solve(f)
s=
-3
2^(1/2)
-2^(1/2)

>> ezplot(f,[-8,8,-8,8]), grid on


Matlab Programming Course
Week Five: Solving Higher Order Equations >> eq1 = 'w+x+4*y+3*z=5';
>> eq2 = '2*w +3*x+y-2*z = 1';
solve can be used to generate solutions of systems of equations.
5x + 4y = 3 >> eq3 = 'w+2*x-5*y+4*z=3';

x 6y = 2 >> eq4 = 'w-3*z=9';


>> s = solve('5*x + 4*y = 3','x 6*y = 2'); Now we call solve, passing the equations as a comma-delimited list:
Notice that each equation is enclosed in single quote marks and that >> s = solve(eq1,eq2,eq3,eq4);
we use a comma to delimit the equations. We can get the values of x
and y by using a dot notation as follows. First lets get x: We can then extract the value of each variable
>> x = s.x
>> w = s.w
x=
w=
13/17
MATLAB has generated a nice fraction for us that we can write in on 1404/127
our homework solutionsthe professor will never suspect a thing. >> x = s.x
Next we get y:
>> y = s.y x=
y= 818/127
7/34 >> y = s.y
This method can be used with larger linear systems. Consider:
y=
w + x + 4y + 3z = 5
53/127
2w + 3x + y 2z = 1
w + 2x 5y + 4z = 3 >> z = s.z

w 3z = 9 z=
87/127
Matlab Programming Course
Week Five: Expanding and Collecting Equations
In elementary school we learned how to expand
equations. For instance:
To work with many symbolic
functions, you must tell MATLAB
(x + 2) (x 3) = x2 x 6 that your variable is symbolic. For
We can use MATLAB to accomplish this sort of task by example, if we type:
calling the expand command. Using expand is
relatively easy. For example: >> expand((y-2)*(y+8))
>> expand((x1)*(x+4)) MATLAB returns:
When this command is executed, we get:
??? Undefined function or variable
ans = x^2 +3*x 4 'y
The expand function can be applied in other ways. For
example, we can apply it
To get around this, first enter:
to trig functions, generating some famous trig >> syms y
identities: Then we get:
>> expand(cos(x+y))
>> expand((y-2)*(y+8))
This gives us:
ans =
ans =
y^2+6*y-16
cos(x)*cos(y)sin(x)*sin(y)
Matlab Programming Course
Week Five: Expanding and Collecting Equations
MATLAB also lets us go the other way, Another algebraic task we can do
collecting and simplifying equations. symbolically is factoring. To show that:
First lets see how to use the collect X2-y2= (x + y)(x - y)
command. One way you can use it is for Using MATLAB, we type:
distribution of multiplication. Consider: >> factor(x^2-y^2)

x(x2- 2) = x3- 2x ans =


(x-y)*(x+y)
To do this in MATLAB, we just write: We can factor multiple equations with a
>> collect(x*(x^2-2)) single command:
ans = >> factor([x^2-y^2, x^3+y^3])
x^3-2*x Finally, we can use the simplify command.
Another example (with a new symbolic This command can be used to divide
variable t): polynomials. For instance, we can show that
>> syms t (x2 + 9)(x2- 9) = x4-81 by writing:
>> collect((t+3)*sin(t)) >> simplify((x^4-81)/(x^2-9))
ans =
ans =
x^2+9
sin(t)*t+3*sin(t)
Matlab Programming Course
Week Five: Expanding and Collecting Equations
>> simplify(exp(2*log(3*x)))
ans =
9*x^2

The simplify command is useful


for obtaining trig identities.

>> simplify(cos(x)^2-sin(x)^2)
ans =
2*cos(x)^2-1
>> simplify(cos(x)^2+sin(x)^2)
ans =
1
Matlab Programming Course
Week Five: Solving with Exponential and Log
>> a = double(s.x(1))
Functions
The symbolic solver can also be used with exponential and
a =
logarithmic functions 0.2876
log10 (x) - log10 (x - 3) = 1 >> b = double(s.x(2))
>> eq = 'log10(x)-log10(x-3) = 1'; b =
>> s = solve(eq); 1.6214
>> s(1)
ans =
We can also extract the y values:
10/3 >> c = double(s.y(1))
Otro ejemplo: c =
y = 3 2x 2.5887
y = 5x + 1 >> d = double(s.y(2))
We can call solve to find the solution: d =
>> s = solve('y = 3^2*x','y = 5^x+1') 14.5924
s = The idea is to see if s.x(1) satisfies the first
x: [2x1 sym] equation giving s.y(1)
y: [2x1 sym] >> 3^2*a
there are two values of each variable that solve the ans =
equation. We can get the values of x out by typing: 2.5887
Matlab Programming Course
Week Five: Solving with Exponential and Log
Functions
We can also enter and solve equations
involving the exponential function. For
example:
>> eq = 'exp(x)+ x';
>> s = solve(eq)

s =
-lambertw(0, 1)

>> double(s)

ans =
-0.5671

>> ezplot(eq)
>> grid on
Matlab Programming Course
Week Five: Series Representations of Functions
MATLAB can be used to obtain the series
representation of a function, given symbolically.
The taylor function returns the Taylor series
expansion of a function
>> syms x
>> s = taylor(sin(x))
s=
X-1/6*x^3+1/120*x^5
( Matlab13=> x^5/120 - x^3/6 + x)
MATLAB has returned the first three terms of the
expansion. In fact what MATLAB returns is:

In the case of sin, these are the only nonzero


terms. Lets plot it:
>> ezplot(s)
Matlab Programming Course
Week Five: Series Representations of Functions
We can make the call by writing
taylor(f, order,m). For the present
example, lets try 20 terms:
>> s = taylor(sin(x),'order',20)

s =
- x^19/121645100408832000 +
x^17/355687428096000 -
x^15/1307674368000 +
x^13/6227020800 - x^11/39916800 +
x^9/362880 - x^7/5040 + x^5/120 -
x^3/6 + x

>> ezplot(s)
Matlab Programming Course
Week Five:end

You might also like