Você está na página 1de 6

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;
if((choice==2 && mod(interval,2)~=0))
disp('simpson 1by3 cannot be executed')
return
elseif(choice==3)
choice=[1 2];%modifying value to reate effective programming
if(mod(interval,2)~=0)
disp('simp 1by 3 not possible')
flag=1;%to evaluate specific possible integrations only
end
end
%if all the two integrations possible flag will be at zero 0
%Performing Integration
trapres=0;
simp1by3res=0;
for i=1:length(choice)
sum0=0;sum1=0;
switch(choice(i))
case {1}
sum0=sum(y(2:(xlen-1)));
trapres=(h/2)*(y(1)+y(xlen)+(2*sum0));
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));
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

(b) Simpsons (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');
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;
if((choice==2 && mod(interval,2)~=0))
disp('simpson 1by3 cannot be executed')
return
elseif(choice==3)
choice=[1 2];%modifying value to reate effective programming
if(mod(interval,2)~=0)
disp('simp 1by 3 not possible')
flag=1;%to evaluate specific possible integrations only
end
end
%if all the two integrations possible flag will be at zero 0
%Performing Integration
trapres=0;
simp1by3res=0;
for i=1:length(choice)
sum0=0;sum1=0;
switch(choice(i))
case {1}
sum0=sum(y(2:(xlen-1)));
trapres=(h/2)*(y(1)+y(xlen)+(2*sum0));
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));
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

(c) Combination of Trapezoid and Simpsons 3/8th 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;
if((choice==2 && mod(interval,2)~=0))
disp('simpson 1by3 cannot be executed')
return
elseif(choice==3)
choice=[1 2];%modifying value to reate effective programming
if(mod(interval,2)~=0)
disp('simp 1by 3 not possible')
flag=1;%to evaluate specific possible integrations only
end
end
%if all the two integrations possible flag will be at zero 0
%Performing Integration
trapres=0;
simp1by3res=0;
for i=1:length(choice)
sum0=0;sum1=0;
switch(choice(i))
case {1}
sum0=sum(y(2:(xlen-1)));
trapres=(h/2)*(y(1)+y(xlen)+(2*sum0));
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));
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

Você também pode gostar