-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodigo_ayerim.R
135 lines (86 loc) · 2.88 KB
/
codigo_ayerim.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
options(digits = 9)
# 1. Metodo de la definicion
# funciones
Polbase=function(n,s,t){
L=0 # hay que ponerlo igual a 0 para hacerlo vector, aunque se use en un productorio
for(i in 1:n){
L[i]=1 #ahora si se pone a 1
for(j in 1:n){
if (i!=j){ # !=i significa que j no tomará los valores de i
L[i]= L[i]*( t - s [j])/ (s[i]- s[j])
}
}
}
return(L) #Return es el resultado
}
# funcion para determinar polinomio
Polinterp=function(B,L,n){
p=0
for (i in 1:n){
p=p + B[i]*L[i]
}
# return(p)
res <- list('Approximated value' = p) # , 'Neville iterations table'=q
return(res)
}
# B=c(10,20,35.33,35.5)
# s=c(1,3.5,5,10)
s <- c(0.0, 0.1, 0.3, 0.6, 1.0, 1.1)
B <- c(-6.00000, -5.89483, -5.65014, -5.17788, -4.28172, -3.99583) # El vector B, que contendrá los valores de la función interpolada
n=length(s)
# Punto 1
T1 = 0.41 #valor a interpolar
L1=Polbase(n,s,T1) # El vector L, que contendrá los polinomios de base evaluados en t
pol1=Polinterp(B,L1,n)
pol1
# 2. Metodo Neville
# https://rstudio-pubs-static.s3.amazonaws.com/286755_5d9464e5f87e4579b5afff23276f0381.html
poly.neville <- function(x, y, x0) {
n <- length(x)
q <- matrix(data = 0, n, n)
q[,1] <- y
for (i in 2:n) {
for (j in i:n) {
q[j,i] <- ((x0 - x[j-i+1]) * q[j,i-1] - (x0 - x[j]) * q[j-1,i-1]) / (x[j] - x[j-i+1])
}
}
res <- list('Approximated value'=q[n,n], 'Neville iterations table' = round(q, 5)) # ,
return(res)
}
poly.neville(s, B, 0.41)
# de newton
# devtools::install_github("FedericoComoglio/rSymPy")
divided.differences <- function(x, y, x0) {
require(rSymPy)
n <- length(x)
q <- matrix(data = 0, n, n)
q[,1] <- y
f <- as.character(round(q[1,1], 5))
fi <- ''
for (i in 2:n) {
for (j in i:n) {
q[j,i] <- (q[j,i-1] - q[j-1,i-1]) / (x[j] - x[j-i+1])
}
fi <- paste(fi, '*(x - ', x[i-1], ')', sep = '', collapse = '')
f <- paste(f, ' + ', round(q[i,i], 5), fi, sep = '', collapse = '')
}
x <- Var('x')
sympy(paste('e = ', f, collapse = '', sep = ''))
approx <- sympy(paste('e.subs(x, ', as.character(x0), ')', sep = '', collapse = ''))
# # 'Approximation from Interpolation'=as.numeric(approx),
return(list('Approximation from Interpolation'=as.numeric(approx),
'Interpolated Function'=f, 'Divided Differences Table'=q))
}
z1 <- Polinterp(B,L1,n)
z2 <- poly.neville(s, B, 0.41)
z3 <- divided.differences(s, B, 0.41)
y <- c(z1$`Approximated value`, z2$`Approximated value`, z3$`Approximation from Interpolation`)
dff <- data.frame(x = 0.41, y, color = c('a', 'b', 'c'))
#
# install.packages("ggplot2")
library(ggplot2)
df <- data.frame(y = B, x = s, color = 'black')
ggplot() +
geom_point(data = df, aes(x, y)) +
geom_point(data = dff, aes(x, y, color = color)) +
xlim(-1, 2)