26
Lecture 16 Lecture 16 Symbolic Mathematics Symbolic Mathematics Symbolic mathematics: Symbolic mathematics: algebra algebra ezplot ezplot calculus calculus © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Lecture 16 Symbolic Mathematics Symbolic mathematics: algebraezplotcalculus © 2007 Daniel Valentine. All rights reserved. Published by Elsevier

Embed Size (px)

Citation preview

Lecture 16Lecture 16Symbolic MathematicsSymbolic Mathematics

Symbolic mathematics:Symbolic mathematics:

algebra algebra

ezplot ezplot

calculuscalculus

© 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

““Symbolic” toolbox allows you Symbolic” toolbox allows you to:to:

Enter expressions in symbolic form with symbolic data Enter expressions in symbolic form with symbolic data types.types.

Expand or simplify symbolic expressions.Expand or simplify symbolic expressions.

Find symbolic roots, limits, minima, maxima, etc.Find symbolic roots, limits, minima, maxima, etc.

Differentiate and integrate symbolic functions.Differentiate and integrate symbolic functions.

Generate Taylor series of functions (among other tools).Generate Taylor series of functions (among other tools).

Solve algebraic and differential equations symbolically.Solve algebraic and differential equations symbolically.

Solve simultaneous equations (even some nonlinear).Solve simultaneous equations (even some nonlinear).

Do variable precision arithmetic.Do variable precision arithmetic.

Create graphical representations of symbolic functions. Create graphical representations of symbolic functions.

syms x;syms x;

x = sym('x');x = sym('x');

Creating Symbolic VariablesCreating Symbolic Variables

In order to use the symbolic toolbox, In order to use the symbolic toolbox, you must create you must create symbolic variablessymbolic variables..

To create one symbolic variable, type:To create one symbolic variable, type:

You can also use the You can also use the symssyms command:command:

syms K T P0;syms K T P0;

Creating Multiple VariablesCreating Multiple Variables

To create several symbolic variables To create several symbolic variables use the syms command as follows:use the syms command as follows:

This creates the symbolic variables This creates the symbolic variables K, T,K, T, and and P0P0; execute the ; execute the command command whoswhos to check that this is to check that this is indeed the case.indeed the case.

P = P0*exp(K*T)P = P0*exp(K*T)

Creating a symbolic expression Creating a symbolic expression with symbolic variableswith symbolic variables

To create an expression using To create an expression using existing symbolic variables, type:existing symbolic variables, type:

This creates a symbolic expression This creates a symbolic expression that includes the exponential function. that includes the exponential function. It could represent the exponential It could represent the exponential growth of a population.growth of a population.

E = E = sym('msym('m*c^2')*c^2')

Creating an expression with Creating an expression with sym() sym()

You can also create symbolic You can also create symbolic expressions with the expressions with the sym()sym() command: command:

ideal_gas_law = sym('P*V = n*R*Temp')ideal_gas_law = sym('P*V = n*R*Temp')

Symbolic equationsSymbolic equations

It is possible to write equations with the It is possible to write equations with the symbolic toolbox. Type the following:symbolic toolbox. Type the following:

This equation is the ideal gas law.This equation is the ideal gas law.

Manipulating symbolic Manipulating symbolic expressionsexpressions

numden()numden() expand()expand() factor()factor() collect()collect() simplify()simplify() simple()simple() poly2sym()poly2sym()

y=2*(x + 3)^2/(x^2 + 6*x + 9)y=2*(x + 3)^2/(x^2 + 6*x + 9)

[numerator, denominator]=numden(y)[numerator, denominator]=numden(y)

numden()numden()

The The numden()numden() command is used to command is used to separate the numerator and denominator separate the numerator and denominator of a quotient. of a quotient.

Use numden() on the following expression:Use numden() on the following expression:

numerator=numerator=2*(x + 3)^22*(x + 3)^2denominator=denominator=(x^2 + 6*x + 9)(x^2 + 6*x + 9)

expand(numeratorexpand(numerator)

expand()expand()

expand()expand() is used to expand an is used to expand an expression by expanding the expression by expanding the products of factors in an expression.products of factors in an expression.

Expand the numerator of y:Expand the numerator of y:

ans=ans=2*x^2 + 12*x + 182*x^2 + 12*x + 18

factor(denominatorfactor(denominator))

factor()factor() The The factor()factor() command is used to factor command is used to factor

an expression into a product of terms.an expression into a product of terms.

Example: Factor the expression for the Example: Factor the expression for the denominator.denominator.

ans=ans=(x + 3)^2(x + 3)^2

b=sym('3*a b=sym('3*a -- (a + 3)*(a (a + 3)*(a -- 3)^2');3)^2');

simplify(bsimplify(b))

simplify()simplify() The The simplify()simplify() command uses the command uses the

Maple simplification algorithm to Maple simplification algorithm to simplify each part of an expression.simplify each part of an expression.

Enter the expression:Enter the expression:

ans=ans=12*a - a^3 + 3*a^2 - 2712*a - a^3 + 3*a^2 - 27

simple(bsimple(b))

simple()simple()

The The simple()simple() command executes many command executes many techniques to simplify an expression; techniques to simplify an expression; its its ansans is the simplest expression. is the simplest expression.

ans = ans = 3*a - (a + 3)*(a - 3)^23*a - (a + 3)*(a - 3)^2

Note: The simple() command displays the results of Note: The simple() command displays the results of all simplification methods; only the final result, all simplification methods; only the final result, i.e., the answer (i.e., the answer (ansans) is shown here.) is shown here.

a=[1,3,2];a=[1,3,2];

b=poly2sym(a)b=poly2sym(a)

poly2sym()poly2sym() The The poly2sym()poly2sym() function uses an array of function uses an array of

coefficients to create a polynomial:coefficients to create a polynomial:

b=b=x^2 + 3*x + 2x^2 + 3*x + 2

The function The function sym2poly()sym2poly() is the inverse is the inverse of poly2sym().of poly2sym().

E1=x^2 E1=x^2 -- 99

solve(E1)solve(E1)

solve()solve() The The solve()solve() function sets an function sets an

expression equal to zero, then expression equal to zero, then solves the equation for its roots.solves the equation for its roots.

ans=ans=33-3-3

solve('asolve('a*x^2 + b*x + c')*x^2 + b*x + c')

ans = ans = 1/2/a*( -b + (b^2 - 4*a*c)^(1/2))1/2/a*( -b + (b^2 - 4*a*c)^(1/2))1/2/a*( -b - (b^2 - 4*a*c)^(1/2))1/2/a*( -b - (b^2 - 4*a*c)^(1/2))

Note: The expression is in single quotes; if Note: The expression is in single quotes; if the symbolic variables in the expression the symbolic variables in the expression have not been defined previously, then the have not been defined previously, then the single quotes are necessary.single quotes are necessary.

Hands onHands on

solve('asolve('a*x^2 + b*x + c', 'a')*x^2 + b*x + c', 'a')

ans = ans = -(b*x + c)/x^2-(b*x + c)/x^2

Note that this solves the expression for “a”.Note that this solves the expression for “a”.

Hands onHands on

one=sym('3*x + 2*y one=sym('3*x + 2*y –– z = 10');z = 10');

two=sym('two=sym('--x + 3*y + 2*z = 5');x + 3*y + 2*z = 5');

three=sym('x three=sym('x –– y y –– z = z = --1');1');

[x,y,z]=solve(one,two,three)[x,y,z]=solve(one,two,three)

Systems of equationsSystems of equations You can use solve() to find the You can use solve() to find the

solution of a system of equations.solution of a system of equations.

x=x=-2-2

y=y=55

z=z=-6-6

Note that the solve function produces symbolic output. You Note that the solve function produces symbolic output. You can change the output to numerical values with thecan change the output to numerical values with the double() double() command.command.

syms a b c x y;syms a b c x y;quadratic = a*x^2 + b*x + c;quadratic = a*x^2 + b*x + c;yquadratic = subs(quadratic,x,y)yquadratic = subs(quadratic,x,y)

Substitution with subs()Substitution with subs() The The subs()subs() command allows you to command allows you to

substitute a symbol with another symbol substitute a symbol with another symbol or assign a number to a variable.or assign a number to a variable.

yquadratic =yquadratic =

a*y^2 + b*y + ca*y^2 + b*y + c

subs(quadraticsubs(quadratic, {, {a,b,c,xa,b,c,x}, {1,2,3,4})}, {1,2,3,4})

Multiple substitutionsMultiple substitutions

You can use the subs() command to You can use the subs() command to do multiple substitutions in one do multiple substitutions in one command. This is done by grouping command. This is done by grouping the variables and their substitutes the variables and their substitutes (other variables or numerical values) (other variables or numerical values) in braces. in braces.

ans =ans =2727

subs(symbolic_functionsubs(symbolic_function, {substitutant}, {substitute}), {substitutant}, {substitute})

y = sym('x^2 y = sym('x^2 –– 2*x + 3');2*x + 3');ezplot(yezplot(y))

Plotting symbolical Plotting symbolical functionsfunctions

Plotting symbolic functions in MATLAB Plotting symbolic functions in MATLAB is done with the is done with the ezplot()ezplot() set of set of commands. commands.

The syntax of ezplot() when y is a The syntax of ezplot() when y is a function of x is:function of x is:ezplot(y)ezplot(y)

-6 -4 -2 0 2 4 6

0

10

20

30

40

50

x

x2-2 x+3

diff(y,'x',1)diff(y,'x',1)

DifferentiationDifferentiation MATLAB allows you to differentiate MATLAB allows you to differentiate

symbolic functions with the symbolic functions with the diff()diff() command.command.

The syntax of diff(), when f is a The syntax of diff(), when f is a symbolic function of the variable x symbolic function of the variable x and n is the order of the derivative to and n is the order of the derivative to be determined, is:be determined, is:diff(f,'x'diff(f,'x'

,n),n)

ans = ans = 2*x-22*x-2

Try a second derivative. Your Try a second derivative. Your result should be 2.result should be 2.

int(y,'xint(y,'x')')

IntegrationIntegration MATLAB allows you to integrate MATLAB allows you to integrate

symbolic functions with the symbolic functions with the int()int() command. Either definite integrals, command. Either definite integrals, with assigned numeric or symbolic with assigned numeric or symbolic bounds, or indefinite integrals bounds, or indefinite integrals (note that (note that when you compute indefinite integrals, the constant of when you compute indefinite integrals, the constant of integration does integration does notnot appear explicitly). appear explicitly).int(y,'x',a,b)int(y,'x',a,b)

ans = ans = 1/3*x^3-x^2+3*x1/3*x^3-x^2+3*x

Try a definite integral for Try a definite integral for 1<x<2. The result should be 7/3.1<x<2. The result should be 7/3.

Integration constantIntegration constant

If you want to display the integration If you want to display the integration constant when applying the int() constant when applying the int() function, you can do the following:function, you can do the following: syms x y asyms x y a

y = 2*x;y = 2*x;

int(y,’a’,’x’)int(y,’a’,’x’)

ans = ans =

x^2–a^2x^2–a^2

ExercisesExercises

Use the symbolic toolbox to solve the Use the symbolic toolbox to solve the following system of equations:following system of equations:x + 2y - z = 4x + 2y - z = 43x + 8y + 7z = 203x + 8y + 7z = 202x + 7y + 9z = 232x + 7y + 9z = 23

The velocity of a car is v = t^2 – 3t + 5. Find The velocity of a car is v = t^2 – 3t + 5. Find the displacement for 1<t<5 and the the displacement for 1<t<5 and the acceleration at t=1.5. Plot the equations for acceleration at t=1.5. Plot the equations for distance, velocity, and acceleration on one distance, velocity, and acceleration on one graph.graph.

SummarySummary Creating Symbolic ExpressionsCreating Symbolic Expressions

– sym(‘x’), syms x, expressions i.e. e=sym(‘m*c^2’)sym(‘x’), syms x, expressions i.e. e=sym(‘m*c^2’) ManipulationManipulation

– numden, expand, factor, collect, simplify, simple, numden, expand, factor, collect, simplify, simple, poly2sympoly2sym

SolutionsSolutions– solve, subssolve, subs

PlottingPlotting– ezplotezplot

DifferentiationDifferentiation– diff(y, ‘x’, n)diff(y, ‘x’, n)

IntegrationIntegration– int(y, ‘x’, a, b)int(y, ‘x’, a, b)