1. PRACTICAL
Name- Saloni Singhal
M.Sc. (Statistics) II-Sem.
Roll No: 2046398
Course- MATH-409 L
Numerical Analysis Lab
Submitted To: Dr. S.C. Pandey
2. OBJECTIVE
1. Create an M-file to implement
Trapezoidal Rule.
2. Verify Error can be reduced by
breaking the curve into parts.
3. Theory
Under this rule, the area under a curve is evaluated by dividing the
total area into little trapezoids rather than rectangles.
Let f(x) be continuous on [a,b]. We partition the interval [a,b] into n
equal subintervals, each of width Δx= b−a/n,
such that a=x0<x1<x2…<xn=b
Trapezoidal rule is given by:
As n approaches infinity it becomes equal to the given definite
integral
4. Error in Trapezoidal
Rule
Local Error
Global Error
Global Error O(h2)is sum total of all error and is
of an order less than Local error O(h3)
5. Script
File
a=input('Enter lower limit: ');
b=input('Enter upper limit: ');
if ~(b>a)
error('upper bound must be greater than lower');
end
f=@(x)exp(x) %function
trueval=integral(f,a,b)
err=zeros(1,5)
for i=1:5
n=2^i
[I,xv,fv]=trap(n,a,b)
err(i)=abs(I-trueval)
plot(xv,fv)
hold on
end
hold off
%plot([2,4,8,16,32],err)
function [I,xv,fv]=trap(n,a,b)
f=@(x)sin(x)
h=(b-a)/n;
%average of first and last term
s=0.5*(f(a)+f(b));
fv=zeros(1,n+1);
xv=zeros(1,n+1);
fv(1)=f(a);
xv(1)=a;
fv(n+1)=f(b);
xv(n+1)=b;
%summation of middle terms
for i=1:n-1
s=s+f(a+i*h);
fv(i+1)=f(a+i*h);
xv(i+1)=a+i*h;
%final integral value
end
I=h*s
end
7. Plot of no. of subintervals vs error
Error for n=2,4,8,16,32
8. Conclusion
• Error is dependent upon the curvature of the actual
function as well as the distance between the points.
• Error can thus, be reduced by breaking the curve into
parts i.e. increasing no of subintervals
• The simplicity of the trapezoidal rule makes it an ideal
for many numerical integration tasks. Also, the
trapezoidal rule is exact for piecewise linear curves
such as an ROC curve
9. Caveats
• the coefficients only make sense when the abscissae are
evenly spaced, limiting the use of the approximation to
evenly spaced function evaluations.
• A drawback of this rule is since it uses straight line approx.
function, error is related to the second derivative of the
function.
• More complicated approximation formulas can improve the
accuracy for curves - these include using (a) 2nd and (b) 3rd
order polynomials.
• The formulas that result from taking the integrals under these
polynomials are called Simpson’s rules as discussed further