MATLAB code Week 4 and 5
MATLAB code Week 4 and 5
A. Integration
àInitialization:
value = 0;
dx = (b-a)/n;
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:
Output
z =
2
value =
z =
Figure Window:
3) e^x+tan(x)
Output
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
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)
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
f =
9/2
kokler =
0
3
f =
9/2
Output:
MATLAB code Week 5
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:
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
ans =
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 =
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 '