0% found this document useful (0 votes)
35 views

Lab Assignment 2

Uploaded by

D Preethi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Lab Assignment 2

Uploaded by

D Preethi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Lab Assignment-2

1)

AIM: To find the local maxima and minima for polynomial by using first and second
derivative test and plot the function with maximum point, minimum point and point of
inflection.

CODE:

clc
syms x real
f=input ('Enter the function')
fx=diff(f)
c=solve(fx)
cmin=min(double(c))
cmax=max(double(c))

figure(1)
ezplot (f, [cmin-2, cmax+2])
hold on
fxx=diff (fx, x)
for i=1: 1: size(c)
T1=subs (fxx, x, c(i))
T3=subs (f, x, c(i))

if (double(T1) == 0)
sprintf ('The point x is %d inflection point', double(c(i)))
else
if(double(T1) <0)
sprintf ('The maximum point x is %d', double(c(i)))
sprintf ('The value of the function x is', double(T3))
else
sprintf ('The minimum point X is %d', double(c(i)))
sprintf ('The value of the function x is %d', double(T3))
end
end
plot(double(c(i)), double(t3),'r*','markersize',15);
end

%Plotting inflection points for testing concavity

de=polynomialDegree(fxx);
if(de==0)
sprintf ('The given polynomial is second degree or less')
else
d=solve(fxx) %Finding inflection points
for i=1: 1: size(d)
T2=subs (f, x, d(i));
plot(double(c(i)), double(T2),'g*','markersize',15)
end
end
%Identifying maxima and minima through first derivative test

figure (2)
ezplot (fx, [cmin-2,cmax+2])
title('P lotting first derivative of f and critical points')
hold on
for i=1:1:size(c)
T4=subs(fx,x,c(i));
plot(double(c(i)), double(T4),'r*','markersize',15)
end

figure(3)
ezplot(fxx,[cmin-2,cmax+2]);
hold on
if(de==0)
sprintf('The given polynomial is second degree or less, second derivative plot is
not possible ')
else
for i =1:1:size(d)
T4=subs(fxx,x,d(i));
plot(double(d(i)), double(T4),'r*','markersize',15);
end
title('plotting second derivative test and inflection points')
end

OUTPUT:
Enter the function: x^3+x^2
fx=3x^2+2x
c=-2/3, 0
cmin=-0.667
cmax=0
fxx=6x+2
T1=-2
T3=4/27
ans= ‘The point x is -6.666667e-0.1 is inflection point’
t1=2
t3=0
ans= ‘The minimum point x is 0’
ans= ‘The value of function x is 0’
ans= ‘The value of functions is 0’
d=-1/3

FIGURES:
SCREENSHOTS:
2. Write the MATLAB code to find the local maxima and minima for any
function by using first and second derivative test and plot the function with
maximum point, minimum point and point of inflection.

AIM: To find the local maxima and minima for any function by using first
and second derivative test and plot the function with maximum point,
minimum point and point of inflection.

CODE:

clc
clear all;
syms x real;
syms trigo;
f=input (“Enter the function f(x):”)
fx=diff(f)
c=solve(fx)
cmin=min(double(c))
cmax=max(double(c))

figure (1)
ezplot (f, [cmin-2, cmax+2])
hold on
fxx=diff (fx, x)
for i=1: 1: size(c)
T1=subs (fxx, x, c(i))
T3=subs (f, x, c(i))

if (double(T1) == 0)
sprintf ('The point x is %d inflection point:', double(c(i)))
else
if (double(T1) <0)
sprintf ('The maximum point x is %d:', double(c(i)))
sprintf ('The maximum value of x:', double(T3))
else
sprintf ('The minimum point x is %d:', double(c(i)))
sprintf ('The minimum value of x: ', double(T3))
end
end
plot(double(c(i)), double(t3),'r*','markersize',15);
end
de=fxx;
if(de==0)
sprintf ('The given polynomial is second degree or less')
else
d=solve(fxx)
for i=1: 1: size(d)
T2=subs (f, x, d(i));
plot(double(c(i)), double(T2),'g*','markersize',15)
end
end

figure (2)
ezplot (fx, [cmin-2,cmax+2])
title('Plotting first derivative of f and critical points')
hold on
for i=1:1:size(c)
T4=subs(fx,x,c(i));
plot(double(c(i)), double(T4),'r*','markersize',15)
end

figure(3)
ezplot(fxx,[cmin-2,cmax+2]);
hold on
if(de==0)
sprint ('The given polynomial is second degree or less, second derivative plot is not
possible ')
else
for i =1:1:size(d)
T4=subs(fxx,x,d(i));
plot(double(d(i)), double(T4),'r*','markersize',15);
end
title ('Plotting second derivative test and inflection points')
end

OUTPUT:
Enter the function f(x): sin(x)+tan(x)
fx=cos(x)+tan(x)^2+1
c=pi
fxx=2*tan(x)*(tan(x)^2+1)-sin(x)
T3=0
d=0
SCREENSHOTS:
CODE:
FIGURES:
OUTPUT:

You might also like