source("http://macosa.dima.unige.it/r.R") # If I have not already loaded the library ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Integrals of two variables functions # For the integral of a function of two variables (f in [a,b]×[c,d]) # we can use the command INTEGRAL(f, a,b, c,d, N) that calculates # the "volume" of a N×N parallelepiped whose upper part approximates # the surface of f. |
# Another example: # Integral of (x,y) → x^3*y in T. # I define F so: F = function(x,y) ifelse(x^2+y^2<=1, x^3*y, 0) # What is the range of F? MAXF2(F, 0,1, 0,1) # ~ max in x,y F(max) 0.8675266 0.4973907 0.3247477 MINF2(F, 0,1, 0,1) # ~ min in x,y F(min) 0.7265506 0.7270941 0.0000000 # From 0 to 0.3; I expect an integral much lower than 1 INTEGRAL(F, 0,1, 0,1, 200) # 0.04166803 INTEGRAL(F, 0,1, 0,1, 400) # 0.04166006 INTEGRAL(F, 0,1, 0,1, 800) # 0.0416674 INTEGRAL(F, 0,1, 0,1, 1600) # 0.04166695 fraction(0.0416666666666) # 1/24 # |
# # Another example: # integral of f in the triangle -> f = function(x,y) exp((y-x)/(x+y)) PLANE(0,3, 0,3); g = function(x,y) x+y-2; CURVE(g, "brown") # I define F so: F = function(x,y) ifelse(y <= 2-x, f(x,y), 0) # I put the found values in u:
u = NULL i=1; u[i] = INTEGRAL(F, 0,2, 0,2, 200); more(u[i]) # 2.35580556893031 i=2; u[i] = INTEGRAL(F,0,2,0,2, 400); more(u[i]); u[i-1]-u[i] # 2.35287392674027 0.002931642 Subsequent changes will be minor: I can round to 2.35. # If I want to move forward, I also evaluate the relationship between one variation and the # next: i=3; u[i] = INTEGRAL(F,0,2,0,2, 800); more(u[i]); u[i-1]-u[i]; (u[i-2]-u[i-1])/(u[i-1]-u[i]) # 2.35164340648721 0.00123052 2.382441 i=4; u[i] = INTEGRAL(F,0,2,0,2, 1600); more(u[i]); u[i-1]-u[i]; (u[i-2]-u[i-1])/(u[i-1]-u[i]) # 2.35102515514742 0.0006182513 1.990324 i=5; u[i] = INTEGRAL(F,0,2,0,2, 3200); more(u[i]); u[i-1]-u[i]; (u[i-2]-u[i-1])/(u[i-1]-u[i]) # 2.35071560622102 0.0003095489 1.997265 # Taking into account that in this case at every doubling of N the variation is halved, I # can estimate the distance of the value of the limit equal to the last variation, in fact: V=1/2; SV=0; for(i in 1:1000) SV=SV+V^i; SV # 1 1/2 + 1/2^2 + 1/2^3 + ... = 1 # Anyway let's move on: i=6; u[i] = INTEGRAL(F, 0,2, 0,2, 6400); more(u[i]); u[i-1]-u[i]; (u[i-2]-u[i-1])/(u[i-1]-u[i]) # 2.35055913656474 0.0001564697 1.978332 i=7; u[i] = INTEGRAL(F, 0,2, 0,2, 12800); more(u[i]); u[i-1]-u[i]; (u[i-2]-u[i-1])/(u[i-1]-u[i]) # 2.35048072920642 7.840736e-05 1.995599 # The integral is: 2.35048072920642-7.840736e-05; more(2.35048072920642-7.840736e-05) # 2.350402 2.35040232184642 # The first rounding is certainly acceptable: 2.350402 # This integral is not trivial, but it is symbolically calculable; you can get: exp(1)-1/exp(1); more(exp(1)-1/exp(1)) # 2.350402 2.3504023872876 # It is the same value that we have easily found above. # # For the graph of two variables functions see S19. Other examples of use