I am trying to encapsulate a code in functions but in the process this error happens:
object of type 'closure' is not subsettable
But I can not find why. Here is the code working:
h = (b-a)/n
B = array(0,c(n-1,n-1)) #gera Matriz (n-1)x(n-1) = {0}
x = array(0,c(n-1)) #gera vetor x = {0}
for(i in 1:(n-1))
{
x[i] = a+i*h
}
####Gera a Matriz####
for (i in 1:(n-1)) #preenche a diagonal principal com a expressao: 2+(h^2)*q(x)
{
B[i,i] = 2+(h^2)*q
}
for (i in 1:(n-2)) #preenche as diagonais abaixo/acima com -1. Os demais elementos permanecem 0
{
B[i+1,i]=-1
B[i,i+1]=-1
}
####Gera o Vetor de Termos Independentes####
v_h = array(0,c(n-1,1)) #gera vetor v_h = {0}
for (i in 1:(n-1)) #preenche o vetor v_h com a expressao: -(h^2)*r(x[i])
{
v_h[i,1]=-(h^2)*r(x[i])
}
v_h[1,1]=v_h[1,1] + alpha #primeiro termo : -(h^2)*r(x[i]) + alpha
v_h[n-1,1]=v_h[n-1,1] + beta #ultimo termo : -(h^2)*r(x[i]) + alpha
####Metodo de Gauss####
aux = 0
solucao = array(0,c(n-1,1)) #gera o vetor solucao obtido por Gauss
start.time <- Sys.time() #inicia contagem do tempo
for (j in 1:(n-2)) # escalona a Matriz B
{
if (B[j,j] == 0)
{
for (k in 1:(n-1))
{
if (B[k,j] != 0)
{
aux = B[j,j]
B[j,j] = B[k,j]
B[k,j] = aux
}
}
}
for (i in (j+1):(n-1))
{
m = ((-1)*B[i,j])/B[j,j]
for(k in j:(n-1))
{
B[i,k]=B[i,k]+(m*B[j,k])
}
v_h[i,1]= v_h[i,1]+(m*v_h[j,1])
}
}
solucao[n-1] = v_h[n-1]/B[n-1,n-1]
for(i in (n-2):1) # resolução do sistema triangular
{
solucao[i] = v_h[i]
for(j in (i+1):(n-1))
{
solucao[i]=(solucao[i])-(B[i,j]*solucao[j])
}
solucao[i]=(solucao[i]/B[i,i])
}
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken #tempo obtido
solve (B,v_h) #solucao obtida pelo R
solucao #solucao obtida pelo metodo de Gauss
Now follows the encapsulation attempt:
GeraMatriz = function(n,a,b,q)
{
h = (b-a)/n
B = array(0,c(n-1,n-1)) #gera Matriz (n-1)x(n-1) = {0}
x = array(0,c(n-1)) #gera vetor x = {0}
for(i in 1:(n-1))
{
x[i] = a+i*h
}
for (i in 1:(n-1)) #preenche a diagonal principal com a expressao: 2+(h^2)*q(x)
{
B[i,i] = 2+(h^2)*q
}
for (i in 1:(n-2)) #preenche as diagonais abaixo/acima com -1. Os demais elementos permanecem 0
{
B[i+1,i]=-1
B[i,i+1]=-1
}
return (B)
}
####
GeraVh = function(n,a,b,y,r)
{
h = (b-a)/n
x = array(0,c(n-1)) #gera vetor x = {0}
for(i in 1:(n-1))
{
x[i] = a+i*h
}
alpha = y(a)
beta = y(b)
v_h = array(0,c(n-1,1)) #gera vetor v_h = {0}
for (i in 1:(n-1)) #preenche o vetor v_h com a expressao: -(h^2)*r(x[i])
{
v_h[i,1]=-(h^2)*r(x[i])
}
v_h[1,1]=v_h[1,1] + alpha #primeiro termo : -(h^2)*r(x[i]) + alpha
v_h[n-1,1]=v_h[n-1,1] + beta #ultimo termo : -(h^2)*r(x[i]) + alpha
return(v_h)
}
####
Gauss = function(n,a,b,y,q,r)
{
GeraMatriz(n,a,b,q)
GeraVh(n,a,b,y,r)
aux = 0
solucao = array(0,c(n-1,1)) #gera o vetor solucao obtido por Gauss
start.time <- Sys.time() #inicia contagem do tempo
for (j in 1:(n-2)) # escalona a Matriz B
{
if (B[j,j] == 0)
{
for (k in 1:(n-1))
{
if (B[k,j] != 0)
{
aux = B[j,j]
B[j,j] = B[k,j]
B[k,j] = aux
}
}
}
for (i in (j+1):(n-1))
{
m = ((-1)*B[i,j])/B[j,j]
for(k in j:(n-1))
{
B[i,k]=B[i,k]+(m*B[j,k])
}
v_h[i,1]= v_h[i,1]+(m*v_h[j,1])
}
}
solucao[n-1] = v_h[n-1]/B[n-1,n-1]
for(i in (n-2):1) # resolução do sistema triangular
{
solucao[i] = v_h[i]
for(j in (i+1):(n-1))
{
solucao[i]=(solucao[i])-(B[i,j]*solucao[j])
}
solucao[i]=(solucao[i]/B[i,i])
}
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken #tempo obtido
solve (B,v_h) #solucao obtida pelo R
solucao #solucao obtida pelo metodo de Gauss
}
After inserting the parameters,
y = function(x){return((exp(sqrt(2)*x))+(exp(-sqrt(2)*x))+((2/27)*sin(4*x))-((1/6)*x*cos(4*x)))}
r = function(x){return(3*x*cos(4*x))}
a = 0
b = 1
q = 2
n = 10
The console returns me the following error:
Gauss (n, a, b, y, q, r) Error in v_h [i, 1]: object of type 'closure' is not substable
What can be done?