Full MatLab Assignment Solution
Full MatLab Assignment Solution
% 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);
%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.
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)
ans =
2×5 table
0 0 0 0 -1 Inflection point
1/6 1/12 1/432 -1 1 Local maximum
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
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.
2
% inner product function
end
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];
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
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);
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
(2^(1/2)/2,0,2^(1/2)/2)
(0,1,0)
0 -0.500000000000000 0.500000000000000
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;
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;
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;
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)
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)
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)
root: 0.517757(approximately)
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)
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;
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
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;
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)
1
Published with MATLAB® R2021b
2
% Newton's divided difference interpoaltion formula
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)
1
Published with MATLAB® R2021b
2
% Lagranges interpolating formula
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)
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);
% 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))