source("http://macosa.dima.unige.it/r.R") # If I have not already loaded the library ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- S 04 Lenghts and Areas # areaF(f, a,b, N) (or areaFun) calculates the area of the N-gon that approximates the # graph of f. # Examples for N = 2, 4, 10 thousand, 100 thousand for a certain f BF=3; HF=2 f <- function(x) sqrt(1-x^2) PLANE(-1,1, 0,1); areaF(f,-1,1,2) # 1 PLANE(-1,1, 0,1); areaF(f,-1,1,4) # 1.3660254 PLANE(-1,1, 0,1); areaF(f,-1,1,1e4) # 1.5707947# Use areaF2 if (for great N) you want a faster computation PLANE(-1,1, 0,1); areaF2(f,-1,1,1e5) # 1.5707963 (=π/2) # Another example (graphs on the left): Plane(0,3,0,3); areaF(floor, 0,3, 2) # 3.75 Plane(0,3,0,3); areaF(floor, 0,3, 1000) # 3.0015 Plane(0,3,0,3); areaF2(floor, 0,3, 1e4) # 3.00015
![]()
# The areas are signed: positive/negative if they are above/below the x axis # (graphs above on the right) f1 <- function(x) -x^2+1; f2 <- function(x) -f1(x) BF=3; HF=3 PLANE(-1,1, -1,1); areaF(f1,-1,1,2000) # 1.333333 PLANE(-1,1, -1,1); areaF(f2,-1,1,2000) # -1.333333 # # I can calculate the area of the polygon that is under the graph of a piecewise linear # function (with coordinates in x and y), using the areaPol command, if I define the # function AREA (but I can use another name) in this way: AREA = 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") AREA = function(x,y) areaPol( c(x[length(x)],x[1],x), c(0,0,y) ) AREA(x,y) # 49 # # areaPar(x,y, a,b, N), areaPolar(r, a,b, N) calculate the area enclosed by a curve # in parametric form and polar form (see S01). To calculate the length of a curve (when # it is finite), I use lengFun(f,a,b,N), lengPar(x,y,a,b,N) or lengPolar(r,a,b,N) # depending on how it is defined. lengPar3(x,y,z,a,b,N) works analogously in the case # of a curve in the space. [N is the number of sides of the polygonal chain that approximates the curve] # As can be seen in S08, to calculate the area of figures, integration can be used.
# Examples for calculating the length of the semicircle (in various ways):
3.141593 | ![]() |
length = 24 area = 6 π | ||
![]() |