You are on page 1of 6

Experiment 7

Name: - Ajitsingh Jagtap Reg. No.: - 15BCH0075

Slot: - L5+L6

MATLAB code for
(a) Trapezoidal (multi-segment code),method

Input: -
%NUMERICAL INTEGRATION FOR CONSTANT STEP SIZE
clear all
close all
clc
syms x y
x=input('Enter the x values as an array');
y=input('Enter the y values as an array');
h=input('Enter the step size');
xlen=length(x);
ylen=length(y);
interval=xlen-1;
%conditions for data consistency
if(xlen~=ylen)
disp('The data entered are inconsistent')
return
end
for i=1:xlen-1
if ((abs(x(i+1)-x(i)))~=h)
disp('step size is not uniform')
return
end
end
if(interval<1)
disp('Single data entered integration not possible')
return
end
%menu driven program
disp('enter 1 to perform trapezoidal integration')
disp('enter 2 to perform simpson 1by 3 integration')
disp('enter 3 to perform trapezoidal and simpson 1by3 integration')
choice=input('please enter your choice');
flag=0;

disp(['The simpson 1 by 3 numerical integration value is' num2str(simp1by3res)]) end otherwise disp('not a valid choice') return end end OUTPUT: - Enter the x values as an array [0:10:50] Enter the y values as an array [10 35 55 52 37 34] Enter the step size 10 enter 1 to perform trapezoidal integration enter 2 to perform simpson 1by 3 integration enter 3 to perform trapezoidal and simpson 1by3 integration please enter your choice 1 The trapezoidal numerical integration value is 2010 .sum1=0. sum1=sum(y(3:2:(xlen-1))).2)~=0) disp('simp 1by 3 not possible') flag=1. for i=1:length(choice) sum0=0.%modifying value to reate effective programming if(mod(interval. simp1by3res=0.if((choice==2 && mod(interval. simp1by3res=(h/3)*(y(1)+y(xlen)+(4*sum0)+(2*sum1)).%to evaluate specific possible integrations only end end %if all the two integrations possible flag will be at zero 0 %Performing Integration trapres=0. switch(choice(i)) case {1} sum0=sum(y(2:(xlen-1))). disp(['The trapezoidal numerical integration value is' num2str(trapres)]) case {2} if(flag~=1)%in this condition only we should evaluate 1/3 rule sum0=sum(y(2:2:(xlen-1))). trapres=(h/2)*(y(1)+y(xlen)+(2*sum0)).2)~=0)) disp('simpson 1by3 cannot be executed') return elseif(choice==3) choice=[1 2].

if((choice==2 && mod(interval.2)~=0) disp('simp 1by 3 not possible') flag=1. (b) Simpson’s (multi-segment) 1/3rd method INPUT: - %NUMERICAL INTEGRATION FOR CONSTANT STEP SIZE clear all close all clc syms x y x=input('Enter the x values as an array'). %conditions for data consistency if(xlen~=ylen) disp('The data entered are inconsistent') return end for i=1:xlen-1 if ((abs(x(i+1)-x(i)))~=h) disp('step size is not uniform') return end end if(interval<1) disp('Single data entered integration not possible') return end %menu driven program disp('enter 1 to perform trapezoidal integration') disp('enter 2 to perform simpson 1by 3 integration') disp('enter 3 to perform trapezoidal and simpson 1by3 integration') choice=input('please enter your choice'). interval=xlen-1. switch(choice(i)) . for i=1:length(choice) sum0=0.sum1=0. ylen=length(y). simp1by3res=0. xlen=length(x). y=input('Enter the y values as an array'). h=input('Enter the step size').%modifying value to reate effective programming if(mod(interval.%to evaluate specific possible integrations only end end %if all the two integrations possible flag will be at zero 0 %Performing Integration trapres=0. flag=0.2)~=0)) disp('simpson 1by3 cannot be executed') return elseif(choice==3) choice=[1 2].

case {1} sum0=sum(y(2:(xlen-1))). sum1=sum(y(3:2:(xlen-1))). disp(['The simpson 1 by 3 numerical integration value is' num2str(simp1by3res)]) end otherwise disp('not a valid choice') return end end OUTPUT: - Enter the x values as an array [0:10:50] Enter the y values as an array [10 35 55 52 37 34] Enter the step size 10 enter 1 to perform trapezoidal integration enter 2 to perform simpson 1by 3 integration enter 3 to perform trapezoidal and simpson 1by3 integration please enter your choice 2 simpson 1by3 cannot be executed . trapres=(h/2)*(y(1)+y(xlen)+(2*sum0)). simp1by3res=(h/3)*(y(1)+y(xlen)+(4*sum0)+(2*sum1)). disp(['The trapezoidal numerical integration value is' num2str(trapres)]) case {2} if(flag~=1)%in this condition only we should evaluate 1/3 rule sum0=sum(y(2:2:(xlen-1))).

interval=xlen-1. (c) Combination of Trapezoid and Simpson’s 3/8th method. y=input('Enter the y values as an array'). flag=0. xlen=length(x). if((choice==2 && mod(interval. ylen=length(y).2)~=0)) disp('simpson 1by3 cannot be executed') return elseif(choice==3) choice=[1 2]. %conditions for data consistency if(xlen~=ylen) disp('The data entered are inconsistent') return end for i=1:xlen-1 if ((abs(x(i+1)-x(i)))~=h) disp('step size is not uniform') return end end if(interval<1) disp('Single data entered integration not possible') return end %menu driven program disp('enter 1 to perform trapezoidal integration') disp('enter 2 to perform simpson 1by 3 integration') disp('enter 3 to perform trapezoidal and simpson 1by3 integration') choice=input('please enter your choice'). switch(choice(i)) .%to evaluate specific possible integrations only end end %if all the two integrations possible flag will be at zero 0 %Performing Integration trapres=0. INPUT: - %NUMERICAL INTEGRATION FOR CONSTANT STEP SIZE clear all close all clc syms x y x=input('Enter the x values as an array'). simp1by3res=0.2)~=0) disp('simp 1by 3 not possible') flag=1. for i=1:length(choice) sum0=0.sum1=0.%modifying value to reate effective programming if(mod(interval. h=input('Enter the step size').

disp(['The simpson 1 by 3 numerical integration value is' num2str(simp1by3res)]) end otherwise disp('not a valid choice') return end end OUTPUT: - Enter the x values as an array [0:10:50] Enter the y values as an array [10 35 55 52 37 34] Enter the step size 10 enter 1 to perform trapezoidal integration enter 2 to perform simpson 1by 3 integration enter 3 to perform trapezoidal and simpson 1by3 integration please enter your choice3 simp 1by 3 not possible The trapezoidal numerical integration value is 2010 Final Answer: - t2 M =Q ×∫ c dt t1 M =Q ×2010 But Q=4 m3 /min M =4 ×2010 M =8040 . trapres=(h/2)*(y(1)+y(xlen)+(2*sum0)). case {1} sum0=sum(y(2:(xlen-1))). disp(['The trapezoidal numerical integration value is' num2str(trapres)]) case {2} if(flag~=1)%in this condition only we should evaluate 1/3 rule sum0=sum(y(2:2:(xlen-1))). sum1=sum(y(3:2:(xlen-1))). simp1by3res=(h/3)*(y(1)+y(xlen)+(4*sum0)+(2*sum1)).