save results of loop in a vector R -
i run loop:
n = 10 alpha = 0.05 sigma = 0.01 (i in 0:n){ vt10[i] = ((sigma^2)/(alpha^2))*((n-i)+(2/alpha))*exp(-alpha*(n-i))-(1/(2*alpha))*exp(-2*alpha*(n-i))-(3/(2*alpha)) }
however, vt10
outputs 10 outcomes (doesn't include first iteration, when i = 0
). how can create vector include 11 iterations?
answer:
n = 10 alpha = 0.05 sigma = 0.01 vt10 = numeric(11) ## this! (i in 0:n){ vt10[i+1] = ((sigma^2)/(alpha^2))*((n-i)+(2/alpha))*exp(-alpha*(n-i))-(1/(2*alpha))*exp(-2*alpha*(n-i))-(3/(2*alpha)) }
also, need vt10[i+1]
, because loop 0 10, while array index should 1 11.
comment:
your code looks c code. not start index 0, write loop task.
try:
n = 10 alpha = 0.05 sigma = 0.01 = 0:10 vt10 = ((sigma^2)/(alpha^2))*((n-i)+(2/alpha))*exp(-alpha*(n-i))-(1/(2*alpha))*exp(-2*alpha*(n-i))-(3/(2*alpha))
in situation, there no need predefine vector. r knows performing element-wise computation, , automatically assign length-11 vector vt10
.
Comments
Post a Comment