Guest User

Untitled

a guest
Jan 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. # test polynomial interpolation code by interpolating points on a semi-circle
  2. # using a different number of points each time.
  3.  
  4. # contents:
  5. # 1) make.circle(degree) is a function that generates
  6. # n=degree+1 data points (x,y) on a semicircle. this is the "data" we
  7. # want to interpolate.
  8. # 2) pinterp() makes polynomial interpolation on "data" of degree "degree"
  9. # using degree+1 parameters
  10. # 3) run() is a wrapper that runs the interpolation
  11.  
  12. # note: polynomial interpolation uses n=degree+1 points and T=degree+1 parameters.
  13. #
  14. # output:
  15. # plot of the interpolation overlaid by the used data points
  16.  
  17. rm(list=ls(all=TRUE))
  18. setwd("~/dropbox/Rstuff/splines")
  19.  
  20. require(plotrix)
  21. # this package contains a function to draw a circle.
  22. # you could generate any other data as well.
  23.  
  24. # task: run this approximation for 3,7,11 and 15 sample points.
  25. degrees <- c(2,6,10,14)
  26.  
  27.  
  28. ##################
  29. # define functions
  30. ##################
  31. make.circle <- function(degree){
  32. # makes a vector of degree+1 points on a semi-circle
  33. plot(-2:2,-2:2,type="n",xlab="",ylab="",main="make.circle output. disregard.")
  34. vals <- draw.circle(0,0,radius=1,nv=2*(degree)+1)
  35. data <- array(unlist(vals),dim=c(2*degree+1,2))
  36. data <- data[data[ ,2]>=0, ] # take only upper half of circle§
  37. return(data)
  38. }
  39.  
  40. pinterp <- function(degree,params,data,t){
  41. # polynomial interpolation on data at point t
  42.  
  43. # has a list "x" whose entries are each a
  44. # matrix for points at t at approximation of degree
  45. # deg. x gets shorter with increasing degree. at final entry
  46. # degree+1, x is (1,2)
  47. x <- vector("list",degree+1)
  48. x[[1]] <- data # matrix 1 holds data for degree zero
  49. # each row of x[[j]]
  50. # represents (x,y) coords of a point
  51. n <- degree+1 # number of points we're interpolating on
  52. for (ideg in 2:n){ # recursively build interpolations of increasing degree
  53. nn <- n-ideg+1
  54. x[[ideg]] <- array(data=NA,dim=c(nn,2))
  55. for (i in 1:nn){
  56. x[[ideg]][i, ] <-
  57. (params[i+ideg-1] - t)/(params[i+ideg-1] - params[i])*x[[ideg-1]][i, ] +
  58. (t - params[i])/(params[i+ideg-1] - params[i])*x[[ideg-1]][i+1, ]
  59. }
  60. }
  61. return(x[[degree+1]]) # return only final degree values,i.e. result
  62. }
  63.  
  64. run <- function(degrees,t){
  65. nd <- length(degrees)
  66. rval <- vector("list",nd)
  67. origdata <- vector("list",nd)
  68. for (j in 1:nd){
  69. degree <- degrees[j]
  70. params <- seq(0,5,length=degree+1)
  71. nt <- length(t)
  72. rval[[j]] <- array(data=NA,dim=c(nt,2))
  73. data <- make.circle(degree)
  74. origdata[[j]] <- data
  75. for (it in 1:nt) rval[[j]][it, ] <- pinterp(degree,params,data,t[it])
  76. }
  77. return(list(interpol=rval,orig=origdata))
  78. }
  79.  
  80. ###################
  81. # produce output
  82. ###################
  83.  
  84. outrun <- run(degrees,t=seq(1,5,length=20))
  85. rval <- outrun$interpol
  86. orig <- outrun$orig
  87. png(file="graphs/ex1.png")
  88. par(mfrow=c(2,2))
  89. for (j in 1:length(degrees)){
  90. plot(x=orig[[j]][ ,1],y=orig[[j]][ ,2],type="o",
  91. main=paste("degree: ",degrees[j],", points: ",
  92. degrees[j]+1),xlab="x",ylab="y",ylim=c(0,1.5))
  93. lines(x=rval[[j]][ ,1],y=rval[[j]][ ,2],col="red")
  94. }
  95. dev.off()
  96. par(mfrow=c(1,1))
Add Comment
Please, Sign In to add comment