#我把m设为5,方便确认
#你再视需要调整
m=5
n<-100
Z<-m
for (i in 1:m){
u1=runif(n)
u2=runif(n)
R=sqrt(-2*log(u2))
theta=2*pi*u1
z=R*cos(theta)
Z[i]=sum(z)
s<- 100+Z*sqrt(100000)
}
Z
> Z
[1] 3.951455 -14.365024 -19.291418 2.689245 16.993109
s
[1] 1349.560 -4442.619 -6000.482 950.414 5473.693
h=s
p=length(s)
q=length(h)
X=c(s,rep(0,q))
H=c(h,rep(0,p))
Y=rep(0,q+p-1)
for (i in 1:(q+p-1)){
Y[i]=0
for (j in 1:p){
if(i-j+1>0) Y[i]=Y[i]+X[j]*H[i-j+1]}
}
Y
> Y
[1] 1821312 -11991162 3540843 55880992 42335284 -60040949 -64786306
[8] 10404549 29961315
%%%%%%%
结果与matlab相同
ans =
1.0e+007 *
Columns 1 through 8
0.1821 -1.1991 0.3541 5.5881 4.2335 -6.0041 -6.4786 1.0405
Column 9
2.9961