source("http://macosa.dima.unige.it/r.R") # If I have not already loaded the library ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------S08Integrals and Derivatives (and Limits). Taylor polynomial. Volumes.#deriv(f,VariableName),deriv2, …,deriv6# Note: deriv(f,"a") equalsD(body(f),"a")G = function(u) sin(u); deriv(G,"u")#cos(u) f = function(x) 3*x^3-5*x^2+x+3 graphF(f, -2,4, "blue") deriv(f,"x")#3 * (3 * x^2) - 5 * (2 * x) + 1 # To use the expression produced by "deriv" you need to evaluate it witheval: df = function(x)eval(deriv(f,"x")) graph(df,-3,5, "red") # I could also define df by writing 3*(3*x^2)-5*(2*x)+1 [ or 9*x^2-10*x+1 ] deriv2(f,"x")#3 * (3 * (2 * x)) - 5 * 2 d2f = function(x)eval(deriv2(f,"x")); graph(d2f,-3,5, "violet") deriv3(f,"x")#3 * (3 * 2) d3f = function(x)eval(deriv3(f,"x"))+x-x # I put the "x" with a trick graph(d3f,-3,5, "green") # To evaluate the derivative oflog10(x)andlog2(x)you must use log(x)/log(10) and # log(x)/log(2), where log(x) is the inverse of exp(x) f=function(x) log(x); g=function(x) log(x)/10; h=function(x) log(x)/2 deriv(f,"x"); deriv(g,"x"); deriv(h,"x")#1/x 1/x/10 1/x/2# To evaluate the derivative of a function whereabs(u) appears, it needs to be # replaced withsqrt(u^2) # # If f is apiecewise-definedfunction we must derive every sub-function (see). # #Note.dfcalculated by the software can have a widerdomainthanf. Example: f = function(x) log(1+tan(x/2)) BF=5; HF=3; a=-2*pi; b=2*pi; Plane(a,b, -5,5) graph2(f, a,b, "brown"); df = function(x) eval( deriv(f,"x") ); deriv(f,"x")#1/2/cos(x/2)^2/(1 + tan(x/2)) graph2(df, a,b, "seagreen") FDOM=f; graph(DOM, a,b, "red") # DOM is defined 0 in the domain of FDOM pointO(pi*c(-1,-1/2,1,2-1/2),rep(0,4),"blue") text(-5,-1/2,"domain",font=2,col="red"); text(-1/2,-1.5,"f",font=2,col="brown") text(-1/2,2.5,"df",font=2,col="seagreen"); text(-2.5,-1.5,"?",font=2,col="seagreen") ##I can calculate theintegralof apiecewise linear function(with coordinates in x # and y), using theareaPolcommand, if I define: INTEGRAL = function(x,y) areaPol( c(x[length(x)],x[1],x), c(0,0,y) ) # Example: x = c(10,13, 17, 19); y = c(7,1,9,8) Plane(min(x),max(x), 0,max(y)); polyl(x,y,"blue") INTEGRAL = function(x,y) areaPol( c(x[length(x)],x[1],x), c(0,0,y) ) INTEGRAL(x,y)#49# We can useareaPolalso forspline functions(see): x = c(-0.2, 1.3, 1.7, 3.2, 4.1, 4.6) y = c(9.1, 17.5, 20.7, 25.4, 35.7, 51.2) Plane(-1,6,-10,70); POINT(x,y,1); L=spline(x,y,1000); polyline(L$x,L$y,"red") INTEGRAL(L$x,L$y) # INTEGRAL defined above#108.6348#But I can use theintegral(f, a,b) command, if I define such function explicitly:f = function(x) ifelse(x<13, 7-6/3*(x-10), ifelse(x<17, 1+8/4*(x-13), 9-1/2*(x-17))) Plane(10,19, 0,9); graph(f, 10,19, "blue") integral(f, 10,19)#49# A function that can be described more easily:g = function(x) sin(x) integral(sin, 0,pi/2); integral(sin, 0,pi); integral(sin, 0,2*pi)#1 2 2.032977e-16 ("pratically" 0)#Here, how to calculate the effective force on the mast of a racing sailbot # (from "Numerical Methods", Chapra & Canale, McGraw-Hill)# Seefor integrals of F from A to B when A or B arehereinfiniteor F isundefined# at A or B .# Definite integrals of many funcions can be calculated with theareaFcommand:see. # Only when we have focused the theoremfundamental theorem of calculus(see here or # here) we can interpret areas as defined integrals.##Gintegral(f,A,B,col) traces the graph of x ->∫_{[A,x]}f from A to B (A and B must be # finite);Gintegra,Gintegrplot slimmer graphics.Plane(-pi,2*pi, -1,1); graph1(cos, -pi,2*pi, "blue") Gintegr(cos, 0,2*pi, "red"); Gintegr(cos, 0,-pi, "violet") POINT(0,0, "red")# The graph on the right:Plane(-pi,2*pi, -1,1); graph1(cos, -pi,2*pi, "blue") POINT(2.5,0.5, "red")GintegrK(cos, 2.5,2*pi, 0.5, "brown"); GintegrK(cos, 2.5,-pi, 0.5, "seagreen")# WithGintegralK(f,A,B, k, col) (andGintegraK,GintegrK) I can trace the graph # of x ->k +∫_{[A,x]}f# In very special casesGintegral(Gintegra, Gintegr) may not work; in such cases you # can try usingGintegralAor, if it is not working,Gintegralor …BGintegral, or #DGintegral(or0GintegraA,GintegrA, …Gintegra0,Gintegr0, for slimmer graphics):f = function(x)ifelse(x<13, 7-6/3*(x-10), ifelse(x<17, 1+8/4*(x-13), 9-1/2*(x-17))) Plane(10,19, 0,50); Gintegra(f, 10,19, "blue")#Error in integrate Plane(10,19, 0,50); GintegraA(f, 10,19, "brown")# For the same reasons ifintegraldoes not work you can useintegralA, …,intergralD# orintergral0. # # Insome case, somemanipulationsare necessary to achieve a result: a simple example. # # WithRwe can compute definite integrals. We can also calculate theindefinite#integrals(ieantiderivatives) ofpolynomial functions. Q = c(-2,-3,2.4,-1/5) # the polynomial -2-3*x+2.4*x^2-1/5*x^3 q = function(x) Q[1]+x*Q[2]+x^2*Q[3]+x^3*Q[4] # the integral:I= function(p){I=NULL; for (i in 1:length(p)) I = c(I,p[i]/i); I}I(Q); fraction(I(Q))#-2.00 -1.50 0.80 -0.05#-2 -3/2 4/5 -1/20 -2*x-3/2*x^2+4/5*x^3-1/20*x^4 + constant Iq = function(x) x*I(Q)[1]+x^2*I(Q)[2]+x^3*I(Q)[3]+x^4*I(Q)[4] # ∫_{[-1,2] }q using I(Q) Iq(2)-Iq(-1)#-4.05 # I can calculate the defined integer in each case, with: integral(q, -1,2)#-4.05# For other functions I can useWolframAlpha. Let's see how to use it in this case: #integrate-2-3*x+2.4*x^2-1/5*x^3dx# -0.05 x^4 + 0.8 x^3 - 1.5 x^2 - 2 x + constant #integrate-2-3*x+2.4*x^2-1/5*x^3dx fromx=-1to2 # -0.45 # # Theelementary functions(functions of one variable which are the composition of # arithmetic operations, powers, exponentials, logarithms, trigonometric functions, # inverse trigonometric functions) are closed under differentiation but not under # integration. Some examples of elementary functions that have not an elementary # antiderivative: sqrt(1+x^3) sqrt(1-x^4) 1/sqrt(1+x^4) 1/log(x) log(log(x)) # exp(x^2) exp(-x^2) exp(x)/x exp(exp(x)) x^2*exp(-x^2) sin(x^2) cos(exp(x)) # sin(x)/x. Hence the importance of the numerical integration we have seen. # # We can also trace thedirection fieldof thesolutions of an indefinite integral(see):##Seeherefor the compute oflimits of a function##Seeherefor the "study of a function"#Seeherefor the correct use of x ->x^xand similar function that use^.

x -> x^x |