University of Engineering & Technology LHR
(FAISALABAD CAMPUS) by FAISAL SAEED
Trapezoidal Rule
function trapezoidalrule
x0=input('Enter the value of initial input: ');
xn=input ('Enter the value of final input: ');
n=input ('Enter the total no of intervals : ');
h=(xn-x0)/n
f=input('Enter the function to be evaluated:');
sum=0;
for i=x0:h:xn
if(i==x0 || i==xn)
continue;
end
sum=sum +f(i);
end
sum=sum*2;
trapezoidal=h/2*(f(x0)+f(xn)+sum)
end
Output:-
Simpsons 1/3 Rule
function simpsonsrule
x0=input('Enter the value of initial input: ');
xn=input ('Enter the value of final input: ');
n=input ('Enter the total no of intervals : ');
h=(xn-x0)/n
f=input('Enter the function to be evaluated:');
sum=f(x0)+f(xn)+4*f(xn-h);
for count=1:2:n-3;
sum=sum+4*f(x0+count*h)+2*f(x0+(count+1)*h);
A=h*sum/3;
end
fprintf('integration is %d n ',A);
end
Output:-
Simpsons 3/8 Rule
function simpsonsrule
x0=input('Enter the value of initial input: ');
xn=input ('Enter the value of final input: ');
n=input ('Enter the total no of intervals : ');
h=(xn-x0)/n
f=input('Enter the function to be evaluated:');
sum=f(x0)+f(xn);
for count=1:n-1;
if (mod(count,3)==0)
sum=sum+2*f(x0+count*h);
else
sum=sum+3*f(x0+count*h);
end
end
A=sum*3*h/8;
fprintf('integration is %d n ',A);
end
Output:-
Booles Rule
function boolesrule
x0=input('Enter the value of initial input: ');
xn=input ('Enter the value of final input: ');
n=input ('Enter the total no of intervals : ');
h=(xn-x0)/n
f=input('Enter the function to be evaluated:');
sum=7*(f(x0)+f(xn))+32*f(xn-h);
for count=1:2:n-3;
sum=sum+32*f(x0+count*h)+12*f(x0+(count+1)*h);
A=2*h*sum/45;
end
fprintf('integration is %d n ',A);
end
Output:-
Weddles Rule
function weddlesrule
x0=input('Enter the value of initial input: ');
xn=input ('Enter the value of final input: ');
n=input ('Enter the total no of intervals : ');
h=(xn-x0)/n
f=input('Enter the function to be evaluated:');
sum=0;
sum1=0;
sum2=0;
for count=0:2:n;
sum=sum +f(count*h);
end
for count=1:4:n-1;
sum1=sum1+5*f(count*h);
end
for count=3:4:n-1;
sum2=sum2+6*f(count*h);
end
integrationis=3*h*(sum+sum1+sum2)/10
end
Output:-
Rectangular Rule
function rectangularrule
x0=input('Enter the value of initial input: ');
xn=input ('Enter the value of final input: ');
n=input ('Enter the total no of intervals : ');
h=(xn-x0)/n
f=input('Enter the function to be evaluated:');
sum=0;
for i=x0:h:xn
sum=f(i);
end
A=h*sum
end
Output:-
Newton cotes method

Newton cotes method

  • 1.
    University of Engineering& Technology LHR (FAISALABAD CAMPUS) by FAISAL SAEED Trapezoidal Rule function trapezoidalrule x0=input('Enter the value of initial input: '); xn=input ('Enter the value of final input: '); n=input ('Enter the total no of intervals : '); h=(xn-x0)/n f=input('Enter the function to be evaluated:'); sum=0; for i=x0:h:xn if(i==x0 || i==xn) continue; end sum=sum +f(i); end sum=sum*2; trapezoidal=h/2*(f(x0)+f(xn)+sum) end
  • 2.
    Output:- Simpsons 1/3 Rule functionsimpsonsrule x0=input('Enter the value of initial input: '); xn=input ('Enter the value of final input: '); n=input ('Enter the total no of intervals : '); h=(xn-x0)/n f=input('Enter the function to be evaluated:'); sum=f(x0)+f(xn)+4*f(xn-h); for count=1:2:n-3; sum=sum+4*f(x0+count*h)+2*f(x0+(count+1)*h); A=h*sum/3; end fprintf('integration is %d n ',A); end
  • 3.
  • 4.
    Simpsons 3/8 Rule functionsimpsonsrule x0=input('Enter the value of initial input: '); xn=input ('Enter the value of final input: '); n=input ('Enter the total no of intervals : '); h=(xn-x0)/n f=input('Enter the function to be evaluated:'); sum=f(x0)+f(xn); for count=1:n-1; if (mod(count,3)==0) sum=sum+2*f(x0+count*h); else sum=sum+3*f(x0+count*h); end end A=sum*3*h/8; fprintf('integration is %d n ',A); end Output:-
  • 5.
    Booles Rule function boolesrule x0=input('Enterthe value of initial input: '); xn=input ('Enter the value of final input: '); n=input ('Enter the total no of intervals : '); h=(xn-x0)/n f=input('Enter the function to be evaluated:'); sum=7*(f(x0)+f(xn))+32*f(xn-h); for count=1:2:n-3; sum=sum+32*f(x0+count*h)+12*f(x0+(count+1)*h); A=2*h*sum/45; end fprintf('integration is %d n ',A); end Output:-
  • 6.
    Weddles Rule function weddlesrule x0=input('Enterthe value of initial input: '); xn=input ('Enter the value of final input: '); n=input ('Enter the total no of intervals : '); h=(xn-x0)/n f=input('Enter the function to be evaluated:'); sum=0; sum1=0; sum2=0; for count=0:2:n; sum=sum +f(count*h); end for count=1:4:n-1; sum1=sum1+5*f(count*h); end for count=3:4:n-1; sum2=sum2+6*f(count*h); end integrationis=3*h*(sum+sum1+sum2)/10 end
  • 7.
    Output:- Rectangular Rule function rectangularrule x0=input('Enterthe value of initial input: '); xn=input ('Enter the value of final input: '); n=input ('Enter the total no of intervals : '); h=(xn-x0)/n f=input('Enter the function to be evaluated:'); sum=0; for i=x0:h:xn sum=f(i); end A=h*sum end Output:-