You are on page 1of 4

Pharos University in Alexandria

Faculty of Engineering Fall 2022-2023


Department of Basic Sciences

EB103 (Engineering Mathematics 3)


MATLAB Level 3_Session 5_ Integration (Single and Double)

 Objectives:
- Recall indefinite & definite integration.
- Use MATLAB to evaluate double integrals.

 Integration:
Syntax (1):

int (f) : Compute definite and indefinite integrals analytically, F, so that diff(F) = f.
That is, int (f) returns the indefinite integral or antiderivative of function similar to differentiation.

int (f,v) : uses the symbolic object v as the variable of integration.

Note: We must use syms command for constructing symbolic variables before using int.

 Indefinite & definite Integrals

Example (1):


Evaluate:  x sec xdx,  cos
2
x dx,  tan 3 xdx,  x sin 4 xdx.
0
Solution:

MATLAB commands Output In Command Window


syms x
int(x*(sec(x))^2,x) ans = log(cos(x)) + x*tan(x)
int(cos(sqrt(x)),x) ans =
2*cos(x^(1/2)) +
2*x^(1/2)*sin(x^(1/2))
int(tan(x)^3,x) ans = log(cos(x)) - (cos(x)^2
- 1)/(2*cos(x)^2)
int(x*sin(4*x),x,0,pi) ans = -pi/4 or -0.7854

Page 1 of 4
Pharos University in Alexandria
Faculty of Engineering Fall 2022-2023
Department of Basic Sciences

Example (2):
b
Write an M-file that evaluates 
a
f ( x)dx using if statement to ensure that a is less than b and displays
the result or an error message if condition is not matched.

Solution:

M-file Run In Command Window


syms x >>Example_2:
f=input('Enter The function f(x): '); Enter The function f(x): x^3+1
a=input('Enter lower limit a: '); Enter lower limit a: 0
b=input('Enter upper limit b: '); Enter upper limit b: 3
if a < b The result =
r=int(f,x,a,b); 93/4
disp('The result = ');
disp(r); >>Example_2:
else Enter The function(x): x^3+1
error('Invalid inputs') Enter lower limit a: 3
end Enter upper limit b: 0
Error using Example_2 (line 10)
Invalid inputs

Syntax (2):

integral(fun,xmin,xmax) for numerical integration where fun Integrand, specified as a function handle,
which defines the function to be integrated from xmin to xmax.

MATLAB commands Output In Command Window


syms x
integral(@(x)x.*sin(4*x),0,pi) ans = -0.7854

Note: The operator .* is element-wise multiplication or array multiplication.

Page 2 of 4
Pharos University in Alexandria
Faculty of Engineering Fall 2022-2023
Department of Basic Sciences

Example (3):

Calculate the integral  e  ax dx


Note: 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.
Hint: assume( condition): Sets assumption on symbolic object and states that condition is valid for all
symbolic variables in condition. It also removes any assumptions previously made on these symbolic
variables.
Solution:
M-File Run In Command Window
syms a x
assume(a > 0)
f = exp(-a*x^2);
int(f, x, -inf, inf) ans = pi^(1/2)/a^(1/2)

 Double Integration

integral2: Numerically evaluate double integral

Syntax : integral2(fun,xmin,xmax,ymin,ymax),which approximates the integral of the function


z = fun(x,y) over the planar region xmin ≤ x ≤ xmax and ymin(x) ≤ y ≤ ymax(x).

Input Arguments description:

fun: Integrand, specified as a function handle, defines the function to be integrated


over xmin ≤ x ≤ xmax and ymin(x) ≤ y ≤ ymax(x). The function fun must accept two arrays of the same
size and return an array of corresponding values. It must perform element-wise operations.

Xmin: Lower limit of x, specified as a real scalar(number) value that is either finite or infinite.

Xmax: Upper limit of x, specified as a real scalar value that is either finite or infinite.

Ymin: Lower limit of y, specified as a real scalar value that is either finite or infinite. You can specify
ymin to be a function handle (a function of x) when integrating over a nonrectangular region.

Ymax: Upper limit of y, specified as a real scalar value that is either finite or infinite. You also can
specify ymax to be a function handle (a function of x) when integrating over a nonrectangular region.

Page 3 of 4
Pharos University in Alexandria
Faculty of Engineering Fall 2022-2023
Department of Basic Sciences

Example (4):

Evaluate the following integrals:


2 1
a.  (x + y)d x
0 0
dy

 /2 1

  cos x d y d x
b. 0 cos x
5 x

 x (1+ y ) d y d x
c. 0 0
2 2


2
ex d y d x
d. 0 y

Solution:

MATLAB commands Output In Command Window


syms x y
fun = @(x,y) x+y fun = @(x,y)(x+y)
integral2(fun,0,1,0,2) ans = 3.0000
syms x y
ymin = @(x)cos (x) ymin = @(x)cos (x)
integral2(@(x,y)cos(x),0,pi/2,ymin,1) ans = 0.2146
ymax = @(x)x ymax = @(x)x
integral2(@(x,y) x.*(1+y),0,5,0,ymax) ans = 119.7917
ymin = @(y) y ymin = @(y) y
integral2(@(x,y)exp(x.^2),0,2,ymin,2) ans = 6.1062

© By Engineering Mathematics Group

Page 4 of 4

You might also like