# Differentiation and integration

In MATLAB, you can differentiate and integrate symbolic expressions. We illustrate these commands by means of an example:
>> syms x y
>> y = atan(x)

Differentiating with respect to , respectively, once and three times:
>> diff(y,x)

ans =
1/(1+x^2)

>> diff(y,x,3)

ans =
8/(1+x^2)^3*x^2-2/(1+x^2)^2

The indefinite integral :
>> int(y,x)

ans =
x*atan(x)-1/2*log(x^2+1)

The definite integral :
>> int(y,x,2,7)

ans =
7*atan(7)-1/2*log(2)-1/2*log(5)-2*atan(2)

This is the exact value of the integral. The numerical value is obtained by
>> double(ans)

ans =
6.6367

The command double gives the numerical value of an exact number.

By far most integrals cannot be calculated explicitly. Consider the example of trying to calculate the integral of  ' over the interval [1,4].

>> int(exp(-x)*sqrt(1+x^3),x,1,4)

ans =
int(exp(-x)*(1+x^3)^(1/2),x = 1..4)

>> double(ans)

ans =
1.0017

MATLAB returns the integral, because it cannot calculate it explicitly. The function double` gives a numerical approximation of the integral.

Esteur 2010-03-22