Error: object of type 'closure' is not subsettable

5

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?

    
asked by anonymous 21.12.2016 / 19:33

1 answer

3

The functions in the R work a little differently than you should be accustomed to. In your case, just change the first two lines of the Gauss function to:

B <- GeraMatriz(n,a,b,q)
v_h <- GeraVh(n,a,b,y,r)

No% of functions do not share scope. All objects created within a function collected by R after the function has finished running (unless they were created with gc ). This way, you always have to assign the result of a function to the value of a variable in the next function.

The default error in this case should be:

  

<<-

It would be much easier to know what happened. The problem is that you must have some function called Error: object 'v_h' not found defined in your global environment.

See for example what happens when you try to get elements of the function:

> v_h <- function(x){
+   return(x)
+ }
> v_h[1,1]
Error in v_h[1, 1] : object of type 'closure' is not subsettable

This happens because functions in R first look for objects within their environment and recursively within their environment parent. For example:

> x <- 1
> funcao <- function(a){
+   return(x*2)
+ }
> funcao(10)
[1] 2

Even though v_h is declared within the scope of x it is found and calculated because R looks for variables with funcao name in the parent environment.

To get around these issues, it is good to delete all objects from the global environment from time to time. If you had done so you would have received the error that would indicate the problem more clearly. To delete all objects just use: x .

    
21.12.2016 / 20:46