clc;
clear all;
% Define the symbolic variable
syms x real;
% Input the function from the user
f = input("Enter the function f(x): ");
% Compute the first derivative
fx = diff(f, x);
critical_points = solve(fx);
% Calculate min and max of critical points for plotting
cmin = min(double(critical_points));
cmax = max(double(critical_points));
% Plot the original function
figure(1);
ezplot(f, [cmin-2, cmax+2]);
hold on;
% Compute the second derivative
fxx = diff(fx, x);
% Analyze critical points for maxima, minima, and inflection points
for i = 1:length(critical_points)
second_derivative_value = subs(fxx, x, critical_points(i));
function_value = subs(f, x, critical_points(i));
if double(second_derivative_value) == 0
fprintf("The point x = %.2f is an inflection point\n", double(critical_points(i)));
elseif double(second_derivative_value) < 0
fprintf("The maximum point x = %.2f\n", double(critical_points(i)));
fprintf("The value of the function is %.2f\n", double(function_value));
else
fprintf("The minimum point x = %.2f\n", double(critical_points(i)));
fprintf("The value of the function is %.2f\n", double(function_value));
end
plot(double(critical_points(i)), double(function_value), "r*", "MarkerSize", 15);
end
% Identify and plot inflection points
degree = polynomialDegree(fxx);
if degree == 0
fprintf("The given polynomial is second degree or less\n");
else
inflection_points = solve(fxx); % Find inflection points
for i = 1:length(inflection_points)
inflection_value = subs(f, x, inflection_points(i));
plot(double(inflection_points(i)), double(inflection_value), "g*", "MarkerSize", 15);
end
end
% Plot the first derivative
figure(2);
ezplot(fx, [cmin-2, cmax+2]);
title("Plot of the First Derivative of f and Critical Points");
hold on;
for i = 1:length(critical_points)
critical_value = subs(fx, x, critical_points(i));
plot(double(critical_points(i)), double(critical_value), "r*", "MarkerSize", 15);
end
% Plot the second derivative
figure(3);
ezplot(fxx, [cmin-2, cmax+2]);
hold on;
if degree == 0
fprintf("The given polynomial is second degree or less; second derivative plot is not possible.\n");
else
for i = 1:length(inflection_points)
second_derivative_value = subs(fxx, x, inflection_points(i));
plot(double(inflection_points(i)), double(second_derivative_value), "r*", "MarkerSize", 15);
end
title("Plot of the Second Derivative of f and Inflection Points");
end
Cite As
Kirubahari (2024). Concavity (https://www.mathworks.com/matlabcentral/fileexchange/173345-concavity), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2024b
Compatible with any release
Platform Compatibility
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0 |