R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # splint.r cubic spline interpolation > # > npts = 100 # points for plotting > n = 7 # how many points for interpolation > xi <- 4*(2*(1:n)-n-1)/(n-1) # equally spaced interpolation pts > z <- 1/(1+xi*xi) # function values > szx <-splinefun(xi,z,method="natural")# interp cubic spline function > # now get ready to plot > x <- 4*(2*(1:npts)-npts-1)/(npts-1) > zx <- 1/(1+x*x) # true function > s <- szx(x,deriv=0) # interpolated values > sp <- szx(x,deriv=1) # deriv of spline > cbind(x,zx,s,sp) x zx s sp [1,] -4.00000000 0.05882353 0.05882353 0.05529536 [2,] -3.91919192 0.06112445 0.06328978 0.05521880 [3,] -3.83838384 0.06355990 0.06774366 0.05498914 [4,] -3.75757576 0.06614030 0.07217279 0.05460637 [5,] -3.67676768 0.06887707 0.07656480 0.05407050 [6,] -3.59595960 0.07178274 0.08090733 0.05338151 [7,] -3.51515152 0.07487109 0.08518800 0.05253942 [8,] -3.43434343 0.07815727 0.08939443 0.05154422 [9,] -3.35353535 0.08165799 0.09351425 0.05039592 [10,] -3.27272727 0.08539167 0.09753510 0.04909451 [11,] -3.19191919 0.08937870 0.10144459 0.04763998 [12,] -3.11111111 0.09364162 0.10523037 0.04603236 [13,] -3.03030303 0.09820543 0.10888004 0.04427162 [14,] -2.94949495 0.10309788 0.11238125 0.04235778 [15,] -2.86868687 0.10834982 0.11572162 0.04029083 [16,] -2.78787879 0.11399560 0.11888877 0.03807077 [17,] -2.70707071 0.12007351 0.12187035 0.03569760 [18,] -2.62626263 0.12662627 0.12466030 0.03364204 [19,] -2.54545455 0.13370166 0.12739841 0.03472839 [20,] -2.46464646 0.14135310 0.13037028 0.03942734 [21,] -2.38383838 0.14964044 0.13386782 0.04773891 [22,] -2.30303030 0.15863074 0.13818297 0.05966310 [23,] -2.22222222 0.16839917 0.14360765 0.07519990 [24,] -2.14141414 0.17903005 0.15043380 0.09434932 [25,] -2.06060606 0.19061789 0.15895333 0.11711135 [26,] -1.97979798 0.20326856 0.16945819 0.14348599 [27,] -1.89898990 0.21710045 0.18224030 0.17347325 [28,] -1.81818182 0.23224568 0.19759158 0.20707313 [29,] -1.73737374 0.24885109 0.21580397 0.24428562 [30,] -1.65656566 0.26707905 0.23716940 0.28511072 [31,] -1.57575758 0.28710783 0.26197978 0.32954844 [32,] -1.49494949 0.30913105 0.29052706 0.37759877 [33,] -1.41414141 0.33335601 0.32310316 0.42926172 [34,] -1.33333333 0.36000000 0.36000000 0.48453728 [35,] -1.25252525 0.38928387 0.40131974 0.53637990 [36,] -1.17171717 0.42142151 0.44640541 0.57774399 [37,] -1.09090909 0.45660377 0.49441026 0.60862957 [38,] -1.01010101 0.49497500 0.54448754 0.62903663 [39,] -0.92929293 0.53660005 0.59579049 0.63896517 [40,] -0.84848485 0.58142018 0.64747238 0.63841520 [41,] -0.76767677 0.62919689 0.69868646 0.62738671 [42,] -0.68686869 0.67944541 0.74858597 0.60587970 [43,] -0.60606061 0.73136333 0.79632416 0.57389417 [44,] -0.52525253 0.78376649 0.84105429 0.53143013 [45,] -0.44444444 0.83505155 0.88192961 0.47848757 [46,] -0.36363636 0.88321168 0.91810337 0.41506650 [47,] -0.28282828 0.92593292 0.94872881 0.34116690 [48,] -0.20202020 0.96078816 0.97295920 0.25678879 [49,] -0.12121212 0.98552036 0.98994778 0.16193216 [50,] -0.04040404 0.99837017 0.99884781 0.05659702 [51,] 0.04040404 0.99837017 0.99884781 -0.05659702 [52,] 0.12121212 0.98552036 0.98994778 -0.16193216 [53,] 0.20202020 0.96078816 0.97295920 -0.25678879 [54,] 0.28282828 0.92593292 0.94872881 -0.34116690 [55,] 0.36363636 0.88321168 0.91810337 -0.41506650 [56,] 0.44444444 0.83505155 0.88192961 -0.47848757 [57,] 0.52525253 0.78376649 0.84105429 -0.53143013 [58,] 0.60606061 0.73136333 0.79632416 -0.57389417 [59,] 0.68686869 0.67944541 0.74858597 -0.60587970 [60,] 0.76767677 0.62919689 0.69868646 -0.62738671 [61,] 0.84848485 0.58142018 0.64747238 -0.63841520 [62,] 0.92929293 0.53660005 0.59579049 -0.63896517 [63,] 1.01010101 0.49497500 0.54448754 -0.62903663 [64,] 1.09090909 0.45660377 0.49441026 -0.60862957 [65,] 1.17171717 0.42142151 0.44640541 -0.57774399 [66,] 1.25252525 0.38928387 0.40131974 -0.53637990 [67,] 1.33333333 0.36000000 0.36000000 -0.48453728 [68,] 1.41414141 0.33335601 0.32310316 -0.42926172 [69,] 1.49494949 0.30913105 0.29052706 -0.37759877 [70,] 1.57575758 0.28710783 0.26197978 -0.32954844 [71,] 1.65656566 0.26707905 0.23716940 -0.28511072 [72,] 1.73737374 0.24885109 0.21580397 -0.24428562 [73,] 1.81818182 0.23224568 0.19759158 -0.20707313 [74,] 1.89898990 0.21710045 0.18224030 -0.17347325 [75,] 1.97979798 0.20326856 0.16945819 -0.14348599 [76,] 2.06060606 0.19061789 0.15895333 -0.11711135 [77,] 2.14141414 0.17903005 0.15043380 -0.09434932 [78,] 2.22222222 0.16839917 0.14360765 -0.07519990 [79,] 2.30303030 0.15863074 0.13818297 -0.05966310 [80,] 2.38383838 0.14964044 0.13386782 -0.04773891 [81,] 2.46464646 0.14135310 0.13037028 -0.03942734 [82,] 2.54545455 0.13370166 0.12739841 -0.03472839 [83,] 2.62626263 0.12662627 0.12466030 -0.03364204 [84,] 2.70707071 0.12007351 0.12187035 -0.03569760 [85,] 2.78787879 0.11399560 0.11888877 -0.03807077 [86,] 2.86868687 0.10834982 0.11572162 -0.04029083 [87,] 2.94949495 0.10309788 0.11238125 -0.04235778 [88,] 3.03030303 0.09820543 0.10888004 -0.04427162 [89,] 3.11111111 0.09364162 0.10523037 -0.04603236 [90,] 3.19191919 0.08937870 0.10144459 -0.04763998 [91,] 3.27272727 0.08539167 0.09753510 -0.04909451 [92,] 3.35353535 0.08165799 0.09351425 -0.05039592 [93,] 3.43434343 0.07815727 0.08939443 -0.05154422 [94,] 3.51515152 0.07487109 0.08518800 -0.05253942 [95,] 3.59595960 0.07178274 0.08090733 -0.05338151 [96,] 3.67676768 0.06887707 0.07656480 -0.05407050 [97,] 3.75757576 0.06614030 0.07217279 -0.05460637 [98,] 3.83838384 0.06355990 0.06774366 -0.05498914 [99,] 3.91919192 0.06112445 0.06328978 -0.05521880 [100,] 4.00000000 0.05882353 0.05882353 -0.05529536 > plot(c(x,x),c(zx,s)) # Figure 7.3 > # clean up & go > rm(list=ls()) > q()