Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.0 kB
2
Indexable
Never
// (1)To Plot the function and its derivatives
Matlab Code

clc
clear all
syms x real
f= input(‘Enter the function f(x):’);
fx= diff(f,x)
fxx= diff(fx,x)
D = [0, 5];
l=ezplot(f,D)
set(l,‘color’,‘b’);
hold on
h=ezplot(fx,D);
set(h,‘color’,‘r’);
e=ezplot(fxx,D);
set(e,‘color’,‘g’);
legend(‘f’,‘fx’,‘fxx’)
legend(‘Location’,‘northeastoutside’)

// (2) To find the maxima and minima of the single variable function and visualize it.
Matlab Code

clc
clear all
syms x real
f= input(‘Enter the function f(x):’);
fx= diff(f,x);
fxx= diff(fx,x);
c = solve(fx)
c=double(c);
for i = 1:length(c)
T1 = subs(fxx, x ,c(i) );
T1=double(T1);
T3= subs(f, x, c(i));
T3=double(T3);
if (T1==0)
sprintf(‘The inflection point is x = %d’,c(i))
else
if (T1 < 0)
sprintf(‘The maximum point x is %d’, c(i))
sprintf(‘The maximum value of the function is %d’, T3)
else
sprintf(‘The minimum point x is %d’, c(i))
sprintf(‘The minimum value of the function is %d’, T3)
end
end
cmin = min(c);
cmax = max(c);
D = [cmin-2, cmax+2];
ezplot(f,D)
hold on
plot(c(i), T3, ‘g*’, ‘markersize’, 15);
end

// (4) Find the volume of the region D enclosed by the surfaces z = x ^ 2 + 3y ^ 2 and 
   z = 8 - x ^ 2 - y ^ 2

clc
clear
syms x y z
xa = - 2;
xb = 2;
ya=-sqrt (2-x^2/2); 
yb = sqrt(2 - x ^ 2 / 2); 
za=x^2+3 y^2; 
zb = 8 - x ^ 2 - y ^ 2;
I=int (int (int (1+0*z, z, za, zb), y, ya, yb), x, xa, xb) 
viewSolid (z, za, zb, y, ya, yb, x, xa, xb)

// (5) Find the volume of the regicg cut from the cylinder x ^ 2 + y ^ 2 = 4 by the plane
       z = 0 and the plane x + z = 3 The limits of integration are z = 0to  3 - x, x = - root(4 - y) to sqrt(4 - y) , v = - 2to  2
clear
clc
syms x y z
ya = - 2;
yb = 2;
xa = - sqrt(4 - y ^ 2); 
xb = sqrt(4 - y ^ 2);
za =0+0^ * x+0^ * y;
zb= 3 - x -0^ * y;
i = int (int (int ( 1 +0 * z,z,za,zb),x,xa,xb) ,y,ya, yb)
viewSolidone ( z, za, zb, x, xa, xb, y, ya, yb )

// (6) evaluting volume undersurface To find integrate integrate (x + y)/4 dy from x / 2 to x dx from 1 to 2

syms x y z
int (int ((x+y)/4, y, x (2, x), x, 1, 2 ) 
viewSolid ( z ,0+0^ * x+0^ * y y^ * y, (x + y) / 4 ,y,x| 2,x,x,1,2)

// (7) To find the volume of a solid generated by revolving a curve about the
    x−axis

clc
clearvars
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));
disp(['Volume is: ', num2str(double(Volume))])
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),'−b','LineWidth',2);
fill([xvals xvalsr],[fx(xvals) ones(size(xvalsr))*yr],[0.8 0.8 0.8],'FaceAlpha',0.8)
plot([fL(1) fL(2)],[yr yr],'−r','LineWidth',2);
legend('Function Plot','Filled Region','Axis of Rotation','Location','Best');
title('Function y=f(x) and Region');
set(gca,'XLim',xlim)
xlabel('x−axis');
ylabel('y−axis');
subplot(2,1,2)
hold on;
plot(xivals,fx(xivals),'−b','LineWidth',2);
fill([xivals xivalsr],[fx(xivals) ones(size(xivalsr))*yr],[0.8 0.8 0.8],'FaceAlpha',0.8)
fill([xivals xivalsr],[ones(size(xivals))*yr −fx(xivalsr)+2*yr],[1 0.8 0.8],'FaceAlpha'
,0.8)
plot(xivals,−fx(xivals)+2*yr,'−m','LineWidth',2);
plot([iL(1) iL(2)],[yr yr],'−r','LineWidth',2);
title('Rotated Region in xy−Plane');
set(gca,'XLim',xlim)
xlabel('x−axis');
ylabel('y−axis');
[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(22,11);

// (8) The volume of the solid generated by the revolving the curve y = root x about the line y = 1 from x = 1 to
       x = 4 is given by the following code:

clear
clc
syms x
f(x)=sqrt(x); % Given function
yr=1; % Axis of revolution y=yr
I=[0,4]; % Interval of integration
a=I(1);b=I(2);
vol=pi*int((f(x)-yr)^2,a,b);
disp('Volume of solid of revolution is: ');
disp(vol); % Visualization if solid of revolution
fx=matlabFunction(f);
xv = linspace(a,b,101); % Creates 101 points from a to b
[X,Y,Z] = cylinder(fx(xv)-yr);
Z = a+Z.*(b-a); % Extending the default unit height of the
%cylinder profile to the interval of integration.
surf(Z,Y+yr,X) % Plotting the solid of revolution about y=yr
hold on;
plot([a b],[yr yr],'-r','LineWidth',2); % Plotting the line y=yr
view(22,11); % 3-D graph viewpoint specification
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');