This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
mf1<-function(a,b,eps=0.0005){
f<-function(i){
i^2-3
}
repeat{abs(b-a)->ab
if(ab<eps){
break
}else{
if(f(a)*f(b)==0){
list("f(a)",f(a),"f(b)",f(b))
}else{
if(f(a)*f(b)>0){
return("无解")
}else{
c<-(a+b)/2
print(c)
if(f(c)==0){
return(c)
}else{
if(f(a)*f(c)>0){
c->a
}else{
c->b
}
}
}
}
}
}
list(a=a,"f(a)"=f(a),b=b,"f(b)"=f(b),"a-b"=a-b,root=(a+b)/2,"f(root)"=f((a+b)/2))
}
mf1(1,6,eps=10^(-10))
## [1] 3.5
## [1] 2.25
## [1] 1.625
## [1] 1.9375
## [1] 1.78125
## [1] 1.703125
## [1] 1.742188
## [1] 1.722656
## [1] 1.732422
## [1] 1.727539
## [1] 1.72998
## [1] 1.731201
## [1] 1.731812
## [1] 1.732117
## [1] 1.731964
## [1] 1.73204
## [1] 1.732079
## [1] 1.732059
## [1] 1.73205
## [1] 1.732055
## [1] 1.732052
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## [1] 1.732051
## $a
## [1] 1.732051
##
## $`f(a)`
## [1] -1.509637e-11
##
## $b
## [1] 1.732051
##
## $`f(b)`
## [1] 2.3695e-10
##
## $`a-b`
## [1] -7.275958e-11
##
## $root
## [1] 1.732051
##
## $`f(root)`
## [1] 1.109268e-10
#来源
fzero<-function(f,a,b,eps=1e-5){
if(f(a)*f(b)>0){
list(fail="finding root is fail!")
}else{
repeat{
if(abs(b-a)<eps){
break
}else{
x<-(a+b)/2
if(f(a)*f(x)<0) b<-x
else a<-x}}
list(root=(a+b)/2,fun=f(x))}}
f<-function(x) x^3-x-1
fzero(f,1,2,1e-6)
## $root
## [1] 1.324718
##
## $fun
## [1] -1.857576e-06
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.