MATLAB code Week 4
Integration: Indefinite, definite and Area between the curves
MATLAB Syntax used:
int(f,v) uses the symbolic object v as the
variable of integration, rather than the
variable determined by symvar
rsums(f, [a, b]) rsums(f, a, b) and rsums(f, [a,
b]) approximates the integral
for x from a to b.
fill(X,Y,C) fill(X,Y,C) creates filled polygons from
the data in X and Y with vertex color
specified by C.
char(X) converts array X of nonnegative integer
codes into a character array.
A. Integration
i) Inbuilt MATLAB function:
syms x
f=input('enter the function f(x):');
a=input('enter lower limit of x ');
b=input('enter the upper limit of x');
n=input('number of intervals');
z=int(f,a,b) % direct evaluation
ii) As a sum of rectangles by using rsums command :
àInitialization:
value = 0;
dx = (b-a)/n;
à sum of the function values at all the right points
for k=1:n
c = a+k*dx;
d=subs(f,x,c);
value = value + d;
end
à value of the sum* length of the sub interval is the approx. value of the integral
ivalue = dx*value
ezplot(f,[a b])
à Taking mid point function values
rsums(f, a, b)
Problems:
1) Sin(x) in [0, 2 pi]
Output
enter the function f(x):sin(x)
enter lower limit of x 0
enter the upper limit of x2*pi
number of intervals10
z =
value =
-1.5389e-016
z =
Figure Window:
2) Cos(x) in [-pi/2, pi/2]
Output
enter the function f(x):
cos(x)
enter lower limit of x
-pi/2
enter the upper limit of x
pi/2
number of intervals
10
z =
2
value =
(pi*((2^(1/2)*(5 - 5^(1/2))^(1/2))/2 + 5^(1/2) + (2^(1/2)*(5^(1/2) + 5)^(1/2))/2 +
1))/10
z =
Figure Window:
3) e^x+tan(x)
Output
enter the function f(x):
exp(x)+tan(x)
enter lower limit of x
0
enter the upper limit of x
pi/4
number of intervals
10
z =
exp(pi/4) + log(2)/2 - 1
value =
(pi*(exp(pi/4) + exp(pi/5) + exp(pi/8) + exp(pi/10) + exp(pi/20) + exp((3*pi)/20) +
exp(pi/40) + exp((3*pi)/40) + exp((7*pi)/40) + exp((9*pi)/40) + tan(pi/20) +
tan((3*pi)/20) + tan(pi/40) + tan((3*pi)/40) + tan((7*pi)/40) + tan((9*pi)/40) +
(5^(1/2)*(5 - 2*5^(1/2))^(1/2))/5 + 2^(1/2) + (5 - 2*5^(1/2))^(1/2)))/40
z =
exp(pi/4) + log(2)/2 - 1
B. Area between the curves:
clc
clear all
close all
syms x
y1=input('ENTER THE Y1 REGION VALUE');
y2=input('ENTER THE Y2 REGION VALUE');
t=solve(y1-y2); %(Y1-Y2=0)
po=double(t)
poi=sort(po)
n=length(poi)
m1=min(poi)
m2=max(poi)
ez1=ezplot(y1,[m1-1,m2+1])
hold on
TA=0
ez2=ezplot(y2,[m1-1,m2+1])
if n>2
for i=1:n-1
A=int(y1-y2,poi(i),poi(i+1))
TA= TA+abs(A)
x1 = linspace(poi(i),poi(i+1))
yy1 =subs(y1,x,x1)
yy2 = subs(y2,x,x1)
%iii) Creating a polygon:
xx = [x1,fliplr(x1)]
yy = [yy1,fliplr(yy2)]
fill(xx,yy,'g')
grid on
end
else
A=int(y1-y2,poi(1),poi(2))
TA=abs(A)
x1 = linspace(poi(1),poi(2));
yy1 =subs(y1,x,x1)
yy2 = subs(y2,x,x1)
xx = [x1,fliplr(x1)]
yy = [yy1,fliplr(yy2)]
fill(xx,yy,'g')
end
Problems:
4) Find the area of the regions enclosed by the curves, 𝑦 = 𝑥 ! − 2𝑥, 𝑦 = 𝑥
Output
ENTER THE Y1 REGION VALUE
x
ENTER THE Y2 REGION VALUE
x^2-2*x
f =
9/2
kokler =
0
3
f =
9/2
5) Find the area of the regions enclosed by the curves 𝑦 = 𝑥4 − 4𝑥2 + 4, 𝑦 = 𝑥2
Output:
MATLAB code Week 5
Volume of Solid of Revolution
clc
clear all
close all
%%
syms x;
f = input('Enter the function: ');
fL = input('Enter the interval on which the function is defined: ');
yr = input('Enter the axis of rotation y = c (enter only c value): ');
iL = input('Enter the integration limits: ');
Volume = pi*int((f-yr)^2,iL(1),iL(2));
sprintf('Volume is %3f ', double(Volume))
%% Shading the area between the axis of rot. and function
fx = inline(vectorize(f));
xvals = linspace(fL(1),fL(2),201);
xvalsr = fliplr(xvals);
xivals = linspace(iL(1),iL(2),201);
xivalsr = fliplr(xivals);
xlim = [fL(1) fL(2)+0.5];
ylim = fx(xlim);
figure('Position',[100 200 560 420])
subplot(2,1,1)
hold on;
plot(xvals,fx(xvals),'k-','LineWidth',2);
plot([fL(1) fL(2)],[yr yr],'r-','LineWidth',2);
fill([xivals xivalsr],[fx(xivals) ones(size(xivalsr))*yr],[0.8 0.8 0.8],'FaceAlpha',0.8)
%% Marking the axis
%plot([fL(1) fL(2)],[yr yr],'r-','LineWidth',2);
legend('Function Plot','Axis of Rotation', 'Filled Region','Location','Best');
title('Function y=f(x) and Region');
set(gca,'XLim',xlim)
xlabel('x−axis');
ylabel('y−axis');
%% Plotting reflection of the curve about the axis of rot
subplot(2,1,2)
hold on;
plot(xivals,fx(xivals),'b-','LineWidth',2);
plot([iL(1) iL(2)],[yr yr],'r-','LineWidth',2);
fill([xivals xivalsr],[fx(xivals) ones(size(xivalsr))*yr],[0.8 0.8 0.8],'FaceAlpha',0.8)
plot(xivals,-fx(xivals)+2*yr,'m-','LineWidth',2);
fill([xivals xivalsr],[ones(size(xivals))*yr -fx(xivalsr)+2*yr],[1 0.8 0.8],'FaceAlpha',0.8)
title('Rotated Region in xy−Plane');
set(gca,'XLim',xlim)
xlabel('x−axis');
ylabel('y−axis');
%% Solid
[X,Y,Z] = cylinder(fx(xivals)-yr,100);
figure('Position',[700 200 560 420])
Z = iL(1) + Z.*(iL(2)-iL(1));
surf(Z,Y+yr,X,'EdgeColor','none','FaceColor','flat','FaceAlpha',0.6);
hold on;
plot([iL(1) iL(2)],[yr yr],'r-','LineWidth',2);
xlabel('X−axis');
ylabel('Y−axis');
zlabel('Z−axis');
view(21,11);
Problems:
1) Find the volume of the solid generated by revolving the region bounded by 𝑦 =
√𝑥, 𝑜 ≤ 𝑥 ≤ 4 about the line 𝑦 = 1
Output:
Enter the function:
sqrt(x)
Enter the interval on which the function is defined: [0
4]
Enter the axis of rotation y = c (enter only c value): 1
Enter the integration limits:
[0 4]
ans =
'Volume is 4.188790e+00
2) Find the volume of the solid generated by revolving the region bounded by 𝑦 =
𝑠𝑖𝑛 𝑥, 𝑜 ≤ 𝑥 ≤ 𝜋 about the line 𝑦 = 𝑐, 𝑜 ≤ 𝑐 ≤ 1, 𝑐 = 0,0.2,0.4.0.6,0.8,1.0. Can you
identify the range or exact value of c that minimize and maximize the volume of the
solid?
Output:
For C=0
Enter the function:
sin(x)
Enter the interval on which the function is defined: [0
pi]
Enter the axis of rotation y = c (enter only c value): 0
Enter the integration limits:
[0 pi]
ans =
'Volume is 4.934802e+00 '
For c=0.2
Enter the function:
sin(x)
Enter the interval on which the function is defined: [0
pi]
Enter the axis of rotation y = c (enter only c value): 0.2
Enter the integration limits:
[0 pi]
ans =
'Volume is 2.816312e+00 '
For c=0.4
f=
sin(x)
fL =
0 3.1416
yr =
0.4000
iL =
0 3.1416
ans =
'Volume is 1.487391e+00 '
For c=0.6
s
i
n
(
x
)
f
L
=
0 3.1416
yr =
0.6000
iL =
0 3.1416
ans =
'Volume is 9.480374e-01 '
For c=0.8
s
i
n
(
x
)
f
L
=
0 3.1416
yr =
0.8000
iL =
0 3.1416
ans =
'Volume is 1.198253e+00 '