---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
# We see, in detail, an exercise that is not easy to solve by hand (not even with
# WoframAlpha, which, like much of the symbolic computing software, is not reliable for
# the study of functions: it is necessary to use tricks to work with odd exponent powers
# and to avoid making illicit simplifications of the type, for instance, √-1/√-1 -> 1).
#
#     Study the graph of   x → (x/4)2/3 - (x-4)1/3   and identify the
#     (local) minimum and maximum points and the concavity changes
#                       (i.e. the inflection points)
#
# First of all I find the problem that for software z^a is defined only for z ≥ 0; then
# I use rad3 that in this version of R is defined everywhere:
 
g = function(x) rad3((x/4)^2) - rad3(x-4)
#
# First I study the function graphically, without "calculations".
# I start with a wide interval, and then narrow it.
 
graphF( g, -100,100, 1)
 
# I get the first graph below to the left.
# To confirm that for x that grows or decreases g(x) tends to grow beyond any limit, we
# make numerical tests (try with 10, 100, 1000, ... and -10, -100, -1000, ..., and round
# the outputs to shorten them)
 
n=1:12; round(g(10^n))
# [1]   0   4   30   163   809   3869   18205  85035  395850  1839861  8545238 39675026
n=1:12; round(g(-10^n))
# [1]   4  13   50   206   901   4069   18636  85963  397850  1844170  8554521 39695026
 
# I see that in both cases the outputs multiply (about) by 100 when the input is multi-
# plied by 1000. OK [I can say it grows like |x|^(2/3), which is understandable by the
# expression of g(x) since 2^3 > 1/3 (see at the bottom the graphs in a wider range)].
# Formally, the limit for  x -> Inf  and for  x -> -Inf  is Inf.
# So I can narrow the input interval:
 
graphF( g, -10,100, 1)
 
# I get the second graph.
 
 
# I have red marked those that at first sight might be the points sought. Let's first
# look at the left side of the graph of g.
graphF( g, -2,12, 1)
# I get the graph above to the right.
 
# The program does the chart by pointing it: the dotted vertical line does not mean
# that the function is not continuous. I can check this with some zoom:
graphF( g, 3.9,4.1, 1)
 
     
 
# I get the chart above to the left.
#
# Looking at the expression of G, I perceive that for x=0 and x=4 (where x/4 and x-4
# change sign) I have critical points where there are a relative minimum and a
# concavity change (inflection point). To study what happens to the right of 12 I will
# need to calculate the derivatives. Let's see what we can do to the left without them.
#
h = minmax(g, -2,2); h; g(h)
# [1] -1.589587e-14     "pratically" 0
# [1] 1.587401

# I try to put 1.587401 in WolframAlpha, and I see it is an approximation of 2^(2/3)
# I check this: g(0) = rad3(4), that is 4^(1/3), or (2^2)^(1/3) = 2^(2*1/3) = 2^/3)
# So A = (0,rad3(4)) is a relative minimum point.
#
# For x=4 g(x)=1. As I understand from the graph in C = (4,1) I have a vertical
# inflection point.
#
# Let's look at B and D:
j = minmax(g, 1,3); j; g(j);  k = minmax(g, 5,10); k; g(k)
# [1] 2
# [1] 1.889882
# [1] 8
# [1] 0
# If I put 1.889882 in WolframAlpha I see it is an approximation of 3/2^(2/3)
# [=3*2^(1/3)/2]. I can (as above) check this. B = (2,3*rad3(2)/2) is a relative
# maximum point. Instead D = (8,0) is a relative minimum point.
#
# Let's study the part of the graph to the right of point D.
# Is there an oblique inflection point? Above I marked a hypothetical point E of that
# kind. To test this I have to calculate the first derivative first and see where it
# grows or decreases (or I have to calculate the second derivative). I do both with R.
# I use this also to study the problems that I have solved earlier.
#
# I can calcolate g'  with deriv(g,"x"); I can save it as function with
# dfg <- function(x) eval(deriv(g,"x"))
 
deriv(g,"x")
 
# Error: ...
# The definition of g contains rad3 which is defined as follows:
rad3
# function(x) sign(x)*abs(x)^(1/3)
# I should break the definition of g right and left of 4, where the input of rad3(x-4)
# changes sign.
 
g1 = function(x) ((x/4)^2)^(1/3)+(4-x)^(1/3)
g2 = function(x) ((x/4)^2)^(1/3)-(x-4)^(1/3)
# I check the thing graphically (graph above to the right):
 
HF=2.7; BF=2.7
graphF( g, -2,12, 1)
graph( g1, -2,4, "red")
graph( g2, 4,12, "blue")
# OK           
dg1 <- function(x) eval( deriv(g1,"x") )
solution(dg1,0, 1,3)
# [1] 2
dg2 <- function(x) eval( deriv(g2,"x") )
solution(dg2,0, 5,10)
# [1] 8
# I found B and D again. But are they the same values?
more( minmax(g, 1,3) ); more(solution(dg1,0, 1,3))
#    1.99999998773738       2
# The second method is more accurate because the algorithm to find the minimum and
# maximum without the derivative (although being more general) is more approximate
# (but differences are generally negligible from the point of view of application).
#
# To find E I can study where grow/decrease D(g)
maxmin(dg2,10,100)
# [1] 31.52936
 
# Or I can study where the 2nd derivative is 0
d2g2 <- function(x) eval( deriv2(g2,"x") )
solution(d2g2,0, 10,100)
# [1] 31.52936
 
# I found the same value. In reality, the two procedures do not give the same values:
more( maxmin(dg2,10,100) ); more( solution(d2g2,0, 10,100) )
#    31.5293616373527          31.5293611968877
 
# Again, the second method gives a more accurate result.
# The corresponding y is:
g( solution(d2g2,0, 10,100) )
# [1] 0.9412041
# So E = (31.52936, 0.9412041)
#
# If I want I can calculate the derivatives by hand, or with R. By deriv(g2,"x") I get
# ((x/4)^2)^((1/3)-1)*((1/3)*(2*(1/4*(x/4))))-(x-4)^((1/3)-1)*(1/3)   which I can
# simplify in   (x/4)^(-1/3)/6 - (x-4)^(-2/3)/3 ...   or I can use WolgramAlpha.  If
# I put:   solve ( d/dx d/dx ((x/4)^2)^(1/3)-(x-4)^(1/3) ) = 0
# I get:   31.52936119688764498693379…
#
# Here's how the graph with the solutions was obtained:
 
HF=2.7; BF=2.7*2
graphF( g, -1,40, 1)
graph ( g, 3,5, 1)
a = 0; b = 2; c = 4; d = 8; e = solution(d2g2,0, 10,100)
POINT( c(a,b,c,d,e), g( c(a,b,c,d,e) ), "red")
 
# other elements on the chart:
 
coldash="red"; gridV(2); gridV(4); gridV(8) 
type(0,1.4,"A"); type(2,2.1,"B"); type(5.5,1,"C")
type(8,0.2,"D"); type(31.53,0.75,"E")
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Comparison of g with:
f = function(x) abs(x)^(2/3)
graphF (g, -1e5,1e5, "black"); graphF (f, -1e5,1e5, "brown")
 
     
 
# I see that (for x that tends to Inf and to -Inf) both curves rise beyond any
# limitations and keep concavity down

Other examples of use