Advertisement
thiagolamf

newton+lagrange

Aug 31st, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.01 KB | None | 0 0
  1. source("SPLINES.R")
  2.  
  3. main <- function()
  4. {
  5.     #X <- c(1,2,4,6,7)
  6.     #Y <- c(2,4,1,3,3)
  7.     X <- c(3,4.5,7,9)
  8.     Y <- c(2.5,1,2.5,0.5)
  9.     xis <- seq(from = 3.2, to = 8.7, length.out = 10000)
  10.     #xis <- c(1.2,2.9,5.2,6.7)
  11.    
  12.     sp3 <- splines3(4,X,Y,xis,0)
  13.     sp31 <- splines3(4,X,Y,xis,1)
  14.     sp2 <- splines(4,X,Y,xis)
  15.    
  16.     new <- newton(X,Y,xis)
  17.     lagg <- lagrange(X,Y,xis)
  18.    
  19.     #print(sp2)
  20.     #print(sp3)
  21.    
  22.     plot(sp2,type = "o",col = "purple",pch = 20) #Desenha
  23.     lines(sp3,type = "o",col = "red",pch = 20) #Desenha
  24.     lines(new,type = "o",col = "green",pch = 20) #Desenha
  25.     lines(lagg,type = "o",col = "blue",pch = 20) #Desenha
  26.     lines(X,Y,type = "p",pch = 8)
  27.     legend("top", xpd=TRUE, ncol=2, legend=c("SP", "SP3","Newton","LaGrange"),
  28.        fill=c("purple", "red","green","blue"), bty="n")
  29.    
  30.     #natural vs not a knot:
  31.     #plot(sp3,type = "o",col = "red") #Desenha
  32.     #lines(sp31,type = "o",col = "blue") #Desenha
  33.     #legend("top", xpd=TRUE, ncol=2, legend=c("Natural", "Not a Knot"),
  34.     #   fill=c("red", "blue"), bty="n")
  35.     #-----------------------------
  36.     #Newton vs Lagrange:
  37.     #plot(new,type = "o",col = "green") #Desenha
  38.     #lines(lagg,type = "o",col = "blue") #Desenha
  39.     #legend("top", xpd=TRUE, ncol=2, legend=c("Newton", "Lagrange"),
  40.     #   fill=c("green", "blue"), bty="n")
  41.     #-----------------------------
  42.     # sp vs sp3
  43.     #plot(sp2,type = "o",col = "green") #Desenha
  44.     #lines(sp3,type = "o",col = "blue") #Desenha
  45.     #legend("top", xpd=TRUE, ncol=2, legend=c("Splines 2", "Splines 3"),
  46.     #   fill=c("green", "blue"), bty="n")
  47.    
  48. }
  49.  
  50. newton <- function (X,Y,x)
  51. {
  52.     n <- length(X)
  53.     V <- Y  #v[i] = y[i]
  54.    
  55.     for(k in 1:(n-1) ){
  56.         for(i in seq(from = n, to = (k +1))){
  57.             V[i] = (V[i] - V[i-1]) / (X[i] - X[i-k]) }}
  58.    
  59.    
  60.     R <- array(dim = length(x)) #Criar array com as respostas
  61.    
  62.     for(j in 1:length(x)) #Percorre o array x e salva as respostas no array R
  63.     {
  64.         R[j] <- V[n]
  65.         for(i in (n-1):1)
  66.         {
  67.             R[j] <- R[j] * (x[j] - X[i]) + V[i]
  68.         }
  69.     }
  70.  
  71.     X <- c(X, x) #Juntando os dados
  72.     Y <- c(Y, R)
  73.    
  74.     dataF <- data.frame(X,Y)
  75.     ord <- dataF[order(X) , ] #Ordenando pela abscissa
  76.    
  77.     #plot(ord,type = "b") #Desenha
  78.    
  79.     return(ord)
  80.    
  81. }
  82.  
  83. # x = c(1.1,1.4,1.9,2.1,2.5,3.0,3.2)
  84. # y = c(0.6942,0.6952,1.1759,1.6562,3.4325,8.0855,11.0925)
  85. #seq(from = -1, to = 1, length.out = 50)
  86.  
  87. #f(x) = 1/ 1+25x^2
  88. fx <- function(limite1,limite2,qtd)
  89. {
  90.     X <- seq(from = limite1, to = limite2, length.out = qtd)
  91.     Y <- array(dim = qtd)
  92.     for(i in 1:qtd)
  93.     {
  94.         Y[i] = 1 / ( 1 + (25 * (X[i]^2)  ))
  95.     }
  96. }
  97.  
  98. lagrange <- function(X,Y,x)
  99. {
  100.     n <- length(X)
  101.    
  102.     Px2 <- array(dim = length(x)) #Criar array com as respostas
  103.    
  104.     for(k in 1:length(x))
  105.     {
  106.         Px <- 0
  107.         for(i in 1:n)
  108.         {
  109.             P <- 1
  110.             for(j in 1:n)
  111.             {
  112.                 if(j != i )
  113.                 {
  114.                     P <- P * ( (x[k] - X[j]) / (X[i] - X[j]) )  #prod
  115.                 }
  116.             }
  117.             Px <- (P * Y[i]) + Px #Somatorio
  118.         }
  119.         Px2[k] = Px
  120.     }
  121.    
  122.     X <- c(X, x) #Juntando os dados
  123.     Y <- c(Y, Px2)
  124.    
  125.     dataF <- data.frame(X,Y)
  126.     ord <- dataF[order(X) , ] #Ordenando pela abscissa
  127.    
  128.     #plot(ord,type = "b") #Desenha
  129.    
  130.     return(ord)
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement