私のコードは次のとおりです。
m<-c(9,17,33,65,129,257,513)
results<-matrix(,7,5)
results[,1]<-m
#methods
trap<-function(a,b,m,func)
{
h=(b-a)/(m-1)
x<-seq(a,b,h)
y<-function(x) {
z<-eval(parse(text=func))
return(z)
}
result<-h*(0.5* y(x[1]) + sum(y(x[2:(length(x)-1)]))+ 0.5*y(x[length(x)]) )
result
}
次のコマンドを実行するとtrap(0,5,results[,1],"x^2")
、意図した出力が得られますが、厄介な警告メッセージも表示されます。
Warning messages:
1: In if (n < 0L) stop("wrong sign in 'by'
argument") : the condition has length > 1 and only the first element
will be used
2: In if (n > .Machine$integer.max) stop("'by' argument
is much too small") : the condition has length > 1 and only the
first element will be used
3: In 0L:n : numerical expression has 7
elements: only the first used
4: In (0L:n) * by : longer object
length is not a multiple of shorter object length
5: In if (by > 0)
pmin(x, to) else pmax(x, to) : the condition has length > 1 and only
the first element will be used
だから私は何が起こっているのかを理解しようと試みました.すべてがこれを指しているようx<-seq(a,b,h)
です. .
誰かがこのメッセージを理解するのを手伝ってくれるので、警告されていることを修正できますか?