Not able to get local and absolute max or min of a function or graph symbolically

14 views (last 30 days)
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
x = -2.00 is a critical point. y' is undefined here x = 2.00 is a critical point. y' is undefined here
The critical point(s) is/are
(1.41,2.00) (-1.41,-2.00)
% % 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
The local max is -2.0000 at x= -1.4142
The local min is 0.0000 at x= -2.0000
The local max is 0.0000 at x= 2.0000
The local min is -2.0000 at x= -1.4142
The local max is -2.0000 at x= -1.4142
The local min is 0.0000 at x= -2.0000
The local max is -2.0000 at x= -1.4142
The local min is 0.0000 at x= 2.0000

Answers (1)

Walter Roberson
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
rezheen
rezheen on 4 May 2025
Am I on the right path with this code?
dy=diff(y);
x_roots=solve(dy,x);
y_x_roots=subs(y,x_roots);
x_roots_min=min(x_roots); x_roots_=max(x_roots);
yxx=diff(dy,x);
for k = 1:length(x_roots)
d2=subs(yxx,x,x_roots(k));
d1=subs(y,x,x_roots(k));
if d2== 0
fprintf('The test fails at x=%.2f',x_roots(k))
elseif d2<0
fprintf('The absolute maximum is %.2f at x = %.2f\n', d1,x_roots(k))
else
fprintf('The absolute minimum is %.2f at x = %.2f\n', d1, x_roots(k))
end
end
I'm able to get absolute max, min. I just need local max, min
Walter Roberson
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.)

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!