0

I am currently trying to write a code that will solve the consumption path over a 100x100 state space, subject to possible shocks in production. I currently have

 ###################################Part 3.1###################################################

nonpersist<-matrix(c(.5,.5,.5,.5),nrow=2,ncol=2) 
persist<-matrix(c(.9,.1,.3,.7),nrow=2,ncol=2) 



space<-seq(length=100, from=0, to=7.2) ##Create a sequence of 100 values ending at the                      point where value goes negative
alpha<-0.3 #take alpha as given
beta<-0.98 #take beta as given
vprime <- c(1:100) ##create a vector length 100 to be replaced later
t_vj <- c(1:100)  ##create a vector length 100 to be replaced later
A <- c(rep(4,100))
random<-c(runif(100))
iterater<-function(space){  ##create a function to perform the iteration
  for(i in 1:100){  
    for(j in 1:100){  
      if((A[i]*(space[i]^alpha)-space[j])<0){
          t_vj[j]=-99
      }
      else if(random[i]<.5){
              A[i]<-4
              t_vj[j+1] <- (log(A[i]*(space[i]^alpha)-space[j])+ beta*t_vj[j])
      }
      else{
              A[i]<-20
              t_vj[j+1] <- (log(A[i]*(space[i]^alpha)-space[j])+ beta*t_vj[j])
      }
    }
  }
  vprime[i]<-max(t_vj)  
  plot(space,vprime, xlab="State Space", ylab="Value")
}
iterater(space)  #call the function

Which unfortunately is currently yielding the graph https://i.stack.imgur.com/UXoUt.png

Which is not what we should expect in a non-linear function.

Any ideas?

Any help would be much appreciated.

Ali
  • 56,466
  • 29
  • 168
  • 265
Jake_Mill22
  • 39
  • 1
  • 7

1 Answers1

0

You need

vprime[i] <- max(t_vj)

To appear within the first for loop. Specifically, the last 3 lines of your iterater function should look like:

  vprime[i]<-max(t_vj)
}
plot(space,vprime, xlab="State Space", ylab="Value")

enter image description here

Robert Krzyzanowski
  • 9,294
  • 28
  • 24