前面文章中介绍了使用plot函数绘制图形的方法。本文介绍一下使用R中curve函数绘制常见函数曲线的方法。
1、curve函数简介
curve函数语法格式如下:
curve(expr, from = NULL, to = NULL, n = 101, add = FALSE, type = "l", xname = "x", xlab = xname, ylab = NULL, log = NULL, xlim = NULL, ...)
主要的参数如下:
expr:函数名称或一个关于变量x的函数表达式;
from,to:表示绘图的起止范围;
n:一个整数值,表示x取值的数量;
add:是一个逻辑值,当为TRUE时,表示将绘图添加到已存在的绘图中;
type:与plot函数中type含义相同;
xname:用于x轴变量的名称。
xlab,ylab:x轴和y轴的标签名称。
2、绘制y=x的直线
#y=x
curve(x+0, -10, 10, bty="l", xlab="x1", ylab="y")
abline(h=0, v=0, lty=2, col="gray")
text(0.5, -0.5, "0", col="gray")
text(8, 10, "y = x")
图形如下:
3、绘制 y = 2x + 3 的直线
# y= 2x + 3
curve(2*x + 3, -10, 10, bty="l", xlab = "x", ylab = "y", asp = 1)
abline(h=0, v=0, lty=2, col = "darkgray")
text(0.8, -0.8, "0", col = "darkgray")
text(15, 20, "y = 2x + 3")
图形如下:
3、绘制形如y = ax2 + bx + c的图像
#y=x^2
curve(x^2,-5,5,bty="l",xlab="x",ylab="y",asp=1,ylim=c(-5,25))
abline(h=0,v=0,lty=2,col="darkgray")
text(0.8,-0.8,"0",col="darkgray")
text(7,23,expression(paste("y = ",x^2)))
#y=x^2+3x+1
curve(x^2+3*x+1,-10,5,col="blue",add=T)
text(8,25.5,expression(paste("y = ",x^2," + 3x + 1")),col="blue")
abline(v= -b/(2*a),col="lightblue",lty=2)
本段代码在同一幅图中绘制了两个函数图像,分别是:y = x2和y = x2 + 3x + 1,其图像如下图所示:
4、幂函数图像
下面的程序代码给出的是形如y = xα的图像。
# y = x
curve(x^1,-1.5,2,bty="l",xlab="x",ylab="y",asp=1,ylim=c(-1,1))
abline(h=0,v=0,lty=2,col="darkgray")
text(0.1,-0.1,"0",col="darkgray")
lines(c(0,1,1),c(1,1,0),lty=2,col="red")
text(1.1,0.95,"1",col="red")
#y = x ^ 2
curve(x^2,-1.5,2,add=T,col="blue")
#y = x ^ 3
curve(x^3,-1.5,2,add=T,col="orange")
#y = x ^ (1/2)
curve(x^(1/2), 0,2,add=T,col="green")
#y = x ^ (1/3)
curve(x^(1/3), 0,2,add=T,col="darkgreen")
图像如下图所示:
5、指数函数
下面程序代码绘制的是形如 y = ax的函数图像。
curve(2^x,-3,3,bty="l",xlab="x",ylab="y",asp=1,ylim=c(-0.5,8))
abline(h=0,v=0,lty=2,col="darkgray")
text(0.2,-0.15,"0",col="darkgray")
curve(4^x,-3,3,bty="l",add=T,col="blue")
curve((1/2)^x,-3,3,bty="l",add=T,col="red")
curve((1/4)^x,-3,3,bty="l",add=T,col="green")
curve(exp,-3,3,bty="l",add=T,col="orange")
具体如下图所示:
5、对数函数图像
绘制形如 y = loga(x)的函数图像
curve(log(x,2),0,10,bty="l",xlab="x",ylab="y",asp=1,ylim=c(-3,5))
abline(h=0,v=0,lty=2,col="darkgray")
text(0.15,-0.15,"0",col="darkgray")
curve(log(x),0,10,bty="l",add=T,col="orange")
curve(log(x,10),0,10,bty="l",add=T,col="blue")
curve(log(x,0.5),0,10,bty="l",add=T,col="green")
curve(log(x,0.1),0,10,bty="l",add=T,col="red")
图像如下图所示:
其中,橘黄色的曲线是y = ln(x)的函数图像。
6、三角函数图像
(1)正弦与余弦函数
#sin(x)
curve(sin(x),-10,10,bty="l",xlab="x",ylab="y",asp=1)
#cons(x)
curve(cos,-10,10,add=T,col="blue")
#下面是绘制坐标轴和图中红色的线及标签
abline(h=0,v=0,lty=2,col="darkgray")
text(0.5,-0.5,"0",col="darkgray")
lines(c(-pi,-pi),c(1.5,-1.5),col="red",lty=3)
text(-pi,2.5,expression(-pi),col="darkgray")
lines(c(-pi/2,-pi/2),c(1.5,-1.5),col="red",lty=3)
text(-pi/2,2.5,expression(-frac(pi,2)),col="darkgray")
lines(c(pi/2,pi/2),c(1.5,-1.5),col="red",lty=3)
text(pi/2,2.5,expression(frac(pi,2)),col="darkgray")
lines(c(pi,pi),c(1.5,-1.5),col="red",lty=3)
text(pi,2.5,expression(pi),col="darkgray")
如下图:
(2)正切函数
curve(tan,-2*pi,2*pi,col="red",ylim=c(-6,6),asp=1)
abline(h=0,v=c(-pi,-pi/2,0,pi/2,pi),lty=2,col="darkgray")
text(c(-pi+0.5,-pi/2+0.5,0.5,pi/2+0.5,pi+0.5),-0.5,
c("-π","-π/2",0,"π/2","π"),col="darkgray")
函数图像如下图:
(3)余切函数
在R的默认基础库中没有提供余切函数,但是可以根据余切与正切的关系来绘制。余切等于正切的倒数。
下面的代码是绘制余切函数:
curve(1/tan(x),-2*pi,2*pi,col="blue",ylim=c(-6,6),asp=1)
abline(h=0,v=c(-pi,-pi/2,0,pi/2,pi),lty=2,col="darkgray")
text(c(-pi+0.5,-pi/2+0.5,0.5,pi/2+0.5,pi+0.5),0.5, c("-π","-π/2",0,"π/2","π"),col="darkgray")
函数图像如下图所示:
新闻热点
疑难解答