4.3 Monotonic Functions and the First Derivative Test

This page presents a MATLAB utility for plotting a function together with its first derivative, a monotonicity profile and a sign profile for its first derivative.

Octave / MATLAB


Octave / MATLAB

Monotonicity and the sign of the first derivative

The function monotonicity (in monotonicity.m) accepts a one variable symbolic expression and a two element array as input. It plots the expression over the interval specified by the two element array. It also plots the expression's first derivative, a sign profile for this first derivative (using '+' for positive and '.' for negative), and a monotonicity profile for the original expression (using upward pointing triangles for increasing and downward pointing triangles for decreasing). Notice that $f'>0$ always corresponds to $f$ increasing, and $f'<0$ to $f$ decreasing. Be careful not to "jump a track" and suppose that $f'>0$ means $f'$ is increasing.

>> syms x >> monotonicity( x^2-10*sin(3*x), [2 7] )

You may wish to examine the implementation of monotonicity for its simple (albeit kludgy) use of a logical mask -- a fairly important MATLAB programming trick.

function monotonicity( fcn, interval ) % Plot fcn and first derivative over interval % together with sign profile of the first derivative of fcn % Author: R Smyth % Version 1.0 8/9/2013 syms fp fp = diff(fcn); % differentiate fcn a = interval(1); % extract interval endpoints b = interval(2); v = linspace(a,b,101); % inputs for fcn and derivative vv = linspace(a,b,26); % inputs for sign profile of derivative y = double( subs( fcn, v ) ); % evaluate fcn and convert outputs to floating point form yp = double( subs( fp, v ) ); % evaluate derivative and convert to floating point ypLowRes = double( subs( fp, vv ) ); % evaluate derivative with wider spacing (suitable for profile display) ymin = min([min(y) min(yp) 0]); % determine plot range ymax = max([max(y) max(yp) 0]); h = ymin - .1*(ymax-ymin); % determine height for display of derivative sign profile hh = ymin - .05*(ymax-ymin); % determine height for display of monotonicity profile ywinMin = h - .1*(ymax-ymin); % determine vertical limits for viewing window ywinMax = ymax + .1*(ymax-ymin); pos = h + (ypLowRes<=0)*(-.2*(ymax-ymin)); % h when derivative is positive, out of window otherwise up = hh + (ypLowRes<=0)*(-.2*(ymax-ymin)); % hh when fcn is increasing, out of window otherwise neg = h + (ypLowRes>=0)*(-.2*(ymax-ymin)); % h when derivative is negative, out of window otherwise down = hh + (ypLowRes>=0)*(-.2*(ymax-ymin)); % hh when fcn is decreasing, out of window otherwise % plot function (in blue), first derivative (in red), x-axis (black) % derivative sign profile with '+' for positive and '.' for negative, % monotonicity profile with upward pointing triangles for increasing % and downward pointing triangles for decreasing plot(v,y,'b',v,yp,'r',[a b],[0 0],'k',vv,pos,'r+',vv,neg,'r.',vv,up,'b^',vv,down,'kv'); grid on axis([a b ywinMin ywinMax]); % set viewing window varname = char(symvar(fcn,1)); fout = ['f(' varname ')']; text(b+.01*(b-a), hh, 'function'); % label monotonicity profile text(b+.01*(b-a), h, 'derivative'); % label sign profile legend('function', '1st derivative', 'Location', 'Northwest') title( [fout ' = ' char(fcn)] ) % Note especially the use of the logical "mask" (ypLowRes<=0) which evaluates to 0 whenever % ypLowRes is positive, and 1 otherwise.