How to specify intervals of increasing, decreasing of a function in MATLAB
13 views (last 30 days)
Show older comments
rezheen
on 9 May 2025
Commented: Walter Roberson
on 12 May 2025
Hello, I want to specify intervals of increasing & decreasing of a function for a specific range; say -20-20. This is my code here:
clc, close, clear
syms x y real
dy=1-(4/x^2); x=~0; % This is the derivative of the function
% dy=(sin(x)-1)*(2*cos(x)+1); % (0<=x<=2*pi) % Code doesn't work for this.
fplot(dy); hold on; axis([-5, 5, -5, 5]);
j=xline(0); set(j,'color','black','Linewidth', 1.5)
k=yline(0); set(k,'color','black','Linewidth', 1.5)
cp=solve(dy); % Finding critical points
y_cp=subs(dy,cp);
[~,idx] = unique(cp);
cp = cp(idx);
y_cp = y_cp(idx);
[num, den]=numden(dy); % Not a conventional way to find other critical points
% I need to account for ties here. The technique I used above for 1st set
% of critical points (idx, unique) isn't working.
cp2=solve(den==0);
disp('The critical points are:')
for k =1:length(cp)
fprintf("(%.2f,%.2f)\n",cp(k), y_cp(k))
plot(cp,y_cp,'*', 'color', 'black');
end
for k =1:length(cp2)
fprintf("x = %.2f is a critical point, y is undefined\n",cp2(k))
end
% y is increasing if dy > 0, decreasing if dy < 0, if dy=0 it's a critical point. Code below doesn't work.
for i = -20:20
dy_sub_i=subs(dy,i);
end
if dy_sub_i>0
fprintf('f is increasing in %s-%s', i(0), i);
elseif dy_sub_i<0
fprintf('f is decreasing in ');
end
5 Comments
Walter Roberson
on 9 May 2025
It is a function. It is sort of increasing for positive x, if you look only at the irrational numbers, but it is everywhere discontinuous, and everywhere decreasing, when you transition from an irrational number to a rational one.
It is not obvious to me that the function is everywhere discontinuous.
If you take any particular irrational input, x, and add to it a rational
then the resulting
will be irrational, so
and
are both non-zero (x = 0 is not considered for this purpose because 0 is rational.)




For any rational
there exists a smaller rational
namely
with N->infinity . And
would be non-zero for each of those.




The question then becomes, whether for each
there exists a
such that
is rational. Ummm... maybe ? It is not at all obvious to me that such exists.



It is usually said that there are an infinite number of irrational numbers between any two rational numbers. That would surely imply that there are an infinite number of irrational numbers beside each irrational number. So by the "closed compact set" arguments, f(x) is continuous at each irrational x.
John D'Errico
on 10 May 2025
Edited: John D'Errico
on 10 May 2025
Sorry. Your logic fails. All we need is the definition of a continuous function.
The definition of a continuous function requires three things.
A function f is continuous at a point c if:
- f(c) is defined.
- lim f(x) as x-->c exists
- lim f(x) as x-->c = f(c)
Certainly f(x) is well defined at all points on the real line.
At all rational points (except zero):
- For any rational point c, f(c)=0
- Consider the limit of f(x) as x-->c. There will be irrational points arbitrarily close to c, where f(x)=x. Thus, the limit will approach c, which is non-zero for any rational non-zero c.
- Therefore, the limit does not equal f(c)=0 unless c=0.
At all irrational points c, we can make a similar argument that the limit will not exist, since there exist rational numbers arbitrarily close to c. This argues that the limit for an irrational c, will approach 0. Of course, we probably don't even need to make this second argument about what happens at an irrational point, since we know what happens at any rational point, and the rationals are dense on the real line.
That means the function is NOT continuous anywhere (except arguably at 0), since the limit is not the same as the value at that given point.
The flippant statement about an infinite number of irrationals between any pair of rational numbers is meaningless in this respect, since the same statement applies in the reverse. You cannot just play around with infinities like that, not in this context.
Accepted Answer
Walter Roberson
on 9 May 2025
Edited: Walter Roberson
on 9 May 2025
Start at the left end of the interval. Record sign() of dy there, along with the current position. Move right one position. Calculate sign() of dy there, along with the current position. If it is the same as the recorded dy, continue moving right.
If you reach the right-hand edge, then emit an appropriate message about the sign of the interval indicating the ends of the interval.
If you reach a place where the sign changes, the emit an appropriate message about the sign of the interval, indicating the locations of the start of the interval and the last location where the interval was the same sign; then record the current location as the start of the new interval.
Note: there is a much more compact way of doing this, using diff() on the sign() of the derivatives
6 Comments
Walter Roberson
on 12 May 2025
syms x y real
dy=-3*x^2+4*x; % new function
fplot(dy); hold on; axis([-5, 5, -5, 5]);
j=xline(0); set(j,'color','black','Linewidth', 1.5)
k=yline(0); set(k,'color','black','Linewidth', 1.5)
cp=solve(dy); y_cp=subs(dy,cp); [~,idx] = unique(cp);
cp = cp(idx); y_cp = y_cp(idx);
disp('The critical points are:')
for k =1:length(cp)
fprintf("(%.2f,%.2f)\n",cp(k), y_cp(k))
plot(cp,y_cp,'*', 'color', 'black');
end
a1 = round(min(cp)-1);
a2 = round(max(cp)+1);
a3 = a1:0.1:a2;
a4 = arrayfun(@(V)limit(dy, x, V), a3);
sa4 = sign(a4);
starts = strfind([0, sa4<0], [0 1]);
stops = strfind([sa4<0, 0], [1 0]);
for K = 1 : length(starts)
fprintf('f is decreasing on %g (%g) : %g (%g)\n', a3(starts(K)), a4(starts(K)), a3(stops(K)), a4(stops(K)));
end
starts = strfind([0, sa4>0], [0 1]);
stops = strfind([sa4>0, 0], [1 0]);
for K = 1 : length(starts)
fprintf('f is increasing on %g (%g) : %g (%g)\n', a3(starts(K)), a4(starts(K)), a3(stops(K)), a4(stops(K)));
end
More Answers (1)
John D'Errico
on 9 May 2025
Edited: John D'Errico
on 10 May 2025
This can get fairly messy, if your function is carefully designed to cause you problems. For example, you may need to find a singularity. And singularities can come in many different forms, not just as a root of the denominator of a rational polynomial. As such, numden is worthless to you. A derivative singularity can cause problems, for example:
fplot(@(x) sqrt(abs(x - sqrt(2))),[-20,20])
grid on
The singularity resides here at x==sqrt(2), an irrational number. So just testing at isolated points will not help you a lot, nor will the function numden.
Or your function may just be the simple
fplot(@tan,[-20,20])
grid on
which is everywhere increasing, except at some isolated (transcendental) locations, where it transitions from +inf to -inf.
And of course, discontinuities can cause problems all of their own. Next is a cute one, which will cause all hell to break loose if you are not careful, as it is everyehere constant, except at some isolated locations, and those locations do not occur at any set of convenient points on the real line. (I could easily make that worse too.)
fplot(@(x) round(2*sin(x.^2/10)),[-20,20])
grid on
Do you see the issues? And I've not even scratched the surface of the nasty behavior such a tool could encounter. How about the simple function f(x), defined as:
f(x) = 0 if x is rational
f(x) = x if x is irrational
It is a function. It is sort of increasing for positive x, if you look only at the irrational numbers, but it is everywhere discontinuous, and everywhere decreasing, when you transition from an irrational number to a rational one. (As if the word "transition" makes any sense in this context.) And if you consider only the rational numbers, then it is everywhere constant at zero. And since all numbers that can be represented in double precision are inherantly rational, then f(x) will be arguably identically zero, when implemented in MATLAB. And worse, you can't even locate the uncountably many transition points. As I said, nasty.
In order to do anything with your question, you will need to severely restrict the behavior of your allowed functions. For example, you may want to require continuity, even some degree of differentiability, as otherwise, one can always create some function that will play hell with any code you write.
Remember that not all functions are polynomials, even though those are the ones you tend to see in a class.
All of this gets much easier if you are willing to require some degree of continuity. If you get rid of any breaks in continuity, then it gets simpler. If you exclude derivative singularities and breaks at that level, then too, you start to exclude most of the problematic functions I showed. But that also might exclude things like rational polynomials. Even something as simple as 1/x is an issue, as if that is allowed, then you need to properly handle functions like tan(x). Again, writing code to handle general problems like this becomes tricky. It requires you to carefully set boundaries on what class of problems you would agree to handle.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!