Error: Object of type 'closure' is not subsettable

Asked

Viewed 806 times

5

I’m trying to encapsulate a code in functions but in the process such error happens:

Object of type 'closure' is not subsettable

But I can’t find why. Follow 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 attempted encapsulation:

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 insertion of 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 

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 subsettable

What can be done?

1 answer

3


The functions in R work a little different than what you should be used to. In your case just change the first two linahs of the function Gauss for:

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

In the R functions do not share scope. All objects created within a function collected by gc after the function has finished running (unless they have been created with <<-). Thus, you always need 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:

Error: object 'v_h' not found

It would be much easier to know what happened. The problem is that you must have some function called v_h defined in its global environment.

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

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

This is because the functions in the R first look for objects within your Environment and recursively within your Environment father. For example:

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

Even without x be declared within the scope of funcao it is found and calculated as the R searches for variables with name x in the parent environment.

To be able to circumvent these problems, it is good to occasionally exclude all objects from the global environment. If you had done so you would have received the error that would indicate the problem more clearly. To delete all objects just use: rm(list = ls()).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.