0% found this document useful (0 votes)
11 views6 pages

Session3 2

Uploaded by

alginbaby989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Session3 2

Uploaded by

alginbaby989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

BMAT101P-CALCULUS LAB

Evaluating Extremum of Single Variable Functions

Dr. Mohit Kumar


VIT Chennai
Graph of a Curve and its Tangent Line in the Neighbourhood D of a Point
clear;
close all;
clc;

syms x
y = input('Enter the function f in terms of x: ');
% Example: Try the function y = x^2 - 2*x;

x1 = input('Enter the x value at which tangent is to be drawn: ');


% Example: Try the point x1 = 2;

D = [x1-2, x1+2]; % Region about x1 (or Neighbourhood of x1)


fplot(y, D); % Graph of the curve in D
hold on;

% Equation of the tangent line passing through x1.


yd = diff(y, x); % Differentiation in MATLAB
slope = subs(yd, x, x1); % Finding the slope at x1
y1 = subs(y, x, x1); % Finding the value of the function at the given point
plot(x1, y1, 'ko'); % Plot the point

Tgt_line = slope*(x - x1)+y1; % Tangent Line Equation at the given point


h = fplot(Tgt_line, D); % Plotting the Tangent Line
set(h, 'Color', 'r'); % Set the color of the tangent line to red

hold off;
Plot the Function and its Derivatives
• To plot the function and its derivatives in MATLAB, you can use the ‘ezplot’, ‘plot’, or ‘fplot’ functions.
• Here are some examples code to plot the function and its rst and second derivatives:

%% Example 1

% Define the symbolic variable


syms x
% Define the function
f = x^3 - 2*x^2 + x + 3;
% Calculate the first derivative of the function
df = diff(f);
% Calculate the second derivative of the function
d2f = diff(df);
% Plot the function and its derivatives using ezplot
ezplot(f, [-3, 3])
hold on
ezplot(df, [-3, 3])
ezplot(d2f, [-3, 3])
legend('Function', 'First derivative', 'Second derivative')
xlabel('x')
ylabel('y')
title('Function and its derivatives')
grid on
fi
Plot the Function and its Derivatives
%% Example 2

% Define the symbolic variable


syms x
% Define the function
f = x^3 - 2*x^2 + x + 3;
% Calculate the first and second derivatives of the function
df = diff(f);
d2f = diff(df);
% Convert the symbolic functions to function handles
f = matlabFunction(f);
df = matlabFunction(df);
d2f = matlabFunction(d2f);
% Define the range of x values for the plot
x_min = -3;
x_max = 3;
x_range = linspace(x_min, x_max, 100);
% Plot the function and its derivatives
figure
hold on
plot(x_range, f(x_range), 'LineWidth', 2)
plot(x_range, df(x_range), 'LineWidth', 2)
plot(x_range, d2f(x_range), 'LineWidth', 2)
legend('Function', 'First derivative', 'Second derivative')
xlabel('x')
ylabel('y')
title('Function and its derivatives')
grid on
Plot the Function and its Derivatives

%% Example 3

clear
clc

syms x
% Input a function "f(x)" and the range of "x"
f= input('Enter the function f(x):'); % e.g. f(x) = x^3 +
5*x^2 + 3*x + 1
D=input('Enter the range of x:'); % e.g. D = [0, 5];

% Calculate the first and second derivatives of "f(x)"


fx= diff(f, x);
fxx= diff(fx, x);

% Plot "f(x)" and its first and second derivatives


fplot(f, D, 'b');
hold on
fplot(fx, D, 'r');
fplot(fxx, D, 'g');
legend('Function', 'First derivative', 'Second derivative')
legend('Location', 'northeastoutside');
Extremum of a Single Variable Function
clear; close all; clc
syms x real
f= input('Enter the function f(x):');
% For example f(x)=x^3-6*x^2+9*x+1
fx= diff(f,x);
fxx= diff(fx,x);
c = solve(fx);
fplot(f, [min(double(c))-2, max(double(c))+2])
hold on
for i = 1:length(c)
T1 = subs(fxx, x, c(i));
T3= subs(f, x, c(i));
if (double(T1)>0)
fprintf('The minimum point x is %d \n', double(c(i)))
fprintf('The minimum value of the function is %d \n', double(T3))
plot(double(c(i)), double(T3), 'r*', 'markersize', 15);
elseif (double(T1)<0)
fprintf('The maximum point x is %d \n', double(c(i)))
fprintf('The maximum value of the function is %d \n', double(T3))
plot(double(c(i)), double(T3), 'mo', 'markersize', 15);
else
fprintf('x = %d can be an inflection point.\n', double(c(i)))
plot(double(c(i)), double(T3), 'kx', 'markersize', 15);
end
end

You might also like