2.3R编程
x<-runif(50,0,2)
hist.with.normal<-function(x,xlab=deparse(substitute(x)),...)
{h<-hist(x,plot=F,...)
s<-sd(x)
m<-mean(x)
ylim<-range(0,h$density,dnorm(0,sd=s))
hist(x,fre=F,ylim = ylim,xlab=xlab,...)
curve(dnorm(x,m,s),add=T)}
hist.with.normal(x)
2.3.1流程控制
y<-12345
x<-y/2
while(abs(x*x-y)>1e-10)x<-(x+y/x)/2#前面的条件是真,则对后面表达式求值,abs为绝对值函数
x
[1] 111.1081
x^2
[1] 12345
x<-y/2
repeat{
x<-(x+y/x)/2
if(abs(x*x-y)<1e-1