Documente Academic
Documente Profesional
Documente Cultură
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
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)
>> 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:
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