clc
clear all
disp('This program shows the convergence of getting the value of ')
disp('an improper integral using multiple segment Trapezoidal rule')
disp('Author: Autar K Kaw. autarkaw.wordpress.com')
b=9;
m=14;
fprintf('\nFinding the integral of 1/sqrt(x) with limits of integration as x=0 to x=%g',b)
syms x
f=1/sqrt(x);
a=0;
valexact=double(int(f,x,a,b));
fprintf('\n\nExact value of integral = %f',valexact)
disp( ' ')
f=inline('1/sqrt(x)');
for k=4:1:m
n=2^k;
h=(b-a)/n;
sum=0;
for i=1:1:n-1
sum=sum+f(a+i*h);
end
sum=2*sum+0+f(b);
sum=(b-a)/(2*n)*sum;
fprintf('\nApproximate value of integral =%f with %g segments',sum,n)
end
disp(' ')
This program shows the convergence of getting the value of
an improper integral using multiple segment Trapezoidal rule
Author: Autar K Kaw. autarkaw.wordpress.com
Finding the integral of 1/sqrt(x) with limits of integration as x=0 to x=9
Exact value of integral = 6.000000
Approximate value of integral =4.904246 with 16 segments
Approximate value of integral =5.225408 with 32 segments
Approximate value of integral =5.452337 with 64 segments
Approximate value of integral =5.612757 with 128 segments
Approximate value of integral =5.726182 with 256 segments
Approximate value of integral =5.806382 with 512 segments
Approximate value of integral =5.863092 with 1024 segments
Approximate value of integral =5.903191 with 2048 segments
Approximate value of integral =5.931546 with 4096 segments
Approximate value of integral =5.951596 with 8192 segments
Approximate value of integral =5.965773 with 16384 segments