Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- source("SPLINES.R")
- main <- function()
- {
- #X <- c(1,2,4,6,7)
- #Y <- c(2,4,1,3,3)
- X <- c(3,4.5,7,9)
- Y <- c(2.5,1,2.5,0.5)
- xis <- seq(from = 3.2, to = 8.7, length.out = 10000)
- #xis <- c(1.2,2.9,5.2,6.7)
- sp3 <- splines3(4,X,Y,xis,0)
- sp31 <- splines3(4,X,Y,xis,1)
- sp2 <- splines(4,X,Y,xis)
- new <- newton(X,Y,xis)
- lagg <- lagrange(X,Y,xis)
- #print(sp2)
- #print(sp3)
- plot(sp2,type = "o",col = "purple",pch = 20) #Desenha
- lines(sp3,type = "o",col = "red",pch = 20) #Desenha
- lines(new,type = "o",col = "green",pch = 20) #Desenha
- lines(lagg,type = "o",col = "blue",pch = 20) #Desenha
- lines(X,Y,type = "p",pch = 8)
- legend("top", xpd=TRUE, ncol=2, legend=c("SP", "SP3","Newton","LaGrange"),
- fill=c("purple", "red","green","blue"), bty="n")
- #natural vs not a knot:
- #plot(sp3,type = "o",col = "red") #Desenha
- #lines(sp31,type = "o",col = "blue") #Desenha
- #legend("top", xpd=TRUE, ncol=2, legend=c("Natural", "Not a Knot"),
- # fill=c("red", "blue"), bty="n")
- #-----------------------------
- #Newton vs Lagrange:
- #plot(new,type = "o",col = "green") #Desenha
- #lines(lagg,type = "o",col = "blue") #Desenha
- #legend("top", xpd=TRUE, ncol=2, legend=c("Newton", "Lagrange"),
- # fill=c("green", "blue"), bty="n")
- #-----------------------------
- # sp vs sp3
- #plot(sp2,type = "o",col = "green") #Desenha
- #lines(sp3,type = "o",col = "blue") #Desenha
- #legend("top", xpd=TRUE, ncol=2, legend=c("Splines 2", "Splines 3"),
- # fill=c("green", "blue"), bty="n")
- }
- newton <- function (X,Y,x)
- {
- n <- length(X)
- V <- Y #v[i] = y[i]
- for(k in 1:(n-1) ){
- for(i in seq(from = n, to = (k +1))){
- V[i] = (V[i] - V[i-1]) / (X[i] - X[i-k]) }}
- R <- array(dim = length(x)) #Criar array com as respostas
- for(j in 1:length(x)) #Percorre o array x e salva as respostas no array R
- {
- R[j] <- V[n]
- for(i in (n-1):1)
- {
- R[j] <- R[j] * (x[j] - X[i]) + V[i]
- }
- }
- X <- c(X, x) #Juntando os dados
- Y <- c(Y, R)
- dataF <- data.frame(X,Y)
- ord <- dataF[order(X) , ] #Ordenando pela abscissa
- #plot(ord,type = "b") #Desenha
- return(ord)
- }
- # x = c(1.1,1.4,1.9,2.1,2.5,3.0,3.2)
- # y = c(0.6942,0.6952,1.1759,1.6562,3.4325,8.0855,11.0925)
- #seq(from = -1, to = 1, length.out = 50)
- #f(x) = 1/ 1+25x^2
- fx <- function(limite1,limite2,qtd)
- {
- X <- seq(from = limite1, to = limite2, length.out = qtd)
- Y <- array(dim = qtd)
- for(i in 1:qtd)
- {
- Y[i] = 1 / ( 1 + (25 * (X[i]^2) ))
- }
- }
- lagrange <- function(X,Y,x)
- {
- n <- length(X)
- Px2 <- array(dim = length(x)) #Criar array com as respostas
- for(k in 1:length(x))
- {
- Px <- 0
- for(i in 1:n)
- {
- P <- 1
- for(j in 1:n)
- {
- if(j != i )
- {
- P <- P * ( (x[k] - X[j]) / (X[i] - X[j]) ) #prod
- }
- }
- Px <- (P * Y[i]) + Px #Somatorio
- }
- Px2[k] = Px
- }
- X <- c(X, x) #Juntando os dados
- Y <- c(Y, Px2)
- dataF <- data.frame(X,Y)
- ord <- dataF[order(X) , ] #Ordenando pela abscissa
- #plot(ord,type = "b") #Desenha
- return(ord)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement