Computer Applications Lab Computer Applications Lab Lab 9 Lab 9 - - PowerPoint PPT Presentation

computer applications lab computer applications lab lab 9
SMART_READER_LITE
LIVE PREVIEW

Computer Applications Lab Computer Applications Lab Lab 9 Lab 9 - - PowerPoint PPT Presentation

Computer Applications Lab Computer Applications Lab Lab 9 Lab 9 Numerical Calculus and Symbolic Numerical Calculus and Symbolic Processing Processing Chapter 8 Chapter 8 - Sections 8.1 through 8.3 Sections 8.1 through 8.3 Chapter 10


slide-1
SLIDE 1

Computer Applications Lab Computer Applications Lab Lab 9 Lab 9

Numerical Calculus and Symbolic Numerical Calculus and Symbolic Processing Processing

Chapter 8 Chapter 8 - Sections 8.1 through 8.3 Sections 8.1 through 8.3 Chapter 10 Chapter 10 - Sections 10.1 through 10.3 Sections 10.1 through 10.3

  • Dr. Iyad Jafar

Adapted from the publisher slides

slide-2
SLIDE 2

Outline Outline

Numerical Calculus

  • Integration
  • differentiation

Symbolic Processing

  • Defining symbolic expressions
  • Manipulation of symbolic expressions
  • Symbolic differentiation, integration, and limits

2

slide-3
SLIDE 3

Numerical Calculus Numerical Calculus -

  • Integration

Integration

Integration of function f(x) in the interval a ≤ x ≤

b can be represented as the area between f(x) and the x-axis.

3

slide-4
SLIDE 4

Numerical Calculus Numerical Calculus -

  • Integration

Integration

Numerical integration can be approximated by

partitioning the area into rectangles or trapezoidals then summing up their areas.

4

slide-5
SLIDE 5

Numerical Calculus Numerical Calculus -

  • Integration

Integration

Function Description trapz(x,y) Uses trapezoidal integration to compute the integral

  • f y with respect to x, where the array y contains the

function values at the points contained in the array x. quad(‘func’,a,b,tol) Uses an adaptive Simpson’s rule to compute the

5

quad(‘func’,a,b,tol) Uses an adaptive Simpson’s rule to compute the integral of the function ’fun’ with a as the lower integration limit and b as the upper limit. The parameter tol is optional. tol indicates the specified error tolerance. quad1(‘fun’,a,b,tol) Uses Lobatto quadrature to compute the integral of the function ’fun’. The rest of the syntax is identical to quad.

slide-6
SLIDE 6

Numerical Calculus Numerical Calculus -

  • Integration

Integration

The quad and quadl functions are more accurate than

trapz; however, they are restricted to computing the integrals of functions and cannot be used when the integrand is specified by a set of points. For such cases, use the trapz function.

Example: the integration can be computed in

Matlab

sin(x)dx 2

π

=

Matlab

6

Function Result quad(‘sin’,0,pi) 2 quad1(‘sin’,0,pi) 2 x = 0:0.01:pi trapz(x,sin(x)) 1.9797

slide-7
SLIDE 7

Numerical Calculus Numerical Calculus -

  • Differentiation

Differentiation

The derivative of a function can be interpreted

graphically as the slope function.

Three methods can be used to estimate the slope at

a point numerically; backward difference, forward difference, and central difference.

7

slide-8
SLIDE 8

Matlab provides the diff function for computing

derivative estimates. It computes the backward difference.

Syntax

d = diff(x)

Numerical Calculus Numerical Calculus -

  • Differentiation

Differentiation

where x is a vector of values and the result is a vector d containing the differences between adjacent elements in x. if x has n elements, then d is n-1 elements.

Example:

>> x = [5,7,12,-20]; >> y = diff(x) y = 2 5 -32

8

slide-9
SLIDE 9

Example: compare the true derivative of sin(x) with backward

and central derivatives.

x = [0:pi/50:2*pi] ; n = length(x); y = sin(x);

% true derivative

td = cos(x) ;

Numerical Calculus Numerical Calculus -

  • Differentiation

Differentiation

td = cos(x) ;

% numerical derivative backward difference

nd = diff(y) ./ diff(x) ;

% numerical derivative using central difference

ncd = (y(3:n)-y(1:n-2))./ (x(3:n)-x(1:n-2)) ; plot(x,td,'b'), ;hold on plot(x(2:n),nd,'ro'), plot(x(3:n),ncd,'g*') ; xlabel('x'), ylabel('Derivative of SIN(x)') legend('COS(x)','Backward Derivative','Central Derivative')

9

slide-10
SLIDE 10

0.2 0.4 0.6 0.8 1 SIN(x) COS(x) Backward Derivative Central Derivative

Numerical Calculus Numerical Calculus -

  • Differentiation

Differentiation

10 1 2 3 4 5 6 7

  • 1
  • 0.8
  • 0.6
  • 0.4
  • 0.2

x Derivative of S

slide-11
SLIDE 11

Command Description d = diff(x) Returns a vector d containing the differences between adjacent elements in the vector x. b = polyder(p) Returns a vector b containing the coefficients of the derivative

  • f the polynomial represented by the vector p.

Numerical Calculus Numerical Calculus -

  • Differentiation

Differentiation

11

b = polyder(p1,p2) Returns a vector b containing the coefficients of the polynomial that is the derivative of the product of the polynomials represented by p1 and p2. [num, den] = polyder(p2,p1) Returns the vectors num and den containing the coefficients of the numerator and denominator polynomials of the derivative

  • f the quotient p2/p1, where p1 and p2 are polynomials.
slide-12
SLIDE 12

Example

F(x) = x2 + 2x + 1 , G(x) = x3 + 5x2 – x + 8, find the

derivative of F * G and F/G

>> F = [1,2,1], G = [1,5,-1,8];

% d/dx(F*G)

Numerical Calculus Numerical Calculus -

  • Differentiation

Differentiation

% d/dx(F*G)

>> d = polyder(F,G) d = 5 28 30 22 15

% d/dx(F/G)

>> [num,denum] = polyder(F,G) num = -1 -4 -14 6 17 denum = 1 10 23 6 81 -16 64

12

slide-13
SLIDE 13

Symbolic Processing Symbolic Processing

Refers to the way that computers

perform operations on mathematical expressions the way that humans it with pencil and paper. pencil and paper.

Matlab contains many functions to define

symbols, symbolic expressions, simplification, and solution to symbolic expressions.

13

slide-14
SLIDE 14

Symbolic Processing Symbolic Processing – Defining Symbols Defining Symbols

To define a symbol in Matlab use the sym() or the

syms functions. We can create symbolic constants or symbolic variables.

Examples

  • 1. x = sym(‘x’) % creates the symbolic variable with name x
  • 2. syms x y u v % creates four symbolic variables x,y,u, and v
  • 3. fraction = sym(‘1/3’) % create symbolic constant fraction with value 1/3
  • 3. fraction = sym(‘1/3’) % create symbolic constant fraction with value 1/3

When mathematical operations are used with symbols, the result

is symbolic.

Example

>> x = sym(‘sqrt(2)’); >> 2 * x ans = 2*2^(1/2) >> class(ans) ans = sym

14

slide-15
SLIDE 15

Symbolic Processing Symbolic Processing – Symbolic Symbolic Expressions Expressions

We can use symbols to define symbolic expressions. We can use the

  • perators + - / ^ and the built-in functions.

Example

>> syms x y ; >> r = sqrt(x^2+y^2) r = (x^2+y^2)^(1/2)

If we later assign x and y to 3 and 5, typing r will not result in the evaluation

  • f the expression.
  • f the expression.

We can use the vector and matrix notation with symbols. Example

>> n = 3 , syms x, A = x.^((0:n)’*(0:n)) A = [ 1, 1, 1, 1] [ 1, x, x^2, x^3] [ 1, x^2, x^4, x^6] [ 1, x^3, x^6, x^9] >> class(A) ans = sym

15

slide-16
SLIDE 16

Symbolic Processing Symbolic Processing

The function findsym(E,n) returns the n symbolic variables in

expression E that are closest to x (default symbol in Matlab), with the tie breaker going to the variable closer to z.

>>syms b x1 y >>findsym(6*b+y) ans = b,y >>findsym(6*b+y,1) %Find the one variable closest to >>findsym(6*b+y,1) %Find the one variable closest to x ans = y >>findsym(6*b+y+x1,1) %Find the one variable closest to x ans = x1

>>findsym(6*b+y*i) %i is not symbolic ans = b, y

16

slide-17
SLIDE 17

Manipulating Symbolic Expressions Manipulating Symbolic Expressions

With symbolic expressions in Matlab, we can expand, collect, simplify, and

create new expressions from old expressions using the mathematical

  • perators.

The function collect(E) collects coefficients of like powers in the

expression E. If there is more than one variable, you can use the optional form collect(E,v), which collects all the coefficients with the same power of v. power of v.

>>syms x y >>E = (x-5)^2+(y-3)^2; >>collect(E) % will collect x powers ans = x^2-10*x+25+(y-3)^2 >>collect(E,y) % will collect y powers ans = y^2-6*y+(x-5)^2+9

17

slide-18
SLIDE 18

Manipulating Symbolic Expressions Manipulating Symbolic Expressions

The expand and simplify functions.

>>syms x y >>expand((x+y)^2) % applies algebra rules ans = x^2+2*x*y+y^2 >>expand(sin(x+y)) % applies trig identities ans = sin(x)*cos(y)+cos(x)*sin(y) >>simplify(6*((sin(x))^2+(cos(x))^2))

% applies another trig identity ans = 6

18

slide-19
SLIDE 19

Manipulating Symbolic Expressions Manipulating Symbolic Expressions

The factor function finds the factors of the expression.

>>syms x y >>factor(x^2-1) ans = (x-1)*(x+1)

  • The function subs(E,old,new) substitutes new for old
  • The function subs(E,old,new) substitutes new for old

in the expression E, where old can be a symbolic variable or expression and new can be a symbolic variable, expression, or matrix, or a numeric value or matrix. For example,

>>syms x y >>E = x^2+6*x+7; >>F = subs(E,x,y) F = y^2+6*y+7

19

slide-20
SLIDE 20

Manipulating Symbolic Expressions Manipulating Symbolic Expressions

The function poly2sym(p) converts the

coefficient vector p to a symbolic polynomial.

The function sym2poly(E) converts the

expression E to a polynomial coefficient vector.

>> ploy2sym([2,4,5]) >> ploy2sym([2,4,5]) ans = 2*x^2 + 4*x + 5 >> ploy2sym([2,4,5],y) ans = 2*y^2 + 4*y + 5

20

>> sys x ; >> sym2poly(9*x^2+4) ans = 9 0 4

slide-21
SLIDE 21

Manipulating Symbolic Expressions Manipulating Symbolic Expressions

The function [num,den] = numden(E) returns two

expressions num and den for the numerator and denominator for expression E.

>> syms x >> E1 = x^2 + 5 ; >> E2 = 1 / (x+6) ; >> E2 = 1 / (x+6) ; >> [num,den] = numden(E1+E2) num = x^3 + 6*x^2 + 5*x + 31 den = x + 6 >> pretty(num) % provides mathematical friendly

expression

3 2 x + 6 x + 5 x + 31

21

slide-22
SLIDE 22

Evaluating Symbolic Expressions Evaluating Symbolic Expressions

Use the subs and double functions to evaluate an

expression numerically. Use subs(E,old,new) to replace old with a numeric value new in the expression E. The result is of class double. For example,

% evaluate symbolic expressions >>syms x >>E = x^2+6*x+7; >>G = subs(E,x,2) G = 23 >>class(G) ans = double

22

% evaluate constant expressions >> sqrt2 = sym(sqrt(2)); >> y = 6 * sqrt2 y = 6 * 2^(1/2) >> double(y) ans = 8.4853

slide-23
SLIDE 23

Plotting Expressions Plotting Expressions

The Matlab function ezplot(E) generates a

plot of a symbolic expression E, which is a function of one variable. The default range of the independent variable is the interval [−2π, 2π]. 2 ].

The optional form ezplot(E,[xmin

xmax]) generates a plot over the range from xmin to xmax.

The ezplot function accepts E as string also. The ezplot function does not accept the

specification of line styles and data markers.

23

slide-24
SLIDE 24

40 50 60 70 80 90 x2-6 x+7

ezplot(x^2-6*x+7)

24

  • 6
  • 4
  • 2

2 4 6 10 20 30 x

slide-25
SLIDE 25

The Solve Function The Solve Function

It is used to find the solution of an equation, i.e.

the zeros of the expression.

>> eq1 = ‘x+5’ >> solve(eq1) ans = -5 >> solve(‘exp(2*x)+3*exp(x)=54’) ans = [log(-9)] [log(6) ] >> eq3 = ’x^2+9*y^4=0’; >>solve(eq3) %Note that x is presumed to be the unknown variable ans = [ 3*i*y^2] [-3*i*y^2]

25

slide-26
SLIDE 26

The Solve Function The Solve Function

We can solve an equation in terms of a symbol Example:

>> solve (‘b^2+8*c+2*b=0’) ans = -1/8*b^2-1/4*b >> solve (‘b^2+8*c+2*b=0’,’b’) % solve for b ans = [-1+(1-8*c)^(1/2)] [-1-(1-8*c)^(1/2)]

We can solve more than equation

>> eq1 = ‘6*x+2*y=14’; >> eq2 = ‘3*x+7*y=31’; >> solve(eq1,eq2) ans = x: [1x1 sym] % the answer is a structure y: [1x1 sym] >> x = ans.x x = 1 >> y = ans.y y = 4

26

slide-27
SLIDE 27

Evaluating Symbolic Expressions Evaluating Symbolic Expressions

Example

>> syms w x y z ; >> M = w - x^2 + log(y) + 5*z ; >> % evaluate M with y = 5 and z = 1 >> N = subs(M,[y,z],[5 1]) ans = w-x^2+log(5)+5 ans = w-x^2+log(5)+5 >> % solve in terms of w i.e. solve for x >> G = solve(N,x) % the result is symbolic array G = (w+log(5)+5)^(1/2)

  • (w+log(5)+5)^(1/2)

>> % evaluate the first root if w = 6 >> subs(G(1),w,6) ans = 3.5510

27

slide-28
SLIDE 28

Solving Symbolic Expressions Solving Symbolic Expressions

Example: find the intersection of y = x2 and y = 2x

>> syms x ; >> y1 = x^2 ; >> y2 = 2*x ; >> solve(y1-y2) ans = ans = 2

Example: if x + 6y = a and 2x – 3y = 9, then find the solution for x and y in

terms of the parameter a. >> syms a x y ; >> [x,y] = solve('x+6*y = a','2*x-3*y = 9') x = 1/5*a+18/5 y = 2/15*a-3/5

28

slide-29
SLIDE 29

Symbolic Differentiation Symbolic Differentiation

We can use the diff function to perform symbolic

differentiation by passing the expression directly, as a string, or as a symbolic expression.

>> diff(‘sin(x*y)’) % differentiation with respect to

x

ans = cos(x*y)*y >> syms x y ; % differentiation with respect to y >> diff(sin(x*y),y) ans = x*cos(x*y) >> diff(x*sin(x*y),y,2) % Computes the second derivative ans = -x^3*sin(x*y)

29

slide-30
SLIDE 30

Symbolic Integration Symbolic Integration

We can use the int function to perform symbolic

integration by passing the expression directly, as a string,

  • r as a symbolic expression.

>> int(2*x) % x is defined as a symbol ans = x^2 >> int(sin(x*y),y) % integration with respect to y >> int(sin(x*y),y) % integration with respect to y ans = -1/x*cos(x*y) >> int(sin(x*y),y,0,1) % integrates with respect to y over [0,1] ans = -(-1+cos(x))/x >> int(sin(x*y),0,1) %integrates with respect to x

  • ver [0,1]

ans = -(-1+cos(y))/y

30

slide-31
SLIDE 31

Limits Limits

We can use the limit function to find the limits of

expressions.

>> limit(sin(a*x)/x) % limit as x goes to 0 ans = a >> limit(sin(a*x)/x,3) % limit as x goes to 3 >> limit(sin(a*x)/x,3) % limit as x goes to 3 ans = 1/3*sin(3*a) >> limit(sin(a*x)/x,a,3) % limit as a goes to 3 ans = sin(3*x)/x >> limit((1/x),x,0,’right’) % limit as x goes to 0

from right

ans = Inf

31