Derivative and Integration

Derivative

diff is differentiate symbolic expression or function.

diff(F) % differentiates F with respect to the variable determined by symvar(F,1).
      diff(F,var) % differentiates F with respect to the variable var.
      diff(F,n) % computes the nth derivative of F with respect to the variable determined by symvar.
      diff(F,var,n) % computes the nth derivative of F with respect to the variable var.
      diff(F,var1,...,varN) % differentiates F with respect to the variables var1,...,varN.

Differentiate function

Find the derivative of the function sin(x^2).

syms f(x)
      f(x) = sin(x^2);
      df = diff(f,x)

Differentiation with respect to particular variable

Find the first derivative of this expression:

syms x t
      diff(sin(x*t^2))

Now, find the derivative of this expression with respect to the variable t:

diff(sin(x*t^2), t)

Higher-Order derivatives of univariate expression

Find the 4th, 5th, and 6th derivatives of this expression:

syms t
      
      d4 = diff(t^6,4)
      d5 = diff(t^6,5)
      d6 = diff(t^6,6)

Higher-Order derivatives of multivariate expression with respect to particular variable

Find the second derivative of this expression with respect to the variable y:

syms x y
      diff(x*cos(x*y), y, 2)

Compute the second derivative of the expression xy. If you do not specify the differentiation variable, diff uses the variable determined by symvar. For this expression, symvar(xy,1) returns x. Therefore, diff computes the second derivative of x*y with respect to x.

syms x y
      diff(x*y, 2)

If you use nested diff calls and do not specify the differentiation variable, diff determines the differentiation variable for each call. For example, differentiate the expression x*y by calling the diff function twice:

diff(diff(x*y))

Mixed Derivatives

Differentiate this expression with respect to the variables x and y:

syms x y
      diff(x*sin(x*y), x, y)

We can also compute mixed higher-order derivatives by providing all differentiation variables:

syms x y
      diff(x*sin(x*y), x, x, x, y)

Integration

Definite integral

quad and quadl are used command of definite integral. Basically, quadl is more powerful than quad. If fun is a symbolic expression, then

quadl(fun, a, b) 
      % fun should be added with ''; a and b are upbound and lowbound of integration

Compute definite integral

quadl('sin(x)', 0, pi)
ans =
      
          2.0000

To calculate the definite integral

quad('x.*log(2 - x)', 0, 1)
ans =
      
          0.1363

Indefinite integral

If f is a symbolic expression, then type

int(f)

int(f) attempts to find another symbolic expression, Fun, so that diff(F) = f. That is, int(f) returns the indefinite integral or antiderivative of f (provided one exists in closed form).

Uses the symbolic object vi as the variable of integration, rather than the variable determined by symvar.

int(f, vi)

Here are some examples to show how int works.

To calculate the integral

syms x
      f = 1 / x;
      F = int(f, x)
F =
       
      log(x)

int can also be used to calculate the definite integral

double(int(x.* log(2 - x), 0, 1))
ans =
      
          0.1363

However, if you try to calculate the integral without assigning a value to a, MATLAB assumes that a represents a complex number, and therefore returns a piecewise answer that depends on the argument of a. If you are only interested in the case when a is a positive real number, use assume to set an assumption on a. The assume function is especially useful when you run do integral for the distribution function.

syms a
      assume(a > 0)

Now you can calculate the preceding integral using the commands

syms x
      f = exp(-a*x^2);
      q = int(f, x, -inf, 2);
      double(q)

This returns

ans =
       
      (pi^(1/2)*(erf(2*a^(1/2)) + 1))/(2*a^(1/2))


Previous | Up | Next