computer applications lab computer applications lab lab 9
play

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


  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

  2. Outline Outline � Numerical Calculus ◦ Integration ◦ differentiation � Symbolic Processing ◦ Defining symbolic expressions ◦ Manipulation of symbolic expressions ◦ Symbolic differentiation, integration, and limits 2

  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

  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

  5. Numerical Calculus Numerical Calculus - - Integration Integration Function Description trapz(x,y) Uses trapezoidal integration to compute the integral of 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) quad(‘func’,a,b,tol) Uses an adaptive Simpson’s rule to compute the 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. 5

  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 sin(x)dx 2 = Matlab Matlab 0 Function Result quad(‘sin’,0,pi) 2 quad1(‘sin’,0,pi) 2 x = 0:0.01:pi 1.9797 trapz(x,sin(x)) 6

  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

  8. Numerical Calculus Numerical Calculus - - Differentiation Differentiation � Matlab provides the diff function for computing derivative estimates. It computes the backward difference. � Syntax d = diff(x) 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

  9. Numerical Calculus Numerical Calculus - - Differentiation Differentiation � 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) ; 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

  10. Numerical Calculus Numerical Calculus - - Differentiation Differentiation 1 COS(x) 0.8 Backward Derivative Central Derivative 0.6 0.4 SIN(x) 0.2 Derivative of S 0 -0.2 -0.4 -0.6 -0.8 -1 0 1 2 3 4 5 6 7 x 10

  11. Numerical Calculus Numerical Calculus - - Differentiation Differentiation Command Description Returns a vector d containing the differences between adjacent d = diff(x) elements in the vector x . Returns a vector b containing the coefficients of the derivative b = polyder(p) of the polynomial represented by the vector p . Returns a vector b containing the coefficients of the polynomial b = that is the derivative of the product of the polynomials polyder(p1,p2) represented by p1 and p2 . Returns the vectors num and den containing the coefficients of [num, den] = the numerator and denominator polynomials of the derivative polyder(p2,p1) of the quotient p 2 / p 1 , where p1 and p2 are polynomials. 11

  12. Numerical Calculus - Numerical Calculus - Differentiation Differentiation � Example � F(x) = x 2 + 2x + 1 , G(x) = x 3 + 5x 2 – x + 8, find the derivative of F * G and F/G >> F = [1,2,1], G = [1,5,-1,8]; % d/dx(F*G) % 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

  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

  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

  15. Symbolic Processing Symbolic Processing – Symbolic Symbolic Expressions Expressions � We can use symbols to define symbolic expressions. We can use the operators + - / ^ 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 of the expression. of 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

  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

  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 operators. � 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

  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

  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

  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 >> sys x ; >> sym2poly(9*x^2+4) ans = 9 0 4 20

Recommend


More recommend