f = function(x) {abs(x-3) - 2}
plot(f,xlim = c(0,5), ylim = c(-3,5)); abline(h=0,
v=c(0,5),
lty = 2,
col = "blue")
b.
#3 triangulos retângulos
# 1*1/2 - 2*2/2 - 2*2/2
soma_ratangulo = function(n,inicio,fim){
base = fim - inicio
acrescimo = base/n
vet = c()
for(i in 1:n){
vet = c(vet,i*acrescimo)
}
soma = f(inicio)*acrescimo
for(i in vet[-length(vet)]){
altura = f(i)
base = acrescimo
soma = base*altura + soma
}
return(soma)
}
soma_ratangulo(50,0,5)
## [1] -3.45
soma_ratangulo(100,0,5)
## [1] -3.475
soma_ratangulo(150,0,5)
## [1] -3.483333
#quanto mais retangulos, melhor é a proximação
IntegralDominioReal = function(a,b,f,e = 0.001){
n = 100
while(TRUE){
d = (b-a)/n
A_S = 0 ; A_I = 0
x = a
while(x < b){
A_S = A_S + d*max(f(x),f(x+d))
A_I = A_I + d*min(f(x),f(x+d))
x = x + d
}
if((A_S - A_I) < 2*e) return((A_S+A_I)/2)
else n = n + 10
}
}
IntegralDominioReal(a = 0, b = 5,f)
## [1] -3.5
IntegralDominioRealRec = function(f,a,b,e = 0.001, n = 100){
d = (b-a)/n
A_S = 0 ; A_I = 0
x = a
while(x < b){
A_S = A_S + d*max(f(x),f(x+d))
A_I = A_I + d*min(f(x),f(x+d))
x = x + d
}
if((A_S - A_I) < 2*e) return((A_S+A_I)/2)
else return(IntegralDominioRealRec(f,a,b,e,n+10))
}
IntegralDominioRealRec(f,a = 0,b = 5)
## [1] -3.5
f = function(x){x^2 -x - 1}
IntegralDominioRealRec(f,-1,1,0.005)
## [1] -1.337242
IntegralDominioReal(-1,1,f,0.005)
## [1] -1.337242
g = function(x){x*exp(x^2)}
IntegralDominioReal(0,2,g,0.01)
## [1] 26.79908
(exp(4)-1)/2
## [1] 26.79908
IntegralDominioReal_list = function(a,b,f,e = 0.001){
n = 100
while(TRUE){
d = (b-a)/n
A_S = 0 ; A_I = 0
x = a
while(x < b){
A_S = A_S + d*max(f(x),f(x+d))
A_I = A_I + d*min(f(x),f(x+d))
x = x + d
}
if((A_S - A_I) < 2*e) return(list((A_S+A_I)/2,n))
else n = n + 10
}
}
IntegralDominioRealRec_list = function(a,b,f,e = 0.001, n = 100){
d = (b-a)/n
A_S = 0 ; A_I = 0
x = a
while(x < b){
A_S = A_S + d*max(f(x),f(x+d))
A_I = A_I + d*min(f(x),f(x+d))
x = x + d
}
if((A_S - A_I) < 2*e) return(list((A_S+A_I)/2,n))
else return(IntegralDominioRealRec_list(a,b,f,e,n+10))
}
f = function(x){x^2 -x - 1}
IntegralDominioReal_list(-1,1,f,0.005)
## [[1]]
## [1] -1.337242
##
## [[2]]
## [1] 510
g = function(x){x*exp(x^2)}
IntegralDominioReal_list(0,2,g,0.01)
## [[1]]
## [1] 26.79908
##
## [[2]]
## [1] 10930
(exp(4)-1)/2
## [1] 26.79908
IntegralDominioPositivo = function(a,b,f,e = 0.001){
if( a < 0 | b < 0) stop("Pontos precisam ser positivos")
n = 100
while(TRUE){
d = (b-a)/n
A_S = 0 ; A_I = 0
x = a
while(x < b){
A_S = A_S + d*max(f(x),f(x+d))
A_I = A_I + d*min(f(x),f(x+d))
x = x + d
}
if((A_S - A_I) < 2*e) return((A_S+A_I)/2)
else n = n + 10
}
}
f = function(x){sqrt(log(x))/x}
IntegralDominioPositivo(1,2,f,0.0001)
## [1] 0.3849083
g = function(x){sqrt(abs(log(x)))/x}
IntegralDominioPositivo(1/2,3/2,g,0.0001)
## [1] 0.5568446
f = function(x){log(x-1)}
#Domínio da f é tal que x-1 > 0 => x > 1
IntegralDominio_maior1 = function(a,b,f,e = 0.001){
if( a < 1 | b < 1) stop("Pontos precisam ser positivos")
n = 100
while(TRUE){
d = (b-a)/n
A_S = 0 ; A_I = 0
x = a
while(x < b){
A_S = A_S + d*max(f(x),f(x+d))
A_I = A_I + d*min(f(x),f(x+d))
x = x + d
}
if((A_S - A_I) < 2*e) return((A_S+A_I)/2)
else n = n + 10
}
}
IntegralDominio_maior1(3,5,f,0.0001)
## [1] 2.159283