You are on page 1of 26

% Assignment-1

% Find the area under the graph of f(x) = 2 - x^2 over the interval [0,2]
% using Riemann integral

a = 0;
b = 2;
n = 10000; % Number of subrectangles
h = (b-a)/n;
s = 0;
fvalue = @(x) 2 - x^2;

for i=(a+h):h:b
area = fvalue(i)*h;
s = s +area;
end
fprintf('Area under the given function is: %f square unit(approximately).
\n',s);

% Finding minimum value of f(x,y) = x^2 + y^2 + 4*x - 6*y + 18 in the


% window [0,2]x[2,4] with increment 0.01 for x and y

f2value = @(x,y) x^2 + y^2 + 4*x - 6*y + 18;


initial = f2value(0,2);
for i = 0:0.01:2
for j = 2:0.01:4
v = f2value(i,j);
if (v <= initial)
initial = v;
end
end
end
fprintf('Minimum value of the given function is: %f \n',initial)

%Find the absolute extrema of f(x,y) = x^2 - 2*x*y + 4*y^2 - 4*x - 2*y +24
%for 0<=x<=4 and 0<=y<=2
syms x y
f = x^2 - 2*x*y + 4*y^2 - 4*x - 2*y + 24; % equatin (1)
fx = diff(f,x);
fy = diff(f,y);
s = solve([fx==0,fy==0],[x,y]);
x1 = s.x;
y1 = s.y;

% line segment between (0,0) and (4,0) we have y = 0, equation (1) becomes
f1 = subs(f,y,0); % 0<=x<=4
f1x = diff(f1,x);
x2 = solve(f1x==0,x);
y2 = 0;

1
% line segment between (0,0) and (0,2) we have x = 0, equation (1) becomes
f2 = subs(f,x,0); % 0<=y<=2
f2y = diff(f2,x);
y3 = solve(f2y==0,y);
x3 = 0;

% line segment between (0,2) and (4,2) we have y = 2, equation (1) becomes
f3 = subs(f,y,2); % 0<=x<=4
f3x = diff(f3,x);
x4 = solve(f3x==0,x);
y4 = 2;

% line segment between (4,2) and (4,0) we have x = 4, equation (1) becomes
f4 = subs(f,x,4); % 2<=y<=0
f4y = diff(f4,y);
y5 = solve(f4y==0,y);
x5 = 4;

% Boundary points
bx1 = 0;
by1 = 0;
bx2 = 0;
by2 = 2;
bx3 = 4;
by3 = 2;
bx4 = 4;
by4 = 0;

xco = [x1,x2,x3,x4,x5,bx1,bx2,bx3,bx4];
yco = [y1,y2,y3,y4,y5,by1,by2,by3,by4];

init_max = subs(f,{x,y},{xco(1),yco(1)});
init_min = subs(f,{x,y},{xco(1),yco(1)});

for i = 1:length(xco)
z = subs(f,{x,y},{xco(i),yco(i)});
if z > init_max
init_max = z;
end
if z < init_min
init_min = z;
end
end
fprintf('Absolute extrema of the given finction are:max = %.4f \tmin = %.4f
\n',init_max,init_min)

syms x y
f_ = x*y - x^3 -y^2;
f_x = diff(f_,x);
f_y = diff(f_,y);
f_xx = diff(f_x,x);
f_yy = diff(f_y,y);
f_xy = diff(f_x,y);

2
D = f_xx*f_yy -(f_xy)^2;

[p1,p2]= solve([f_x==0,f_y==0],[x,y]);
sol = [p1,p2];
fvalue1 = subs(f_,{x,y},{sol(1,1),sol(1,2)});
fvalue2 = subs(f_,{x,y},{sol(2,1),sol(2,2)});
f_xxv1 = subs(f_xx,{x,y},{sol(1,1),sol(1,2)});
f_xxv2 = subs(f_xx,{x,y},{sol(2,1),sol(2,2)});
d1 = subs(D,{x,y},{sol(1,1),sol(1,2)});
d2 = subs(D,{x,y},{sol(2,1),sol(2,2)});

if d1>0
if f_xxv1>0
con1 = 'Local minimum ';
elseif f_xxv1<0
con1 = 'Local maximum ';
else
con1 = 'Inconclusive ';
end
elseif d1<0
con1 = 'Inflection point';
else
con1 = 'Inconclusive ';
end

if d2>0
if f_xxv2>0
con2 = 'Local minimum ';
elseif f_xxv2<0
con2 = 'Local maximum ';
else
con2 = 'Inconclusive ';
end
elseif d2<0
con2 = 'Inflection point';
else
con2 = 'Inconclusive ';
end

critical_points = (sol);
fval = [fvalue1;fvalue2];
f_xxvalue = [f_xxv1;f_xxv2];
d = [d1;d2];
con = [con1;con2];

table(critical_points,fval,f_xxvalue,d,con)

%Find the volume of the solid lies above the square R = [0,2] X [0,2], and
%below the elliptic paraboloid z = 16 - x^2 - 2*y^2. Divide R into four
%equal squares and choose the sample point to be the upper right corner
%point of each squares.

ns = 2; % number of square is nsXns

3
fun = @(x,y) 16 - x^2 - 2*y^2;
h1 = (2-0)/ns;
h2 = (2-0)/ns;
vol = 0;
for i = (0+h1):h1:2
for j = (0+h2):h2:2
temp = fun(i,j)*h1*h2;
vol = vol + temp;
end
end
fprintf('Volume of the solid is:%.4f cubic unit(approximately).',vol)

Area under the given function is: 1.332933 square unit(approximately).


Minimum value of the given function is: 9.000000
Absolute extrema of the given finction are:max = 36.0000 min = 17.0000

ans =

2×5 table

critical_points fval f_xxvalue d con


_______________ _____ _________ __ ________________

0 0 0 0 -1 Inflection point
1/6 1/12 1/432 -1 1 Local maximum

Volume of the solid is:34.0000 cubic unit(approximately).

4
Published with MATLAB® R2021b

5
% Assignment-2

syms x
f(x) = x^2*exp(x)-5*x^3;
integral = int(f(x)); % Integral of f(x)
f_x = diff(f(x),x); % First derivative
f_xx = diff(f_x,x); % Second derivative

syms y z
f(x,y,z) = x^2*exp(y) - 5*z^2;
pari = int(f(x,y,z),x); % Partial integral with respect to x
f_z = diff(f(x,y,z),z); % First partial derivative with respect to z
f_zz = diff(f_z,z); % Second partial derivative with respect ot z

syms y(x) x
equn = diff(y,x)==x*y;
cond = y(0)==1;
ysol(x) = dsolve(equn,cond);

fplot(ysol(x),'k--')
xlabel('x')
ylabel('y(x)')
grid on
hold on

tspan = [-6 6];


y0 = 1;
[x,y] = ode23(@(x,y) x*y,tspan,y0);
plot(x,y,'ro')

[x1,y1] = ode45(@(x,y) x*y,tspan,y0);


plot(x1,y1,'g-')
hold off

syms q(t) t r a
eqn1 = diff(q,t)==41*r - (q*r)/100;
con1 = q(0)==a;
sol0 = dsolve(eqn1,con1);
assume(r>0)
ql = subs(sol0,t,(1/0));
q0 = 2*ql;
qsol(t) = subs(sol0,a,q0);
% This problem needs modification otherwise can't solvable

syms x(t) t k c
eqn2 = diff(x,t)==-k*x;
con2 = x(0)==c;% at initial time radioactive nuclei was c
sol11 = dsolve(eqn2,con2);
const = solve(subs(sol11,t,1500)==(c/2));
sol12 = subs(sol11,k,const);
percentage = subs(sol12,t,4500);

1
time = solve(sol12==(c/10));
fprintf('%s percent of the Radio active nuclei will remailn after 4500 years.
\n',percentage)
fprintf('%f years will remain 10 percent of the original remain.\n',time)

c/8 percent of the Radio active nuclei will remailn after 4500 years.
4982.892142 years will remain 10 percent of the original remain.

Published with MATLAB® R2021b

2
% inner product function

function value = inner(x,y)


n = length(x);
s = 0;
for i = 1:1:n
temp = x(i)*y(i);
s = s + temp;
end
value = s;

end

Not enough input arguments.

Error in inner (line 4)


n = length(x);

Published with MATLAB® R2021b

1
% create a function m file that will take two vectors of same dimension and
% calculate their Euclidean inner product.

% a
u = [3 2];
v = [4 5];
w = [-1,6];

% a function inner.m is defined outside


t = inner(u,v);
fprintf(' Euclidean inner product <u,v> = u.v = %d\n',t)

i = inner((u+v),w);
i1 = inner(u,w);
i2 = inner(v,w);
i3 = i1 + i2;
fprintf(' <u+v,w> = %d\n <u,w> + <v,w> = %d\n Hence, <u+v,w> = <u,w> + <v,w>.
\n',i,i3)

%b
u1 = [1 0 -1];
u2 = [2 0 2];
u3 = [0 5 0];
j1 = inner(u1,u2);
j2 = inner(u2,u3);
j3 = inner(u3,u1);
fprintf(' Since <u1,u2> = %d\n <u2,u3>=%d\n <u3,u1>=%d\n Therefore,{u1,u2,u3}
are orthogonal sets.\n',j1,j2,j3)

v1 = sym(u1/sym(norm(u1)));
v2 = sym(u2/sym(norm(u2)));
v3 = sym(u3/sym(norm(u3)));
nv1 = norm(v1);
nv2 = norm(v2);
nv3 = norm(v3);
fprintf(' Since ||v1|| = ||v2|| = ||v3|| = 1\n Hence,{v1,v2,v3} is an
orthonormal set.\n\n')
fprintf(' Where v1, v2 and v3 are respectively:\n')
fprintf(' (%s,%s,%s)\n\n',v1,v2,v3)

%c
disp('Gram-schmidt orthogonalization process:(vectors considers as row
vectors)')
v11 = [1 1 1];
v12 = [0 1 1];
v13 = [0 0 1];
vset = [v11;v12;v13];
uset= zeros(size(vset));
uset(1,:) = vset(1,:);

for k = 2:1:length(v11)

1
s = zeros(size(v11));
for j = 1:1:(k-1)
temp = (inner(vset(k,:),uset(j,:))/
inner(uset(j,:),uset(j,:)))*uset(j,:);
s = s + temp;
end
temp1 = vset(k,:) - s;
uset(k,:) = temp1;

end

fprintf('Orthogonal vectors set {u1,u2,u3} respectively are:\n')


for i = 1:1:length(v11)
v21 = uset(i,:);
disp(v21)
end

fprintf('\nOrthonormal vectors set {e1,e2,e3} respectively are:\n')


for i = 1:1:length(v11)
v22 = uset(i,:)/norm(uset(i,:));
disp(v22)
end
% T(u,v) = (x(u,v),y(u,y)) where x = (u+4)/4 and y = (u-v)/2
% Find T(2,1) and Find the coordinate point of image under T of the square
% region of uv plane bounded by the lines u = -2, u = 2 and v = -2 , v = 2
% Write f(x,y) = 2*x^2 + 6*x*y - 5*y^2 as Q(x) = x'Ax where A is a square
% symmetric matrix hence evaluate Q(2,1) and verify.

x = @(u,v) (u+v)/4;
y = @(u,v) (u-v)/2;
t = @(u,v) [x(u,v),y(u,v)];
t1 = t(1,3);
disp(' T(1,3) = ')
disp(t1)

u111 = -2;
u112 = 2;
v111 = -2;
v112 = 2;
c1 = t(-2,-2);
c2 = t(2,-2);
c3 = t(2,2);
c4 = t(-2,2);

f = @(x,y) (2*x^2 + 6*x*y - 5*y^2);


fvalue = f(2,1);

a = [2 3;3 -5];

syms x y
X = [x;y];
q = (transpose(X)*a*X);

2
qvalue = subs(q,{x,y},{2,1});
fprintf(' f(2,1) = %d \n q(2,1) = %d \n',fvalue,qvalue)
if (fvalue==qvalue)
disp(' Since f(2,1) and q(2,1) are equal hence verified.')
end

Euclidean inner product <u,v> = u.v = 22


<u+v,w> = 35
<u,w> + <v,w> = 35
Hence, <u+v,w> = <u,w> + <v,w>.
Since <u1,u2> = 0
<u2,u3>=0
<u3,u1>=0
Therefore,{u1,u2,u3} are orthogonal sets.
Since ||v1|| = ||v2|| = ||v3|| = 1
Hence,{v1,v2,v3} is an orthonormal set.

Where v1, v2 and v3 are respectively:


(2^(1/2)/2,0,-2^(1/2)/2)

(2^(1/2)/2,0,2^(1/2)/2)

(0,1,0)

Gram-schmidt orthogonalization process:(vectors considers as row vectors)


Orthogonal vectors set {u1,u2,u3} respectively are:
1 1 1

-0.666666666666667 0.333333333333333 0.333333333333333

0 -0.500000000000000 0.500000000000000

Orthonormal vectors set {e1,e2,e3} respectively are:


0.577350269189626 0.577350269189626 0.577350269189626

-0.816496580927726 0.408248290463863 0.408248290463863

0 -0.707106781186547 0.707106781186547

T(1,3) =
1 -1

f(2,1) = 15
q(2,1) = 15
Since f(2,1) and q(2,1) are equal hence verified.

3
Published with MATLAB® R2021b

4
% Bisection method f(x)=cos(x)-x*exp(x)
% Display format n x) |xn+1-xn|

f = @(x) (cos(x)-x*exp(x));
a = 1.0;
b = 0.5;
i = 1;
tol = 0.000001;

fprintf('Using Bisection method:\n')


if(f(a)==0)
fprintf('Required root is:%f',a)
elseif(f(b)==0)
fprintf('Required root is:%f',b)
elseif(f(a)*f(b)>0)
disp('Root is not bracketed.')
else
fprintf(' n \t xn\t\t|x_n+1-x_n|\n')
while (abs(b-a)>tol)
c = (a + b)/2;
if (f(c)*f(a)<0)
b = c;
else
a = c;
end
xn = c;
d = abs(b-a);
fprintf(' %d \t %f \t %f \n',i,xn,d)
i = i + 1;
end
fprintf('\nroot:%.6f(approximately)\n',c)
end

clear all
% Fixed point iteration method
g = @(x) (cos(x)*exp(-x));
x0 = 1;% Initial guess
tol = 0.000001;
x1 = g(x0);
d1 = abs(x1-x0);
j=1;

fprintf('\nUsing Fixed point iteration method:\n')


fprintf(' n \t xn\t\t|x_n+1-x_n|\n')
while (d1>tol)
x1 = g(x0);
d1 = abs(x1 - x0);
fprintf(' %d \t%f \t %f\n',j,x0,d1)
x0 = x1;
j = j + 1;
end

1
fprintf('\nroot:%.6f(approximately)\n ',x1)

clear all
% Newton-Raphson method
fprintf('\nUsing Newton-Raphson method:\n')
syms x
f = cos(x)-x*exp(x);
fp = diff(f,x);
tol = 0.000001;
x0 = 1;
initial = abs(subs(f,x,x0));
n =1;

while (tol < initial)


x1 = x0 - (subs(f,x,x0)/subs(fp,x,x0));
initial = abs(subs(f,x,x1));
fprintf(' %d\t %f\t%f \n',n,x1,initial)
x0 = x1;
n = n + 1;
end
fprintf('\nroot: %.6f(approximately)\n\n',x1)

clear all
% False position method
fprintf('\n Using False position method:\n')
f = @(x) (cos(x)-x*exp(x));
a = 0;
b = 1;
i = 1;
tol = 0.000001;

fprintf(' n \t xn\t\t|x_n+1-x_n|\n')
while (abs(b-a)>tol)
c = (a*f(b) - b*f(a))/(f(b)-f(a));
if (f(c)*f(a)<0)
b = c;
else
a = c;
end
xn = c;
d = abs(b-a);
fprintf(' %d \t %f \t %f \n',i,xn,d)
i = i + 1;
end
fprintf('\nroot:%.6f(approximately)\n',c)

Using Bisection method:


n xn |x_n+1-x_n|
1 0.750000 0.250000
2 0.625000 0.125000
3 0.562500 0.062500
4 0.531250 0.031250
5 0.515625 0.015625
6 0.523438 0.007812

2
7 0.519531 0.003906
8 0.517578 0.001953
9 0.518555 0.000977
10 0.518066 0.000488
11 0.517822 0.000244
12 0.517700 0.000122
13 0.517761 0.000061
14 0.517731 0.000031
15 0.517746 0.000015
16 0.517754 0.000008
17 0.517757 0.000004
18 0.517756 0.000002
19 0.517756 0.000001

root:0.517756(approximately)

Using Fixed point iteration method:


n xn |x_n+1-x_n|
1 1.000000 0.801234
2 0.198766 0.604836
3 0.803602 0.492835
4 0.310766 0.387013
5 0.697780 0.316415
6 0.381365 0.252501
7 0.633866 0.206389
8 0.427477 0.165991
9 0.593468 0.135518
10 0.457950 0.109449
11 0.567399 0.089248
12 0.478151 0.072251
13 0.550402 0.058857
14 0.491545 0.047716
15 0.539261 0.038841
16 0.500420 0.031516
17 0.531936 0.025641
18 0.506295 0.020816
19 0.527111 0.016930
20 0.510182 0.013749
21 0.523931 0.011179
22 0.512752 0.009081
23 0.521833 0.007382
24 0.514450 0.005998
25 0.520448 0.004875
26 0.515573 0.003961
27 0.519534 0.003220
28 0.516314 0.002616
29 0.518931 0.002126
30 0.516804 0.001728
31 0.518532 0.001404
32 0.517128 0.001141
33 0.518269 0.000927
34 0.517342 0.000754
35 0.518095 0.000612
36 0.517483 0.000498

3
37 0.517981 0.000404
38 0.517576 0.000329
39 0.517905 0.000267
40 0.517638 0.000217
41 0.517855 0.000176
42 0.517678 0.000143
43 0.517822 0.000117
44 0.517705 0.000095
45 0.517800 0.000077
46 0.517723 0.000063
47 0.517785 0.000051
48 0.517735 0.000041
49 0.517776 0.000034
50 0.517742 0.000027
51 0.517770 0.000022
52 0.517747 0.000018
53 0.517765 0.000015
54 0.517751 0.000012
55 0.517763 0.000010
56 0.517753 0.000008
57 0.517761 0.000006
58 0.517755 0.000005
59 0.517760 0.000004
60 0.517755 0.000003
61 0.517759 0.000003
62 0.517756 0.000002
63 0.517758 0.000002
64 0.517757 0.000001
65 0.517758 0.000001
66 0.517757 0.000001

root:0.517758(approximately)

Using Newton-Raphson method:


1 0.653079 0.460642
2 0.531343 0.041803
3 0.517910 0.000464
4 0.517757 0.000000

root: 0.517757(approximately)

Using False position method:


n xn |x_n+1-x_n|
1 0.314665 0.685335
2 0.446728 0.553272
3 0.494015 0.505985
4 0.509946 0.490054
5 0.515201 0.484799
6 0.516922 0.483078
7 0.517485 0.482515
8 0.517668 0.482332
9 0.517728 0.482272
10 0.517748 0.482252

4
11 0.517754 0.482246
12 0.517756 0.482244
13 0.517757 0.482243
14 0.517757 0.482243
15 0.517757 0.482243
16 0.517757 0.482243
17 0.517757 0.482243
18 0.517757 0.482243
19 0.517757 0.482243
20 0.517757 0.482243
21 0.517757 0.482243
22 0.517757 0.482243
23 0.517757 0.482243
24 0.517757 0.482243
25 0.517757 0.482243
26 0.517757 0.482243
27 0.517757 0.482243
28 0.517757 0.482243
29 0.517757 0.482243
30 0.517757 0.482243
31 0.517757 0.482243
32 0.517757 0.482243
33 0.517757 0.482243
34 0.517757 0.000000

root:0.517757(approximately)

Published with MATLAB® R2021b

5
% Newton's forward difference interpolation method
x = [140 150 160 170 180 190];
y = [3.685 4.854 6.302 8.076 10.225 11.055];

t1 = 142;
t2 = 156;
h = x(2)-x(1);
p1 = (t1-x(1))/h;
p2 = (t2-x(1))/h;

dy = zeros(length(y));% Forward difference table


for i = 1:1:length(y)
dy(i,1)=y(i);
end

for i = 1:1:length(y)-1
for j = 1:1:length(y)-i
dy(j,i+1) = dy(j+1,i)-dy(j,i);
end
end
% calculation for t1
sum1 = dy(1,1);
for i = 2:1:length(y)
mul1 = 1;
for j = 1:1:i-1
mul1 = mul1*(p1-j+1);
end
temp1 = mul1/factorial(i-1);
sum1 = sum1 + dy(1,i)*temp1;
end

% calculation for t2

sum2 = dy(1,1);
for i = 2:1:length(y)
mul2 = 1;
for j = 1:1:i-1
mul2 = mul2*(p2-j+1);
end
temp2 = mul2/factorial(i-1);
sum2 = sum2 + dy(1,i)*temp2;
end

fprintf(' Estimated presure at temperature %d is: %f \n',t1,sum1)


fprintf(' Estimated presure at temperature %d is: %f \n',t2,sum2)

Estimated presure at temperature 142 is: 3.854108


Estimated presure at temperature 156 is: 5.705119

Published with MATLAB® R2021b

1
% Newton's backward difference interpolation method
%x = [140 150 160 170 180 190];
%y = [3.685 4.854 6.302 8.076 10.225 11.055];
x = [1891 1901 1911 1921 1931];
y = [46 66 81 93 101];

t1 = 1925;
%t2 = 156;
h = x(2)-x(1);
p1 = (t1-x(length(x)))/h;
%p2 = (t2-x(length(x)))/h;

dy = zeros(length(y));% Backward difference table


for i = 1:1:length(y)
dy(i,1)=y(i);
end
% Backward difference table
for i = 1:1:length(y)-1
for j = length(y):-1:(i+1)
dy(j,i+1) = dy(j,i)-dy(j-1,i);
end
end

sum1 = dy(length(y),1);
for i = 2:1:length(y)
mul1 = 1;
for j = 1:1:i-1
mul1 = mul1*(p1+j-1);
end
temp1 = mul1/factorial(i-1);
sum1 = sum1 + dy(length(y),i)*temp1;
end
fprintf('\t\t\t\t\t -:Using Newtons backward difference formula:-\n\n')
fprintf(' Estimated presure at temperature %d is: %f \n',t1,sum1)

-:Using Newtons backward difference formula:-

Estimated presure at temperature 1925 is: 96.836800

1
Published with MATLAB® R2021b

2
% Newton's divided difference interpoaltion formula

x = [0.0 0.2 0.4 0.6 0.8];


y = [1.00000 1.22140 1.49182 1.82212 2.22554];
n = length(x);
t = 0.65;

dy = zeros(n,n+1);
for i = 1:1:n
dy(i,1)=x(i);
dy(i,2)=y(i);
end

for i = 3:1:n+1
for j= 1:1:(n+2-i)
dy(j,i)=(dy(j+1,i-1)-dy(j,i-1))/(dy(j+(i-2),1)-dy(j,1));
end
end

sum = dy(1,2);
for i=1:1:n-1
pro=1;
for j = 1:1:i
pro = pro*(t-x(j));
end
sum = sum + pro*dy(1,i+2);
end
fprintf('Approximate value of y at %f is %f:\n',t,sum)

Approximate value of y at 0.650000 is 1.915551:

1
Published with MATLAB® R2021b

2
% Lagranges interpolating formula

x = [0.0 0.2 0.4 0.6 0.8];


y = [1.00000 1.22140 1.49182 1.82212 2.22554];
n = length(x);
t = 0.65;

sum = 0;
for i = 1:1:n
p = 1;
for j = 1:1:n
if (i~=j)
p = (p*(t-x(j)))/(x(i)-x(j));
end
end
sum = sum + p*y(i);
end
fprintf('Approximate value of y at %f is %f',t,sum)

Approximate value of y at 0.650000 is 1.915551

Published with MATLAB® R2021b

1
% Numerical Integration
% Trapezoidal method
a = 0;
b = 2;
n = 100; % Number of subrectangles
h = (b-a)/n;
f = @(x) 2/(x^2+4);

int_value = 0;
for i = 1:1:n
int_value = int_value + ((h/2)*(f(a)+f(a+h)));
a = a + h;
end
fprintf('Integrated value using Trapezoidal method is: %f \n',int_value)

clear all
% Simpson's 1/3 rule
a = 0;
b = 2;
n = 10; % Number of subrectangles must be a multiple of 2
h = (b-a)/n;
f = @(x) 2/(x^2+4);

sum = 0;
if (mod(n,2)==0)
n1 = n/2;
for i = 1:1:n1
sum = sum + ((h/3)*(f(a)+4*f(a+h)+f(a+2*h)));
a = a+2*h;
end
else
fprintf("%d is not an even number.",n)
end
fprintf('Integrated value using Simpsons 1/3 rule is: %f \n',sum)

clear all
% Simpson's 3/8 rule
a = 0;
b = 2;
n = 9; % Number of subrectangles must be a multiple of 3
h = (b-a)/n;
f = @(x) 2/(x^2+4);

sum = 0;
if (mod(n,3)==0)
n1 = n/3;
for i = 1:1:n1
sum = sum + ((3*h/8)*(f(a)+3*f(a+h)+3*f(a+2*h)+f(a+3*h)));
a = a+3*h;

1
end
else
fprintf("%d is not a multiple of 3 \n",n)
end
fprintf('Integrated value using Simpsons 3/8 rule is: %f \n',sum)

clear all
% Weddle's rule
a = 0;
b = 2;
n = 60; % Number of subrectangles must be a multiple of 6
h = (b-a)/n;
f = @(x) 2/(x^2+4);

sum = 0;
if (mod(n,6)==0)
n1 = n/6;
for i = 1:1:n1
sum = sum + ((3*h/10)*(f(a)+5*f(a+h)+f(a+2*h)+6*f(a+3*h)+f(a
+4*h)+5*f(a+5*h)+f(a+6*h)));
a = a + 6*h;
end
end
fprintf("Integrated value using Weddle's rule is: %f \n",sum)

clear all
% Romberg Integration
a = 0;
b = 2;
f = @(x) 2/(x^2+4);
J = 3; % It approximate upto 2*J order accurate approximation
h = (b-a);

% Composite Trapeziodal rule


T = zeros(J,J);
for j = 1:1:J
a = 0;
b = 2;
h = (b-a)/(2^(j-1));
int_value = 0;
for i = 1:1:(2^(j-1))
int_value = int_value + ((h/2)*(f(a)+f(a+h)));
a = a + h;
end
T(j,1)=int_value;
end

% Richardson's Extrapolatin
for j=1:1:J
for k=2:1:j
T(j,k)=T(j,k-1)+((T(j,k-1)-T(j-1,k-1))/(4^(k-1)-1));
end
end

2
fprintf('Integrated value using Romberg integration is: %.8f',T(3,3))

Integrated value using Trapezoidal method is: 0.785394


Integrated value using Simpsons 1/3 rule is: 0.785398
Integrated value using Simpsons 3/8 rule is: 0.785398
Integrated value using Weddle's rule is: 0.785398
Integrated value using Romberg integration is: 0.78552941

Published with MATLAB® R2021b

You might also like