You are on page 1of 5

University of Dhaka

Department of Electrical and Electronic Engineering


EEE-3102: Numerical Technique Laboratory

University of Dhaka
Department of Electrical and Electronic Engineering
EEE-3102: Numerical Technique Laboratory

Section: B-11
Class Roll: SH-073-027
Name:Mahabub-A-Alahi
Date:18-04-2019

Lab-6 Numerical integration using Matlab

Things to do: Read the theory carefully and answer all the questions from Question 1 to Question8
for the given Matlab code. After practicing the matlab example, perform exercise 1 to exercise 3.

Theory for numerical integration:


The integral of a function y = 𝑓(𝑥) is denoted as
b
A   f ( x)dx
a

An integral can be seen as the area (A) under a curve.

Trapezoid rule for numerical integration:


For a function 𝑦=𝑓(𝑥), the approximation of the area (A) under the curve can be found by dividing the
area up to a number of rectangles and then summing the contribution from all the rectangles.
n 1
( yi 1  yi )
A   ( xi 1  xi ).
i 1 2
This process is known as the trapezoid rule.The integral can be approximated using n trapezoids
formed by using straight line segments between the points (xi-1, yi-1) and (xi, yi) for 1 ≤I ≤ n as shown
in the figure below:

AKA/EEE-3102/Laboratory-6/27 March 2018 Page 1


University of Dhaka
Department of Electrical and Electronic Engineering
EEE-3102: Numerical Technique Laboratory

The area of a trapezoid is obtained by adding the area of a rectangle and triangle:

1 (y  y )
A  y0x  ( y1  y0 )x  1 0 x
2 2
Example: Given the function

y  x2
The exact integral of the function is
b
b
 x3  1 3
A   x dx     (b  a3 )
2

a  3 a 3
The integral from a = 0 to b = 3 is
1
A  (33 )  9
3

AKA/EEE-3102/Laboratory-6/27 March 2018 Page 2


University of Dhaka
Department of Electrical and Electronic Engineering
EEE-3102: Numerical Technique Laboratory

Matlab implementation of Trapezoid rule for numerical integration


Trapezoid rule and diff function in matlab can be used to solve the numerical integral of function y =
x2 from 0 to 3:
clear all; close all; clc;
x=0:1:3;
y=x.^2;
avg_y = y(1:length(x)-1) + diff(y)/2;
A = sum(diff(x).*avg_y)

Question1: What is the integral A?


Ans:9.5000
Question2: Run the above code and find the results for x=0:0.5:3; x=0:0.1:3; and x=0:0.01:3;
Ans(for x=0:0.5:3):9.1250
Ans(for x=0:0.1:3):9.0050
Ans(for x=0:0.01:3):9.0000

Question3: Why the results are different? When the results become more accurate?
Ans:The results are different because the value of the x vector,The result has become more accurate
when the step among the values of x vector has taken very small as x=0:0.01:3.
Matlab built-in function for numerical integration:
1. Integral: MATLAB also has a variety of numerical integrators, of which the standard one is
‘integral’. To compute a numerical approximation of the definite integral, we can use the following
command ‘integral(f,a,b)’

Example: To find the numerical integral offunction y  x 2 from 0 to 3, we use the following
command
y=@(x) x.^2;
A=integral(y,0,3)

Question4: What is the value of A obtained after running the above code? Is it accurate?
Ans:9. Yes the value of A obtained after the running the avobe code is accurate.
2. Quadrature: Quadrature is a numerical method used to find the area under the graph of a function,
that is, to compute a definite integral.

AKA/EEE-3102/Laboratory-6/27 March 2018 Page 3


University of Dhaka
Department of Electrical and Electronic Engineering
EEE-3102: Numerical Technique Laboratory

a) Simpson quadrature:‘A=quad(y,a,b)’ tries to approximate the integral of


function y from a to b within an error of 1e-6 using recursive adaptive Simpson quadrature.

Example:To find the numerical integral of function y  x 2 from 0 to 3, we use the following
command:
y=@(x) x.^2;
A=quad(y,0,3)
Question5: What is the value of A obtained after running the above code? Is it accurate?
Ans:9.0000.yes the value is accurate.

b) Lobatto quadrature:A = quadl(y,a,b) approximates the integral of function y from a to b,


to within an error of 10-6 using recursive adaptive Lobatto quadrature.

Example: To find the numerical integral of function y  x 2 from 0 to 3, we use the


following command:
y=@(x) x.^2;
A=quadl(y,0,3)
Question6: What is the value of A obtained after running the above code? Is it accurate?
Ans:9.0000.yes the result is accurate.
c) Double integral: A = dblquad(f,xmin,xmax,ymin,ymax) calls the quad function to evaluate
the double integral of function z=f(x,y) over the rectangle xmin≤ x ≤xmax, ymin≤ y ≤ymax.

Example: To find the numerical integral of function z  x 2 y for 0 ≤ x ≤ 3 and 1 ≤ y ≤ 2,


we use the following command:
z=@(x,y)(x.^2).*y;
A=dblquad(z,0,3,1,2)

Question7: What is the exact integral of function z?


Ans:13.5
Question8: What is the result obtained after running the above code?
Ans:13.5000

AKA/EEE-3102/Laboratory-6/27 March 2018 Page 4


University of Dhaka
Department of Electrical and Electronic Engineering
EEE-3102: Numerical Technique Laboratory

Exercise
Exercise-1: Find the numerical integration of the following function
5
y
x  2x2  x  3
3

from 0 to 5 using (a) trapezoid rule, (b) ‘integral’ matlab function, (c) ‘quad’ matlab function, and (d)
‘quadl’ matlab function.
Ans(a):2.3634
Ans(b):2.3760
Ans(c):2.3760
Ans(d):2.3760

Exercise-2: Find the numerical integration of the following function


y  sin( x 2 )
from 1 to 2 using (a) trapezoid rule, (b) ‘integral’ matlab function, (c) ‘quad’ matlab function, and (d)
‘quadl’ matlab function.
Ans(a):0.7735
Ans(b):0.4945
Ans(c):0.4945
Ans(d):0.4945

Exercise-3:Find the numerical double integral of the following function


f  x cos y  y sin x
for π ≤ x ≤ 2π and 0 ≤ y ≤ π, from 1 to 2 using dblquadmatlab function
Ans:-9.8696

Best of Luck !!!!

AKA/EEE-3102/Laboratory-6/27 March 2018 Page 5

You might also like