You reused 'i' in the inner 'for' loop. This looks like it give
the
correct answer:
> n<-c(10,8,7,5)
> n_goal<-c(8,9,9,4)
> w<-c(0.1,0.1,0.1,0.1)
> matrix<-mat.or.vec(6,4)
> FI<-function(n_t) {
+
(((n_goal[1]-n[1]+W[1]-f[1]+n_t[1])^2)+((n_goal[2]-n[2]+W[2]-f[2]+n_t[2]-n_t[1])^2)+
+
((n_goal[3]-n[3]+W[3]-f[3]+n_t[3]-n_t[2])^2)+((n_goal[4]-n[4]+W[4]-f[4]-n_t[3])^2))
+ }> for (i in 1:6) {
+ W<-c(n*w)
+ sf<-c(1,1,1,1)
+ f1<-W%*%sf
+ f<-c(f1,0,0,0)
+ out<-nlm(FI, n_t<-c(0,0,0), hessian=TRUE)
+ for (j in 1:3) {
+ if (out$estimate[j]<0) out$estimate[j]=0
+ }
+ n<-c(n-W+f-c(out$estimate,0)+c(0,out$estimate))
+ matrix[i, ]<-n
+ print(n)
+ }
[1] 8.0 9.0 8.5 4.5
[1] 8.00 9.00 8.95 4.05
[1] 8.000003 9.000000 8.999999 3.999998
[1] 8.000002 9.000000 8.999999 3.999998
[1] 8.000002 9.000000 8.999999 3.999998
[1] 8.000002 9.000000 8.999999 3.999998> matrix
[,1] [,2] [,3] [,4]
[1,] 8.000000 9 8.500000 4.500000
[2,] 8.000000 9 8.950000 4.050000
[3,] 8.000003 9 8.999999 3.999998
[4,] 8.000002 9 8.999999 3.999998
[5,] 8.000002 9 8.999999 3.999998
[6,] 8.000002 9 8.999999 3.999998>
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Tue, Nov 26, 2013 at 3:32 AM, ANNA SIMEONIDOU
<assymeon2@hotmail.com>wrote:
>
> Hi all,
> I'm sorry, maybe the solution in my problem is easy, but i am still new
to
> R, and any help
> here is appreciated!
> I want to create a data frame, or a matrix in which the output of each
> loop will be stored.
> Here is the program
>
> > n<-c(10,8,7,5)
> > n_goal<-c(8,9,9,4)
> > w<-c(0.1,0.1,0.1,0.1)
> > matrix<-mat.or.vec(6,4)
> > FI<-function(n_t) {
> +
>
(((n_goal[1]-n[1]+W[1]-f[1]+n_t[1])^2)+((n_goal[2]-n[2]+W[2]-f[2]+n_t[2]-n_t[1])^2)+
> +
>
((n_goal[3]-n[3]+W[3]-f[3]+n_t[3]-n_t[2])^2)+((n_goal[4]-n[4]+W[4]-f[4]-n_t[3])^2))}
> > for (i in 1:6) {
> + W<-c(n*w)
> + sf<-c(1,1,1,1)
> + f1<-W%*%sf
> + f<-c(f1,0,0,0)
> + out<-nlm(FI, n_t<-c(0,0,0), hessian=TRUE)
> + for (i in 1:3) {
> + if (out$estimate[i]<0)
> + out$estimate[i]=0}
> + n<-c(n-W+f-c(out$estimate,0)+c(0,out$estimate))
> + matrix[i, ]<-n
> + print(n)
> + }
> matrix
> n is the output i want to store in the matrix in each loop to have
> subsequently a plot.
>
> (Each n will be a point and i'd like to have 6 separately points---six
4
> dimensions vectors)
> The problem is here: i get wrong matrix
> > print(matrix)
> [,1] [,2] [,3] [,4]
> [1,] 0.000000 0 0.000000 0.000000
> [2,] 0.000000 0 0.000000 0.000000
> [3,] 8.000002 9 8.999999 3.999998
> [4,] 0.000000 0 0.000000 0.000000
> [5,] 0.000000 0 0.000000 0.000000
> [6,] 0.000000 0 0.000000 0.000000
> only third row has right numbers, all the other have zero,
> while print(n) seems right
>
> [1] 8.0 9.0 8.5 4.5
> [1] 8.00 9.00 8.95 4.05
> [1] 8.000003 9.000000 8.999999 3.999998
> [1] 8.000002 9.000000 8.999999 3.999998
> [1] 8.000002 9.000000 8.999999 3.999998
> [1] 8.000002 9.000000 8.999999 3.999998
>
> Actually i want latest matrix to be the same with print(n).
> Also, i tried to create a data frame but it is difficult for me because
> outputs are
> vectors and not single numbers.
>
> Thanks in advance for your help!
> Simeonidou Anna
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]