You are on page 1of 3

disp('----------------------THE INTEGRATION CALCULATOR----------------

-----------------------')
disp('This is a integration program.')
disp('It calculates the area under the curve based on the data you
have input.')
disp('This program calculates the area under the curve using 3 methods
which is;.')
disp('1) Built in Integration function int().')
disp('2) Built in trapezoidal Rule function trapz().')
disp('3) Using Numerical integration.')
disp('Follow the instructions carefully.')
disp('Programmed by Melvin Pereira.')
disp('Enjoy and have fun!!')
syms x;
a0=input('Please enter the lower limit: ');
b0=input('Please enter the upper limit: ');
while(a0>b0)
disp('Invalid input!!');
disp('Upper limit must be higher than lower limit')
a0=input('Please enter the lower limit: ');
b0=input('Please enter the upper limit: ');
end
n=input ('Please enter number of intervals: ');
while(n<2)
disp('Invalid input!!');
disp('Number of intervals must be greater or equal to 2');
n=input ('Please enter number of intervals: ');
end

a=a0;
b=b0;
y = ((2.39e-11)./((power(x,5)).*(exp(1.432./(3500.*x))-
1)));
a2 = double(int(y,a,b));
fprintf('The area under the curve using built in
integration function for the given y function with boundaries %g and
%g : %0.3f\n',a0,b0,a2)
clear x;
clear y;

a=a0;
b=b0;
xx=linspace(a,b,n+1);
y = ((2.39e-11)./((power(xx,5)).*(exp(1.432./(3500.*xx))-
1)));
z = trapz(xx,y);
fprintf('The area under the curve using built in
trapezoidal rule function for the given y function with boundaries %g
and %g : %0.3f\n',a0,b0,z)
clear x;
clear y;

a=a0;
b=b0;
y = @(x)((2.39e-
11)./((power(x,5)).*(exp(1.432./(3500.*x))-1)));
h = ((b-a)/n);
I=0;
I = I + y(a);
for ii = (a+h):h:(b-h)
I = I + 2*y(ii) ;
end

I = I + y(b);
I = I*(h/2);
fprintf('The area under the curve using the formula for
the given y function with boundaries %g and %g
: %0.3f\n',a0,b0,I)
clear x;
clear y;

error1=double((abs(z-I)/z)*100);
error2=double((abs(z-a2)/z)*100);
fprintf('The percentage error between using the formula
and built in integration funtion is :
%0.2f%%\n',error1)
fprintf('The percentage error between using the formula
and built in trapezoidal rule funtion is :
%0.2f%%\n',error2)

x2 =a:h:b;
y2 = ((2.39e-
11)./((power(x2,5)).*exp(1.432./(3500.*x2))));

for i=1:n
fill([x2(i) x2(i+1) x2(i+1) x2(i)],[0 0 y2(i+1)
y2(i)],'g' )
hold on
end
plot(x2,y2,'m--o')
xlabel('Wavelength (cm)')
ylabel('Energy (J)')
title('Graph of Energy against Wavelength')