原始对象 1 不能用作迭代器 [英] Raw object 1 cannot be used as an iterator

查看:67
本文介绍了原始对象 1 不能用作迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将其重写为以下内容,

I have rewitten this as the following,

NN = 2000;
mu = 0;
sigma = 10;
task3a = Table[Random[NormalDistribution[mu,sigma]], {NN}];
ListPlot[task3a, AxesLabel->{" No.obs.", "value of the obs"}, 
PlotLabel -> " Normal Distribution"];

a= 0.6; 
b =-0.45; 

task4a = Table [0, {NN}] ; 
task4a[[1]] = task3a[[1]]; 
task4a[[2]] = a*task4a[[1]] +task3a[[2]]; 
For [i = 3, i <= NN, i++, 
    task4a[[i]] = a*task4a[[i -1]] 
                + b*task4a[[i -2]] 
                + task3a[[i]];
] 
ListPlot[task4a, AxesLabel -> {"No.obs.", "value of the obs"}, PlotLabel-> "Autoregression process for norm.dist. white noise"];

(**************************************************)
avg = (1/NN) * Sum[task4a[[i]], {1, NN}]; 

task5a = Table[0, {33}] ; 

For [k = 0, k <= 32, k++, 
  task5a[[k + 1]] = (1/(NN-k)) * 
  Sum[(task4a[[i]] -avg)*(task4a[[i + k]] - avg), {1, NN-k}] ;
]

ListPlot[task5a, PlotLabel ->"K estimator for AR(2) normal distribution", Joined -> True, PlotRange ->All, AxesLabel -> {"k", "K(k)"}] ;

错误信息

上面的代码正在生成以下错误信息 Sum::itraw,

The above code is generating the following error message Sum::itraw,

看起来,for 循环有问题.

Looks like, there is some problem with the for loop.

我无法理解.

推荐答案

正如@agentyp 所提到的.问题是两个地方的总和索引.执行此代码后程序正常运行.

As @agentyp mentioned. Problem is sum indexes in two places. After execution of this code program works correctly.

NN = 2000;
mu = 0;
sigma = 10;
task3a = Table[Random[NormalDistribution[mu, sigma]], {NN}];
ListPlot[task3a, AxesLabel -> {" No.obs.", "value of the obs"}, 
  PlotLabel -> " Normal Distribution"];

a = 0.6;
b = -0.45;

task4a = Table[0, {NN}];
task4a[[1]] = task3a[[1]];
task4a[[2]] = a*task4a[[1]] + task3a[[2]];
For[i = 3, i <= NN, i++, 
 task4a[[i]] = a*task4a[[i - 1]] + b*task4a[[i - 2]] + task3a[[i]];]
ListPlot[task4a, AxesLabel -> {"No.obs.", "value of the obs"}, 
  PlotLabel -> "Autoregression process for norm.dist. white noise"];

(**************************************************)
avg = (1/NN)*
   Sum[task4a[[i]], {i, 1, NN}];

task5a = Table[0, {33}];

For[k = 0, k <= 32, k++, 
 task5a[[k + 1]] = (1/(NN - k))*
    Sum[(task4a[[i]] - avg)*(task4a[[i + k]] - avg), {i, 1, NN - k}];]

ListPlot[task5a, 
 PlotLabel -> "K estimator for AR(2) normal distribution", 
 Joined -> True, PlotRange -> All, AxesLabel -> {"k", "K(k)"}]

这篇关于原始对象 1 不能用作迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆