Not able to get local and absolute max or min of a function or graph symbolically
14 views (last 30 days)
Show older comments
Hello, I'm trying to code a program that will calculate critical points and tell the user where local and absolute max or min values are located in the function or graph based on those critical points. So far, I find the critical points (correct for both examples) and plot of course, but have trouble with local, absolute max and min. This is my code:
clc, close, clear
syms x y real
y=x*sqrt(4-x^2); % 1st example
fplot(y, [-3 3])
% y=x^2*sqrt(3-x); % 2nd example
% fplot(y, [-2 4])
dy=diff(y);
x_roots=solve(dy,x); % roots of the derivative
y_x_roots=subs(y,x_roots); % y original of those roots
[num, den]=numden(dy);
dy_den=diff(den);
% % Case1: If denominator in y' is a constant
if dy_den==0
disp('The critical point(s) is/are')
for k =1:length(x_roots)
fprintf("(%.2f,%.2f)\n",x_roots(k), y_x_roots(k))
end
% % Case2: If there's a variable in y' denominator
else
den1=solve(den==0); % Getting roots of den
m=length(den1);
if (m>1 & (den1(1)==den1(2)))
fprintf("x = %.2f is a critical point. y' is undefined here",den1(1))
else
fprintf("x = %.2f is a critical point. y' is undefined here\n",den1)
end
disp(newline)
disp('The critical point(s) is/are')
for k =1:length(x_roots)
fprintf("(%.2f,%.2f)\n",x_roots(k), y_x_roots(k))
end
end
% % Trying to calculate local max and min. Runs bad for both examples. I
% % should have a local max, a local min, an absolute max, an absolute min for the highlighted example.
y_den1=subs(y,den1);
for m=1:length(y_x_roots)
for n =1:length(y_den1)
if m < n & (n== max(n)) & (m==min(m))
fprintf("The local max is %.4f at x= %.4f\n",y_den1(n), den1(n))
fprintf("The local min is %.4f at x= %.4f\n",y_x_roots(k), x_roots(k))
else
fprintf("The local max is %.4f at x= %.4f\n",y_x_roots(k), x_roots(k))
fprintf("The local min is %.4f at x= %.4f\n",y_den1(n), den1(n))
end
end
end
0 Comments
Answers (1)
Walter Roberson
on 4 May 2025
Edited: Walter Roberson
on 4 May 2025
[num, den]=numden(dy);
dy_den=diff(den);
% % Case1: If denominator in y' is a constant
if dy_den==0
In general, the derivative of the denominator will be a formula. You should not compare the formula to constants. Instead you should loop over the critical points, evaluating the second derivative at each one, and test the results at the evaluated points.
It doesn't hurt to take the denominator of the second derivative and test whether it is zero at the critical point, in order to detect singularities. However, it is also of interest to take the limit of the second derivative at each of the critical points, as the limit could potentially be finite even though the formula would suggest something divided by 0.
2 Comments
Walter Roberson
on 4 May 2025
First of all, you do not need x_roots_min or x_roots_
Secondly, testing the individual roots will only tell you local minima and local maxima. You need to compare the y value over each of the minima or maxima in order to determine the absolute minima or maxima. (Remember to take into account the possibility of ties.)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!