Download Chapter 12 Review

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Functional decomposition wikipedia , lookup

Big O notation wikipedia , lookup

Non-standard calculus wikipedia , lookup

Principia Mathematica wikipedia , lookup

History of the function concept wikipedia , lookup

Function of several real variables wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Chapter 12 Review: Symbolic
Mathematics
Introduction to MATLAB 7
Engineering 161
Symbolic Mathematics




In MATLAB we can use symbols to perform
mathematical computations as well as numbers.
These features are contained in the Symbolic Math
Toolbox. They are based upon Maple 8 a software
package published by Waterloo Maple, Inc.
We will learn how to define expressions symbolically
and then manipulate them with MATLAB functions.
Symbolic manipulations are often very useful in
engineering problem solving and compliments
solutions to problems with numbers.
Symbolic Mathematics II

After studying the material in Chapter 11 you
should be able to




Create and manipulate symbolic variables
Factor and simplify mathematical expressions
Solve symbolic expressions
Determine the symbolic derivative of an
expression and integrate an expression.
An Example

Before jumping into the details, look at some simple examples,
>> a = sym (‘x - 2’); % a and b are sym variables, they are
>> b = sym (‘2*x + 3’); % strings of characters
>> y = a*b
% y is another string of characters
y =
(x-2)*(2*x+3)
>> expand(y)
ans =
2*x^2 - x - 6
Another Example


Consider another example,
>> S = sym(‘(x^2 - 3*x – 10)/(x + 2)’);
>> simplify(S)
ans =
x–5
Note that the symbolic expression is defined by the
sym function, the argument of the sym function is a
string of characters defining the expression
symbolically enclosed in single quotes.
One More Example


Suppose you want to solve the equation D = D0*exp(-Q/RT)
for Q. This equation describes the rate of diffusion, see section
11.1 in the text.
>>X = sym(‘D=D0*exp(-Q/RT)’);
>>solve (X,’Q’)
% Q is in single quotes to tell MATLAB
ans =
% that it is a symbol
-log(D/D0)*RT
Note in MATLAB log represents the natural logarithm, normally
written as ln in mathematics, log10 represents the logarithm to
the base 10 and log2 represents the logarithm to the base 2.
Defining Symbolic Expressions and Variables

There are two methods,




Either create the complex symbolic expression all at once using the
sym command as we did in the examples,
Or, use the syms command to list all the symbolic variables
individually and then compose the expression using algebraic
operators like, * or + and -, etc.
>> syms a x y;
>>S = x^2 -2*y^2 + 3*a;
These two MATLAB statements define the variables a, x, and y as
symbolic variables, and then creates the symbolic expression S.
Note that in the first case using the sym function, only S would
appear in the Workspace Window, while in the second case, S, a,
x, and y all appear in the Workspace Window.
Plotting Symbolic Expressions


MATLAB provides an easy way to plot symbolic
expressions of a single variable, it is called ezplot.
Suppose S is a symbolic expression of x, then
>>ezplot(S, [xmin, xmax])
will plot S between the limits of xmin and xmax.
>> ezplot(S) always uses the range [-2 *pi, 2*pi]
You can use the commands xlabel, ylabel, and title to
add x and y labels, etc., in the usual way. Also use
grid on, hold on and hold off, subplots, etc.
Plotting Symbolic Expressions II


MATLAB provides a number of built in symbolic
plotting functions in addition to ezplot. They are
described in Table 11.3 and some of them are
illustrated on pages 400-401. Do the practice
exercise 11.7 on page 402 to gain some experience
with the variety of symbolic plotting functions.
Remember you can use xlabel, ylabel, title, hold on
and off, and subplots just as you have used them
before with the basic plotting function plot(x,y)
Let’s look at another example


Consider the golden ratio,
>> y = sym(‘(1 + sqrt(5))/2’);
>> f = y^2 – y - 1
f=
(1/2 + 1/2*5^(1/2))^2-3/2-1/2*5^(1/2)
>>simplify(f)
0
y is a symbolic expression that we manipulate in the
first and second MATLAB statements.
A Matrix Example

Consider the following matrix example,
>>syms a b c d;
>>A = [a b c; a b c; a b c]
A=
[ a, b, c]
[ a, b, c]
[ a, b, c]
>>sum(A(1,:))
ans =
a+b+c
>>sum(A(:,2))
ans =
b+b+b
A Matrix Example II



Matrix multiplication symbolically,
>> A = [a b; c d];
>> A^2
ans =
[ a*a+b*c, a*b+b*d]
[ c*a+d*c, c*b+d*d]
Note here that a, b, c and d were already declared as symbols
in the previous example and hence would be in the Workspace
Window as symbols. No need to redefine them for this second
example.
Try taking the inverse of A using the inv function.
Some Symbolic Functions

There are a number of functions used to manipulate and simplify
symbolic expressions,






collect(S)
expand(S)
factor(S)
simplify(S)
simple(S)
poly2sym(V)
collects coefficients of S
expands S where possible
returns the factors of S where possible
simplifies S using Maple’s rules
returns a number of potential simpler forms
creates a symbolic expression for the
polynomial described by the vector V
>>V = [ 1 -4 0 2 45];
>> S = poly2sym(V)
S=
x^4 - 4*x^3 + 2*x + 45
What do you think V = sym2poly(S) does?
Some Symbolic Functions II

Continuing,

solve(f)

solve(f, ‘y’)

solve(f1,…,fn)
solves the symbolic equation f for its
symbolic variable; if more than one, then
for x, sets f = 0 and solves for x.
solves f(x,y) for y; you don’t need the
quotes if y has been declared as a
symbolic variable
solves a system of equations
Let’s look further into this last function.
Solve Function





When used with an expression, the solve function sets the
expression to zero and solves for its roots
For example, E1 = x – 3, solve(E1) would yield the answer ans
= 3.
One could also have said solve(x-3), that put the expression
inside the parenthesis.
The solve function normally solves for x, if you want to solve for
a different variable, then solve(f, t) for example would solve for
t.
Even when the result of the solve function is a number, it is still
stored as a symbolic variable and needs to be converted to be
used in an arithmetic expression, use the double function.
Substitution: the subs function

Once you have a symbolic expression, you may want
to substitute values into it, we use the subs function.
Consider,
syms a b c x;
f = a*x^2 + b*x + c;
g = subs(f, a, 3)
g=
3*x^2 + b*x + c
Substitution II
h = subs(f, {a, b, c}, {1, 2, 3})
h=
x^2 + 2*x + 3
subs(h, x, -2)
ans =
3
Substitution III

Substituting an expression, consider from Example
11.2,
syms v0 t g theta;
distancex = v0 * t * cos(theta);
impact_time = 2 * v0 * sin(theta)/g;
impact_distance = subs(distancex, t, impact_time)
impact_distance =
2 * v0^2 * sin(theta)/g * cos(theta)
Solving Simultaneous Equations


Suppose we have the following,
>>eq1 = sym (‘3*x + 2*y - z = 10’);
>>eq2 = sym (‘-x + 3*y + 2*z = 5’);
>>eq3 = sym (‘x – y – z = -1’);
>>[A, B, C] = solve (eq1, eq2, eq3)
A=
-2
B=
5
C=
-6
Note the symbols assigned to A, B, and C are assigned alphabetically
based upon the order of the variable names in the equations, eq1,
eq2, eq3.
Solving Simultaneous Equations Again II




Now remember, if you want to use A, B, and C in some subsequent
numerical expression, you will have to convert them to numbers, (A, B,
and C have character representations from the solve operation.)
To do this, do the following;
>> a = double(A), b = double(B), c =double(C); this double function
converts characters to real numbers.
Now a, b, and c have numerical variables assigned to them.
To avoid confusion, watch your Workspace window for the declarations
of the variables as they are created. Characters have the <1x1sym>
designation while real numbers are designated as double typically.
Calculus

MATLAB’s symbolic toolbox allows users to differentiate
symbolically and to perform integrations. This makes it possible
to find analytical solutions (expressions), instead of numeric
approximations for many problems. We will look at
differentiation
integration
solving simple differential equations

Let’s start by looking at an example where we’ll use the int
(integration) function to solve a basic problem.
Another Example: Water Flow (11.32)

Water is being pumped into an initially empty tank. It is known
that the rate of flow of water into the tank at time t (seconds) is
(50 – t) liters/sec. The amount of water Q that flows into the
tank during the first x seconds can be shown to be equal to the
integral of the expression (50 – t) evaluated from 0 to x
seconds.



Determine a symbolic equation that represents the amount of
water in the tank after x seconds.
Determine the amount of water in the tank after 30 seconds.
Determine the amount of water that flowed into the tank between
10 and 15 seconds after the flow was initiated.
Water Flow II

Consider the following sequence of MATLAB statements;
>> f = sym (’50 – t’)
f=
50 – t
>>Q = int (f, 0, ‘x’)
Q=
50*x – 1/2*x^2
>>Q = int (f, 0, 30)
Q=
1050
>>Q = int (f, 10, 15)
Q=
375/2
% create the symbolic variable f = 50 - t
% integrate f(t) between ‘0’ and ‘x’ seconds
% integrate f(t) between 0 and 30 seconds
% integrate f(t) between 10 and 15 seconds
Differentiation

The diff function is used to determine the symbolic
derivative of a symbolic expression f.

diff(f)

diff(f,

diff(f,

diff(f,
We look
returns the derivative of f w.r.t. the
default independent variable
‘t’)
returns the derivative of f w.r.t. the
variable t
n)
returns the nth derivative of f w.r.t the
default independent variable
‘t’, n)
returns the nth derivative of f w.r.t the
variable t
at the example in our book in section 11.4.1, pg 419
The Drag Racer

Consider a drag racer that starts from rest, accelerates to the
finish line and then decelerates back to rest. The total distance
travelled is given by the following expression;
d = 20 + 20 sin (p(t – 10)/20)
Let’s use MATLAB to plot the function d vs t for 0 < t < 20
seconds, determine and plot the velocity and acceleration of the
drag racer and determine the maximum velocity of the car. The
car crossed the finish line at t = 10 seconds when it starts to
decelerate.
The Drag Racer II

Consider the following MATLAB statements;
syms t;
d = 20 + 20*sin(pi*(t – 10)/20);
ezplot(d, [0,20]);
vel = diff(d);
figure(2);
ezplot(vel, [0,20]);
The Drag Racer III

Continuing,
accel = diff(vel);
figure(3);
ezplot(accel, [0,20]);
d
% outputs the expression for d
vel
% outputs the expression for vel
accel
% outputs the expression for accel
The Drag Racer IV

Continuing,
The maximum velocity is determined by setting the
derivative of the velocity to zero and solving for t.
Since accel is the derivative of vel, we have that
max_vel = solve(accel)
Integration

MATLAB supports the following symbolic integration capabilities; the int
function attempts to find a function F such that diff(F) = f; diff(F)
being the derivative of F.

int(f)

int(f, ‘t’)
int(f, a, b)

int(f, ‘t’, a, b)

Int(f, ‘m’, ‘n’)

returns the integral of the expression f w.r.t. the
independent variable
returns the integral of f w.r.t. the variable t
returns the integral of f w.r.t. the independent
variable over the interval [a,b]; a and b numerical
returns the integral of f w.r.t to t over the interval
[a,b]; a and b numerical
returns the integal of f w.r.t. the independent
variable over the interval [m,n] where m and n
are symbolic expressions.
Differential Equations

MATLAB’s symbolic toolbox provides capabilities to solve differential
equations. To review these capabilities use the help dsolve command
in the Command Window. dsolve is the function that you will want to
explore. Use the MATLAB help features. Consider the following,
Suppose we have the equation dx/dt = -a*x and we want to solve for
x
>>dsolve(‘Dx = - a*x’)
ans =
C1*exp(-a*t)
% note the undetermined constant C1
>>dsolve(‘Dx = -a*x’, ‘x(0) = 2’) % with the initial condition specified
ans =
2*exp(-a*t)
% pretty cool
Chapter 11 Assignment


These assignments will give you some
practice with the concepts in Chapter 11.
Assignments 12.7, 12.9 and 12.10, 12.11,
12.15, 12.16, 12.29, 12.30.