In: Advanced Math
1. Approximate the integral,
exp(x), from 0 to 1,
using the composite midpoint rule, composite trapezoid rule, and
composite Simpson’s method. Each method
should involve exactly n =( 2^k) + 1 integrand evaluations, k = 1 :
20. On the same plot, graph the absolute error
as a function of n.
%Matlab code for finding integration using different
method
clear all
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function for which integration have to do
f1=@(x) exp(x);
fprintf('\tfunction for which integration have to do\n')
disp(f1)
%upper and lower limit
a=0; b=1;
fprintf('\tupper and lower limit of integration a=%f,
b=%f.\n',a,b)
%exact integration value for this limit
ext_int=f1(1)-f1(0);
fprintf('\texact integration value for this limit is
%f.\n',ext_int)
%all intervals
k=1:20;
%loop fpr all k
for i=1:length(k)
n(i)=2^i;
%finding integration using different
method
val_simp=Simpson_int(f1,a,b,n(i));
val_trap=Trapizoidal_int(f1,a,b,n(i));
val_mid=Midpoint_int(f1,a,b,n(i));
%error in all method
err_sim(i)=abs(ext_int-val_simp);
err_tra(i)=abs(ext_int-val_trap);
err_mid(i)=abs(ext_int-val_mid);
end
%loglog plot of error vs n
figure(1)
loglog(n,err_sim)
hold on
loglog(n,err_tra)
loglog(n,err_mid)
title('Loglog plot of n vs. error for y=exp(x)')
xlabel('Interval n')
ylabel('Error')
legend('Simpson','Trapizoidal','Mid point')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Simpson Method
function val=Simpson_int(f,a,b,n)
%f=function for which integration have to do
%a=upper limit of integration
%b=lower limit of integration
%n=number of subintervals
dx=(b-a)/n; %interval
length
zs=f(a)+f(b); %simpson
integration
%all x values for given subinterval
xx=a:dx:b;
%Simpson Algorithm for n equally spaced
interval
for i=2:n
if mod(i,2)==0
zs=zs+4*f(xx(i));
else
zs=zs+2*f(xx(i));
end
end
%result using Simpson rule
val=double((dx/3)*zs);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Trapizoidal Method
function val=Trapizoidal_int(f,a,b,n)
% f is the function for integration
% a is the lower limit of integration
% b is the upper limit of integration
% n is the number of trapizoidal interval in
[a,b]
dx=(b-a)/n; %x interval
val=0;
%splits interval a to b into n+1
subintervals
xx=linspace(a,b,n+1);
%loop for trapizoidal integration
for i=2:n
val=val+2*double(f(xx(i)));
end
%Final integration value
val for limit a to b
val=(dx/2)*(val+f(a)+f(b));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for midpoint Method
function val=Midpoint_int(f,a,b,n)
% f is the function for integration
% a is the lower limit of integration
% b is the upper limit of integration
% n is the number of trapizoidal interval in
[a,b]
dx=(b-a)/n; %x interval
val=0;
%splits interval a to b into n+1
subintervals
x=linspace(a,b,n+1);
%loop for trapizoidal integration
for i=1:n
x_mid(i)=(x(i+1)+x(i))/2;
val=val+double(f(x_mid(i)));
end
%result using midpoint integration method
val=dx*val;
end
%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%