蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

绘制正弦、断开的正切与标量分段函数

RWindows x64 R 4.6.2,基础图形设备。curves.R
curves.R44 行
emit <- function(key, values) {
  cat(key, "=", paste(values, collapse = ","), "\n", sep = "")
}
display <- function() {
  png("curve-observed.png",width=1050,height=720,res=100)
  on.exit(invisible(dev.off()),add=TRUE)
  par(mfrow=c(2,3),mar=c(4,4,3,1))
  base <- curve(sin(x),from=-2*pi,to=2*pi,n=81,xlab="Angle",ylab="Value",
                col="#35688F",main="Sine and cosine")
  extra <- curve(cos(x),add=TRUE,col="#B25773",lty=2)
  legend("topright",c("sin","cos"),col=c("#35688F","#B25773"),lty=c(1,2),bty="n",cex=.8)

  points <- seq(-pi,pi,length.out=130)
  tangent <- tan(points)
  group <- floor((points+pi/2)/pi)
  crossings <- sum(diff(group)!=0)
  plot(points,tangent,type="l",ylim=c(-5,5),col="#B25773",main="Across poles")
  abline(v=c(-pi/2,pi/2),col="#777777",lty=2)
  plot(points,tangent,type="n",ylim=c(-5,5),main="Separate branches")
  for (index in split(seq_along(points),group))
    lines(points[index],tangent[index],col="#35688F",lwd=2)
  abline(v=c(-pi/2,pi/2),col="#777777",lty=2)

  scalar <- function(z) if(z>=0) z else z^2
  grid <- seq(-2,2,length.out=9)
  result <- vapply(grid,scalar,numeric(1))
  failed <- tryCatch({scalar(grid);FALSE},error=function(e) TRUE)
  plot(grid,result,type="b",col="#35688F",main="Scalar function")

  log_result <- curve(sqrt(angle),xname="angle",xlim=c(1,100),n=3,
                      log="x",type="p",col="#B25773",main="Logarithmic sampling")
  response <- c(12,17,9,23,18)
  plot(response,type="o",col="#B25773",pch=16,xlab="Week",ylab="Visits",main="Observed series")

  emit("sample",c(length(base$x),round(base$x[c(1,81)],5),round(base$y[41],5),
                  length(extra$x),round(extra$y[51],5)))
  emit("breaks",c(crossings,as.integer(table(group))))
  emit("scalar",c(failed,result[c(1,5,9)]))
  emit("log",c(round(log_result$x,4),round(log_result$y,4)))
  emit("series",c(seq_along(response),response))
}
display()
emit("device",is.null(dev.list()))
本例需在 Windows x64 R 4.6.2,基础图形设备。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64 R 4.6.2,基础图形设备。

入口文件:projects/r-curves-scatter-java/curves.R

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 curves.R,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64 R 4.6.2,基础图形设备。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:R

课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a317.zip

下载课程关联工程包:R

课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a319.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习