source("http://macosa.dima.unige.it/r.R")    # If I have not already loaded the library
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
# To evaluate the derivative of a piecewise-defined function we must derive every
# "sub-function".
#
f = function(x) ifelse( x<1, x, ifelse(x<3, (x-2)^2, 4-x) )
BF=4; HF=2.75
Plane(0,5, -2,2)
graph2(f, 0,5, "brown")
text(4.2,0.2,"f",font=2,col="brown")
# It is a continuous function. We want to study the derivative and the integral function.
# The derivative df(x) is not defined for x=1 and x=3. 
 
                    
 
deriv(f,"x")
# Error: 'Ifelse' not present in the derivative tables
# We must derive the sub-function f1, f2, f3:
f1=function(x) x; f2=function(x) (x-2)^2; f3=function(x) 4-x
df1 = function(x) eval(deriv(f1,"x"))
df2 = function(x) eval(deriv(f2,"x"))
df3 = function(x) eval(deriv(f3,"x"))
df = function(x) ifelse(x<1, df1(x), ifelse(x<3, df2(x), df3(x) ) )
Plane(0,5, -2,2); graph(f, 0,5, "brown"); text(4.2,0.2,"f",font=2,col="brown")
graph(df, 0,5, "red"); text(1.5,-1.5,"df",font=2,col="red")
# To graphically represent the integral function ∫[0,x]f we use Gintegral (see)
Gintegra(f, 0,5, "blue")
# Error: extremely bad integrand behaviour
# We use GintegraA:
GintegraA(f, 0,5, "blue")
# Error: extremely bad integrand behaviour
GintegraB(f, 0,5, "blue")           # OK
text(4.35,1.3,"Int f",font=2,col="blue")
 
                    
 
# The integral function has derivative 0 where f = 0
POINT(4,integral(f,0,4), "red")
integral(f,0,4); fraction(last())
#  1.666667       5/3                                       Int f has a maximun here
# It has a rising point of inflection where his derivative (f) has a local maximum.
POINT(c(1,3), c(integral(f,0,1),integral(f,0,3)), "green")
integral(f,0,1); fraction(last())
#   0.5           1/2
integral(f,0,3); fraction(last())
#   1.166667      7/6
# At x=2 f (derivative of Int f) has a local minimum and f(x)=0:
# this is (for Int f) a failing point of inflection and a stationary point
POINT(2, integral(f,0,2), "magenta")
integral(f,0,2); fraction(last())
#   0.8333333     5/6

Other examples of use