topic 12 symbolic mathematics and programming · 2012. 10. 22. · using the solve function for...
TRANSCRIPT
CITS2401 Computer Analysis & Visualisation
SCHOOL OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING
FACULTY OF ENGINEERING, COMPUTING AND MATHEMATICS
Topic 12 Symbolic
Mathematics and Programming
Based on material by Paul Abbott and Holly Moore
The University of Western Australia
Objectives ◊ Create, visualize and solve symbolic expressions in
Mathematica. ◊ Understand the difference between symbolic and
numerical methods ◊ Solve systems of equations ◊ Determine the symbolic derivative of an expression ◊ Integrate an expression symbolically ◊ Use Matlab to perform symbolic computations.
The University of Western Australia
Symbolic Versus Numeric Computation ◊ A computers memory is discrete. Every floating point number
is stored using 64 binary digits (bits).
◊ By the IEEE 754 standard there are 52 bits for precision, 11 bits for an exponent and 1 bit for the sign, but it is necessarily an approximation.
◊ Consider Sqrt[5] in Mathematica and sqrt(5) in Matlab:
The University of Western Australia
Symbolic Computation
◊ Symbolic computation doesn’t map calculations to numbers. Instead it records the operations that were applied to the data so no information is lost:
• We can use N to produce a numeric approximation when require, but until then the symbols can just accumulate.
• This can allow the expressions to get very big, very quickly, and cause slow computation.
The University of Western Australia
Rule based computation ◊ To manage the size of expressions, rules are used to simplify
expressions whenever possible
◊ To do this we require rules to seek out and apply simplifying substitutions.
The University of Western Australia
Simplify ◊ Simplify is a function that will apply a large sequence of
algebraic rules to an expressions and return the simplest one it can find:
◊ We can also include assumptions, which are predicates:
The University of Western Australia
Expand ◊ Expand works in the opposite way to simplify, applying
algebraic rules to expand an expression:
• Expand works on polynomials
• TrigExpand works on trigonometric expressions
• LogicalExpand works on Boolean expressions
The University of Western Australia
Solve ◊ Solve will solve an equation
symbolically. An equation requires a set of equalities or inequalities, and a set of symbols to solve for:
The University of Western Australia
Solve ◊ For each member of the parameter list, Solve returns a
substitution (or list of substitutions):
◊ Use the /. notation to return apply the substitutions to the original functions. If the system cannot be completely solved, there may be parameters left in the substitution. You can also specify the domain as either Integers, Reals or Complexes
The University of Western Australia
NSolve ◊ NSolve works in the same way as solve, except it seeks a
numerical approximation to the set of equations
The University of Western Australia
Symbolic Visualisation ◊ Functions Plot, Plot3D and
ParametricPlot3D can be used to visualize symbolic functions:
The University of Western Australia
Symbolic Computation in Matlab
◊ MuPad is a Mathematica like interface that has been incorporated into Matlab
◊ Available in the “Symbolic Math Toolbox” – which is an option in the professional version
◊ Included with the Student Edition
• There are some differences between the MuPad and Maple implementations of the symbolic toolbox
The University of Western Australia
Capabilities ◊ Manipulate symbolic
expressions to • Simplify • Solve symbolically • Evaluate numerically • Take derivatives • Integrate • Perform linear algebraic
manipulations ◊ More advanced features
include • LaPlace transforms • Fourier transforms • Variable precision
arithmetic
The University of Western Australia
Symbolic Algebra ◊ Used regularly in math, engineering and
science classes ◊ Often preferable to manipulate equations
symbolically before you substitute in values for variables
96)3(*2
2
2
++
+=
xxxy
Consider this equation
The University of Western Australia
y = 2*(x +3)2
x2 + 6x + 9
This looks like a fairly complicated function of x
y = 2*(x +3)2
x2 + 6x + 9=2*(x2 + 6x + 9)(x2 + 6x + 9)
= 2
If you expand it, it simplifies dramatically, but if you evaulate it numerically….
Let x equal -3
y = 2*(−3+3)2
9−18+ 9= 2* 0
0= undefined
The University of Western Australia
Relationships are not always easy to solve
If we know k0, Q, R, T the its easy to solve for k. It’s not easy to solve for T!
/0
Q RTk k e−=0
0
0
0
ln( ) ln( )
ln
ln
ln( / )
Qk kRT
k Qk RT
k Qk RT
QTR k k
= −
⎛ ⎞= −⎜ ⎟
⎝ ⎠
⎛ ⎞ =⎜ ⎟⎝ ⎠
=
The University of Western Australia
Creating Symbolic Variables
◊ Two approaches • Use the sym command to create
– Single variable
– Expression
– Equation
• Use the syms command to create – Single variables
– Compose expressions and equations from the variables you’ve defined
The University of Western Australia
Here’s an example ◊ Define x as a symbolic variable
• x=sym('x') or syms x
◊ Use x to create a more complicated expression • y = 2*(x+3)^2/(x^2+6*x+9)
The University of Western Australia
The syms command can create multiple variables ◊ syms Q R T k0 ◊ Use these variables to create another symbolic variables
• k=k0*exp(-Q/(R*T)) ◊ Create an entire expression with syms:
• E=sym('m*c^2') ◊ Since m and c have not been specifically defined as symbolic
variables, they are not stored
◊ E was set equal to a character string, defined by the single quotes inside the function.
The University of Western Australia
Equations vs Expressions ◊ We can create an entire equation, and give it a
name
◊ ideal_gas_law=sym('P*V=n*R*Temp')
This is an algebraic equation This is an assignment statement
• Equations are set equal to something • Expressions are not • Both expressions and equations can be assigned a
name, using the MATLAB assignment operator
The University of Western Australia
The variable ideal_gas_law has been assigned to an equation
The University of Western Australia
The variable E as been assigned to an expression
The University of Western Australia
Working with Equations and Expressions ◊ Some MATLAB functions work on both expressions and
equations
◊ Others are limited in their scope
The University of Western Australia
Extracting Numerators and Denominators ◊ These functions work on expressions
The University of Western Australia
Expanding and Factoring
num is an expression
The University of Western Australia
w is an equation
The University of Western Australia
Simplifying
◊ The expand, factor and collect functions can be used to “simplify” an expression and sometimes an equation
◊ What constitutes a simplification is not always obvious
◊ The simplify function uses a set of built in rules
◊ Simple ◊ The simple function is different from simplify
◊ It tries all of the different simplification techniques, and chooses the result that is the shortest
The University of Western Australia
simplify used on an expression
simplify used on an equation
The University of Western Australia
All of the possibilities evaluated are reported, however there is only one actual answer
Both simple and simplify work on expressions and equations
The University of Western Australia
Hints ◊ Use the poly2sym function as a
shortcut to create a polynomial
◊ Extract the coefficients from a polynomial, using the sym2poly function
The University of Western Australia
Solving Equations and Expressions ◊ Use the solve
function
◊ Automatically sets expressions equal to 0 and solves for the roots
◊ Uses the equality specified in equations
◊ Solves for the variables in systems of equations
The University of Western Australia
The answer from the solve function is not necessarily a number
You can specify what variable you want to solve for
The University of Western Australia
Solving Systems of Equations
This result is a structure array. These arrays are described in Chapter 11
The University of Western Australia
There are several different approaches to find the actual values of x, y, and z
The University of Western Australia
Give the result a name, such as answer, and then specify the field name inside the structure array to retrieve the values for x, y, and z
The University of Western Australia
Assign individual variable names. Notice that x, y and z are symbolic variables
The University of Western Australia
If you need to use the value of x, y or z in a function that needs a double as input, you’ll need to change the variable type from symbolic to double
The University of Western Australia
Hint
◊ Using the solve function for multiple equations has both advantages and disadvantages over using the linear algebra techniques described in chapter 9.
◊ In general, if a problem can be solved using matrices the matrix solution will take less computer time.
◊ However, linear algebra is limited to first order equations. ◊ The solve function may take longer, but it can solve non-
linear problems and can solve problems with symbolic variables.
The University of Western Australia
Example 12.1 ◊ Use MATLAB’s symbolic capability to solve an equation
– k = k0*exp(-Q/RT),…..Solve for Q
0 expQk kRT−⎛ ⎞= ⎜ ⎟
⎝ ⎠
0
expk Qk RT
−⎛ ⎞= ⎜ ⎟⎝ ⎠
0
ln k Qk RT⎛ ⎞ −
=⎜ ⎟⎝ ⎠
0ln kQ RTk
⎛ ⎞= ⎜ ⎟⎝ ⎠
The University of Western Australia
We could substitute in a new variable – in this case we’ll put a y everywhere there used to be an x
The University of Western Australia
We could substitute in a number – in this case 3 for x
The University of Western Australia
If the variables inside the expression have been explicitly defined as symbolics we don’t need the single quotes
To substitute into multiple variables group them with curly braces
The University of Western Australia
Example 12.2 ◊ Using Symbolic Maths to solve a ballistics
problem
vertical distance
horizontal distance
Range dist _ x = v0* t *cos(θ )dist _ y = v0* t *sin(θ )−1/ 2*g* t2
The University of Western Australia
Strategy ◊ Find the time the projectile hits the ground
◊ Substitute back into the horizontal distance equation
The University of Western Australia
Results
The University of Western Australia
12.4 Calculus ◊ MATLAB’s symbolic toolbox supports
• Symbolical differentiation
• Symbolic integration
◊ This makes it possible to find analytical solutions for many problems, instead of numeric approximations.
The University of Western Australia
Differentiation
◊ Concept introduced in Calculus I
◊ However… a derivative is really just the slope of an equation
◊ A common application of derivatives is to find velocities and accelerations
The University of Western Australia
Consider a race car…
◊ Assume that during a race the car starts out slowly, and reaches its fastest speed at the finish line
◊ To avoid running into the stands, the car must then slow down until it finally stops
The University of Western Australia
Model
◊ We might model the position of the car using a sine wave
dist = 20+ 20*sin(π *(t −10) / 20)
The University of Western Australia
Create a plot of position vs time using ezplot
The University of Western Australia
0 5 10 15 20
05
1015
2025303540
time, sec
Car position
Dist
ance
from
Sta
rting
Lin
e
Finish Line
ezplot of position
The University of Western Australia
diff function
◊ The diff function finds a symbolic derivative
◊ The velocity is the derivative of the position, so to find the equation of the velocity of the car we’ll use the diff function, then plot the result
The University of Western Australia
Find the symbolic derivative, which corresponds to the velocity
Create a plot of velocity and time
The University of Western Australia
The velocity is the derivative of the position with respect to time
0 5 10 15 20
0
0.5
1
1.5
2
2.5
3
time, sec
Race Car Velocity
velo
city
, dis
tanc
e/tim
e
Finish Line
The University of Western Australia
Acceleration
◊ The acceleration is the derivative of the velocity, so to find the equation of the acceleration of the car we’ll use the diff function, then plot the result
The University of Western Australia
Determine the equation for the acceleration
The University of Western Australia
Acceleration is the derivative of the velocity
0 5 10 15 20-0.6
-0.4
-0.2
0
0.2
0.4
0.6
time, sec
Race Car Acceleration
acce
lera
tion,
vel
ocity
/tim
eFinish Line
The University of Western Australia
Symbolic Differentiation diff(f) Returns the derivative of the
expression f with respect to the default independent variable
y=sym('x^3+z^2') diff(y) ans = 3*x^2
diff(f,’t’) Returns the derivative of the expression f with respect to the variable t.
y=sym('x^3+z^2') diff(y,'z') ans = 2*z
diff(f,n) Returns the nth derivative of the expression f with respect to the default independent variable
y=sym('x^3+z^2') diff(y,2) ans = 6*x
diff(f,’t’,n)
Returns the nth derivative of the expression f with respect to the variable t.
y=sym('x^3+z^2') diff(y,'z',2) ans = 2
The University of Western Australia
Integration
◊ Usually introduced in Calculus II
◊ Often visualized as the area under a curve
◊ MATLAB has built in symbolic integration capability.
The University of Western Australia
Consider a piston cylinder device ◊ Work done by a piston cylinder device as it moves up or
down, can be calculated by taking the integral of P with respect to V
W = PdV1
2∫
To perform the integration we need to know how P changes with V. If P is constant the problem becomes: W = P dV
1
2∫
The University of Western Australia
Model of the behavior of a piston cylinder device
0 1 2 3 4 599
99.5
100
100.5
101Pressure Profile in a Piston Cylinder Device
Volume, cm3
Pres
sure
, psi
a
gas
The University of Western Australia
Hand Calculation
W = PdV = P1
4∫ dV = PV
1
4
1
4∫ = PV4 −PV1 = PΔV
if P =100psiaW = 3cm3 *100psia
Read this as: Work is equal to the integral of P with respect to V, from V=1 to V=4
The University of Western Australia
MATLAB Solution
Work is equal to the integral of P with respect to V, from V=1 to V=4
Substitute in 100 as the value of P
The University of Western Australia
Symbolic Integration
int(f) Returns the integral of the expression f with respect to the default independent variable
y=sym('x^3+z^2') int(y) ans = 1/4*x^4+z^2*x
int(f,’t’) Returns the integral of the expression f with respect to the variable t.
y=sym('x^3+z^2') int(y,'z') ans = x^3*z+1/3*z^3
int(f,a,b) Returns the integral with respect to the default variable, of the expression f between the numeric bounds, a and b.
y=sym('x^3+z^2') int(y,2,3) ans = 65/4+z^2
int(f,’t’,a,b) Returns the integral with respect to the variable t, of the expression f between the numeric bounds, a and b.
y=sym('x^3+z^2') int(y,'z',2,3) ans = x^3+19/3
int(f,’t’,a,b) Returns the integral with respect to the variable t, of the expression f between the symbolic bounds, a and b.
y=sym('x^3+z^2') int(y,'z','a','b') ans = x^3*(b-a)+1/3*b^3-1/3*a^3
The University of Western Australia
Converting Symbolic Expressions to MATLAB functions ◊ It is often useful to manipulate expressions symbolically … but
then to perform numeric calculations using more traditional MATLAB functions
◊ matlabFunction converts a symbolic expression to an anonymous function
The University of Western Australia
Symbolic Manipulation
An anonymous function
Sym to Matlab Function