Finding RuntimeWarning: overflow encountered in double_scalars while running numerical schemes for fluid dynamic study - runtime-error

i'm writing a code to solve Hyperbolic differential equations with different numerica methods such as Lax-Friederichs, Lax-Wendroff and Upwind scheme. During the calculation i often obtain this type of error:
RuntimeWarning: overflow encountered in double_scalars
that seems to disappear when i reduce the dimensions of matrix. Here i attach my code:
for i in range (0,nt):
#inlet
rho[0,i] = P_inlet/(R*T_inlet)
u[0,i] = u_inlet
P[0,i] = P_inlet
T[0,i] = T_inlet
Ac[0,0] = A_var_list[0]
Q1[0,i] = rho[0,i]
Q2[0,i] = rho[0,i] * u[0,i]
Q3[0,i] = (1/2)*(rho[0,i])*(u[0,i]**2) + (P[0,i]/(k-1))
F1[0,i] = rho[0,i] * u[0,i]
F2[0,i] = (1/2)*(rho[0,i])*(u[0,i]**2) + P[0,i]
F3[0,i] = u[0,i] * ((1/2)*(rho[0,i])*(u[0,i]**2) + (k*P[0,i]/(k-1)))
#outlet
rho[nx-1,i] = rho_outlet
P[nx-1,i] = P_outlet
u[nx-1,i] = u_outlet
T[nx-1,i] = T_outlet
Q1[nx-1,i] = rho[nx-1,i]
Q2[nx-1,i] = rho[nx-1,i]*u[nx-1,i]
Q3[nx-1,i] = (1/2)*rho[nx-1,i]*u[nx-1,i] + (P[nx-1,i]/(k-1))
F1[nx-1,i] = rho[nx-1,i] * u[nx-1,i]
F2[nx-1,i] = (1/2)*rho[nx-1,i]*(u[nx-1,i]**2) + P[nx-1,i]
F3[nx-1,i] = u[nx-1,i] * ((1/2)*(rho[nx-1,i])*(u[nx-1,i]**2) + (k*P[nx-1,i]/(k-1)))
#manifold
for i in range (1,nx-1):
rho[i,0] = P_inlet/(R*Tw[i])
u[i,0] = u_inlet
P[i,0] = P_inlet
Ac[i,0] = A_var_list[i]
Q1[i,0] = rho[i,0]
Q2[i,0] = rho[i,0] * u[i,0]
Q3[i,0] = (1 / 2) * (rho[i,0]) * (u[i,0] ** 2) + (P[i,0] / (k - 1))
F1[i, 0] = rho[i, 0] * u[i, 0]
F2[i, 0] = (1 / 2) * (rho[i, 0]) * (u[i, 0] ** 2) + P[i, 0]
F3[i, 0] = u[i, 0] * ((1 / 2) * (rho[i, 0]) * (u[i, 0] ** 2) + (k * P[i, 0] / (k - 1)))
S1[i, 0] = -rho[i, 0] * u[i, 0] * (Ac[i, 0] - Ac[i - 1, 0])
S2[i, 0] = -(rho[i, 0] * ((u[i, 0] ** 2) / (Ac[i, 0])) * (Ac[i, 0] - Ac[i - 1, 0])) - (
(frict_fact * np.pi * rho[i, 0] * d[i] * u[i, 0] ** 2) / (2 * Ac[i, 0]))
S3[i, 0] = - (u[i, 0] * (rho[i, 0] * ((u[i, 0] ** 2) / 2) + (k * P[i, 0] / (k - 1))) * (
(Ac[i, 0] - Ac[i - 1, 0]) / Ac[i, 0])) + (Lambda * np.pi * d[i] * (Tw[i] - T[i, 0]) / Ac[i, 0])
def Upwind():
for n in range (0,nt-1):
for i in range (1,nx):
Q1[i,n+1] = Q1[i-1,n]-((F1[i,n] - F1[i-1,n])/Dx)*Dt + (S1[i,n]-S1[i-1,n])*Dt
Q2[i, n + 1] = Q2[i-1, n] - ((F2[i, n] - F2[i - 1, n]) / Dx) * Dt + (S2[i, n] - S2[i - 1, n]) * Dt
Q3[i, n + 1] = Q3[i-1, n] - ((F3[i, n] - F3[i - 1, n]) / Dx) * Dt + (S3[i, n] - S3[i - 1, n]) * Dt
rho[i, n+1] = Q1[i, n+1]
u[i, n+1] = Q2[i, n+1] / rho[i, n+1]
P[i, n+1] = (Q3[i, n+1] - 0.5 * rho[i, n+1] * u[i, n+1] ** 2) * (k - 1)
T[i, n+1] = P[i, n+1] / (R * rho[i, n+1])
F1[i,n+1] = Q2[i,n+1]
F2[i,n+1] = rho[i,n+1]*((u[i,n+1]**2)/2) +P[i,n+1]
F3[i, n + 1] = u[i, n + 1] * (
(rho[i, n + 1] * ((u[i, n + 1] ** 2) / 2)) + (k * P[i , n + 1] / (k - 1)))
S1[i, n + 1] = -rho[i, n + 1] * u[i, n + 1] * (Ac[i, 0] - Ac[i-1, 0])
S2[i, n + 1] = - (rho[i, n + 1] * (
(u[i, n + 1] ** 2) / (Ac[i, 0])) * (Ac[i, 0] - Ac[i-1, 0])) - ((
(frict_fact * np.pi * rho[i, n + 1] * d[i] * (u[i, n + 1] ** 2)) / (2 * Ac[i, 0])))
S3[i, n + 1] = -(u[i, n + 1] * (
rho[i, n + 1] * ((u[i, n + 1] ** 2) / 2) + (k * P[i, n + 1] / (k - 1))) * (
(Ac[i , 0] - Ac[i-1, 0]) / Ac[i, 0])) + (
Lambda * np.pi * d[i ] * (Tw[i] - T[i, 0]) / Ac[i, 0])
plt.figure(1)
plt.plot(P[:, nt - 1])
plt.figure(2)
plt.plot(u[:, nt - 1])
def Lax_Friedrichs():
for n in range (1,nt):
for i in range (1,nx-1):
F1_m1 = 0.5 * (F1[i, n - 1] + F1[i - 1, n - 1])
F2_m1 = 0.5 * (F2[i, n - 1] + F2[i - 1, n - 1])
F3_m1 = 0.5 * (F3[i, n - 1] + F3[i - 1, n - 1])
S1_m1 = 0.5 * (S1[i, 0] + S1[i - 1, 0])
S2_m1 = 0.5 * (S2[i, 0] + S2[i - 1, 0])
S3_m1 = 0.5 * (S3[i, 0] + S3[i - 1, 0])
F1_p1 = 0.5 * (F1[i + 1, n - 1] + F1[i, n - 1])
F2_p1 = 0.5 * (F2[i + 1, n - 1] + F2[i, n - 1])
F3_p1 = 0.5 * (F3[i + 1, n - 1] + F3[i, n - 1])
S1_p1 = 0.5 * (S1[i + 1, n - 1] + S1[i, n - 1])
S2_p1 = 0.5 * (S2[i + 1, n - 1] + S2[i, n - 1])
S3_p1 = 0.5 * (S3[i + 1, n - 1] + S3[i, n - 1])
Q1[i, n] = 0.5 * (Q1[i - 1, n - 1] + Q1[i + 1, n - 1]) - Dt/Dx * (F1_p1 - F1_m1) + (S1_p1 - S1_m1) * Dt
Q2[i, n] = 0.5 * (Q2[i - 1, n - 1] + Q2[i + 1, n - 1]) - Dt/Dx * (F2_p1 - F2_m1) + (S2_p1 - S2_m1) * Dt
Q3[i, n] = 0.5 * (Q3[i - 1, n - 1] + Q3[i + 1, n - 1]) - Dt/Dx * (F3_p1 - F3_m1) + (S3_p1 - S3_m1) * Dt
rho[i, n] = Q1[i, n]
u[i, n] = Q2[i, n] / rho[i, n]
P[i, n] = (Q3[i, n] - 0.5 * rho[i, n] * u[i, n] ** 2) * (k - 1)
T[i, n] = P[i, n] / (R * rho[i, n])
F1[i, n] = Q2[i, n]
F2[i, n] = rho[i, n] * ((u[i, n] ** 2) / 2) + P[i, n]
F3[i, n] = u[i, n] * (
(rho[i, n] * ((u[i, n] ** 2) / 2)) + (k * P[i, n] / (k - 1)))
S1[i, n] = -rho[i, n] * u[i, n] * (Ac[i, 0] - Ac[i - 1, 0])
S2[i, n] = - (rho[i, n] * (
(u[i, n] ** 2) / (Ac[i, 0])) * (Ac[i, 0] - Ac[i - 1, 0])) - ((
(frict_fact * np.pi * rho[i, n] * d[i] * (u[i, n] ** 2)) / (2 * Ac[i, 0])))
S3[i, n] = -(u[i, n] * (
rho[i, n] * ((u[i, n] ** 2) / 2) + (k * P[i, n] / (k - 1))) * (
(Ac[i, 0] - Ac[i - 1, 0]) / Ac[i, 0])) + (
Lambda * np.pi * d[i] * (Tw[i] - T[i, 0]) / Ac[i, 0])
# Plot
plt.figure(1)
plt.plot(P[:, nt - 1])
plt.figure(2)
plt.plot(u[:, nt - 1])
def Lax_Wendroff():
for n in range (0,nt-1):
for i in range (1,nx-1):
Q1_plus_half = (1 / 2) * (Q1[i, n] + Q1[i + 1, n]) - (Dt / (2 * Dx)) * (F1[i + 1, n] - F1[i, n]) + (
S1[i + 1, n] - S1[i, n]) * Dt
Q1_less_half = (1 / 2) * (Q1[i, n] + Q1[i - 1, n]) - (Dt / (2 * Dx)) * (F1[i, n] - F1[i - 1, n]) + (
S1[i, n] - S1[i - 1, n]) * Dt
Q2_plus_half = (1 / 2) * (Q2[i-1, n] + Q2[i + 1, n]) - (Dt / (2 * Dx)) * (F2[i + 1, n] - F2[i, n]) + (
S2[i + 1, n] - S2[i, n]) * Dt
Q2_less_half = (1 / 2) * (Q2[i, n] + Q2[i - 1, n]) - (Dt / (2 * Dx)) * (F2[i, n] - F2[i - 1, n]) + (
S2[i, n] - S2[i - 1, n]) * Dt
Q3_plus_half = (1 / 2) * (Q3[i, n] + Q3[i + 1, n]) - (Dt / (2 * Dx)) * (F3[i + 1, n] - F3[i, n]) + (
S3[i + 1, n] - S3[i, n]) * Dt
Q3_less_half = (1 / 2) * (Q3[i, n] + Q3[i - 1, n]) - (Dt / (2 * Dx)) * (F3[i, n] - F3[i - 1, n]) + (
S3[i, n] - S3[i - 1, n]) * Dt
rho_less_half = Q1_less_half
u_less_half = Q2_less_half / rho_less_half
P_less_half = (Q3_less_half - ((1 / 2) * rho_less_half * (u_less_half ** 2) / 2)) * (k - 1)
F1_less_half = rho_less_half * u_less_half
F2_less_half = rho_less_half * ((u_less_half ** 2) / 2) + P_less_half
F3_less_half = u_less_half * ((rho_less_half * ((u_less_half ** 2) / 2)) + (k * P_less_half / (k - 1)))
rho_plus_half = Q1_plus_half
u_plus_half = Q2_plus_half / rho_plus_half
P_plus_half = (Q3_plus_half - ((1 / 2) * rho_plus_half * (u_plus_half ** 2) / 2)) * (k - 1)
F1_plus_half = rho_plus_half * u_plus_half
F2_plus_half = rho_plus_half * ((u_plus_half ** 2) / 2) + P_plus_half
F3_plus_half = u_plus_half * ((rho_plus_half * ((u_plus_half ** 2) / 2)) + (k * P_plus_half / (k - 1)))
# I termini sorgente da mettere dentro l'equazione finale di Q li calcolo come medie delle variabili nel condotto
S1_less_half = 0.5 * (S1[i - 1, n] + S1[i, n])
S2_less_half = 0.5 * (S2[i - 1, n] + S2[i, n])
S3_less_half = 0.5 * (S3[i - 1, n] + S3[i, n])
S1_plus_half = 0.5 * (S1[i + 1, n] + S1[i, n])
S2_plus_half = 0.5 * (S2[i + 1, n] + S2[i, n])
S3_plus_half = 0.5 * (S3[i + 1, n] + S3[i, n])
"""S1_less_half = Q1_less_half + F1_less_half
S2_less_half = Q2_less_half + F2_less_half
S3_less_half = Q3_less_half + F3_less_half
S1_plus_half = Q1_plus_half + F1_plus_half
S2_plus_half = Q2_plus_half + F2_plus_half
S3_plus_half = Q3_plus_half + F3_plus_half"""
Q1[i , n + 1] = Q1[i, n] - (Dt / Dx) * (F1_plus_half - F1_less_half) - (S1_plus_half - S1_less_half) * Dt
Q2[i, n + 1] = Q2[i, n] - (Dt / Dx) * (F2_plus_half - F2_less_half) - (S2_plus_half - S2_less_half) * Dt
Q3[i, n + 1] = Q3[i, n] - (Dt / Dx) * (F3_plus_half - F3_less_half) - (S3_plus_half - S3_less_half) * Dt
rho[i, n + 1] = Q1[i, n + 1]
u[i, n + 1] = Q2[i, n + 1] / rho[i, n + 1]
P[i, n + 1] = (Q3[i, n + 1] - 0.5 * rho[i, n + 1] * (u[i, n + 1] ** 2)) * (k - 1)
F1[i, n + 1] = rho[i, n + 1] * u[i, n + 1]
F2[i, n + 1] = rho[i, n + 1] * ((u[i, n + 1] ** 2) / 2) + P[i, n + 1]
F3[i, n+1] = u[i, n+1] * (
(rho[i, n+1] * ((u[i, n+1] ** 2) / 2)) + (k * P[i, n+1] / (k - 1)))
S1[i, n+1] = -rho[i, n+1] * u[i, n+1] * (Ac[i, 0] - Ac[i - 1, 0])
S2[i, n+1] = - (rho[i, n+1] * (
(u[i, n+1] ** 2) / (Ac[i, 0])) * (Ac[i, 0] - Ac[i - 1, 0])) - ((
(frict_fact * np.pi * rho[i, n+1] * d[i] * (u[i, n+1] ** 2)) / (2 * Ac[i, 0])))
S3[i, n+1] = -(u[i, n+1] * (
rho[i, n+1] * ((u[i, n+1] ** 2) / 2) + (k * P[i, n+1] / (k - 1))) * (
(Ac[i, 0] - Ac[i - 1, 0]) / Ac[i, 0])) + (
Lambda * np.pi * d[i] * (Tw[i] - T[i, 0]) / Ac[i, 0])
# Plot
plt.figure(1)
plt.plot(P[:, nt - 1])
plt.figure(2)
plt.plot(u[:, nt - 1])
I'm pretty sure that's a matter of indices but i havent't found the solution yet. Hope you can help me.

Related

Auto-Regression (2) in R

I need help generating an AR(2) model in R and I am new to the software.
I have the following AR(2) process:
y[t] = phi_1 * y[t-1] + phi_2 * y[t-2] + e[t] where e[t] ~ N(0,2)
How can I generate a series of y[t]?
Thanks for the help, much appreciated!
You could do:
set.seed(123)
n <- 200
phi_1 <- 0.9
phi_2 <- 0.7
e <- rnorm(n, 0, 2)
y <- vector("numeric", n)
y[1:2] <- c(0, 1)
for (t in 3:n) {
y[t] <- phi_1 * y[t - 1] + phi_2 * y[t - 2] + e[t]
}
plot(seq(n), y, type = "l")

Plotting fitted values from regression

Hey I have following code in R
S0 = 40
r = log(1 + 0.07)
sigma = 0.3
K = 45
n_steps_per_year = 4
dt = 1 / n_steps_per_year
T = 3
n_steps = n_steps_per_year * T
R = n_paths
Q = 70
P = 72
n_paths = P * Q
d = exp(-r * dt)
N = matrix(rnorm(n_paths * n_steps, mean = 0, sd = 1), n_paths, n_steps)
paths_S = matrix(nrow = n_paths, ncol = n_steps + 1, S0)
for(i in 1:n_paths){
for(j in 1:n_steps){
paths_S[i, j + 1] = paths_S[i, j] * exp((r - 0.5 * sigma ^ 2) * dt + sigma * sqrt(dt) * N[i, j])
}
}
I = apply(K - paths_S, c(1,2), max, 0)
V = matrix(nrow = n_paths, ncol = n_steps + 1)
V[, n_steps + 1] = I[, n_steps + 1]
dV = d * V[, n_steps + 1]
model = lm(dV ~ poly(paths_S[, n_steps], 10))
pred = predict(model, data.frame(x = paths_S[, n_steps]))
plot(paths_S[, n_steps], d * V[, n_steps + 1])
lines(paths_S[, n_steps], pred)
but when I run the last two lines then I get very strange plot (multiple lines instead of one line). What is going on?
You did not provide n_paths, lets assume:
n_paths = 7
set.seed(111)
Then running your code, before you plot, you need to order your x values before plotting:
o = order(paths_S[,12])
plot(paths_S[o, n_steps], d * V[o, n_steps + 1],cex=0.2,pch=20)
lines(paths_S[o, n_steps], pred[o],col="blue")

How to compute double integral in r?

Suppose that we have the following density :
bvtnorm <- function(x, y, mu_x = 10, mu_y = 5, sigma_x = 3, sigma_y = 7, rho = 0.4) {
function(x, y)
1 / (2 * pi * sigma_x * sigma_y * sqrt(1 - rho ^ 2)) *
exp(- 1 / (2 * (1 - rho ^ 2)) * (((x - mu_x) / sigma_x) ^ 2 +
((y - mu_y) / sigma_y) ^ 2 - 2 * rho * (x - mu_x) * (y - mu_y) /
(sigma_x * sigma_y)))
}
f2 <- bvtnorm(x, y)
I'm wanting to compute the follwing integral :
integral_1=1-adaptIntegrate(f2, lowerLimit = c(-Inf,0), upperLimit = c(+Inf,+Inf))
Unfortunately , it provides this error :
Error in f(tan(x), ...) : argument "y" is missing, with no default
I don't know how to resolve this.
Thank you for help in advance !
With package cubature, functions hcubature and pcubature the integrand would have to be changed a bit. The integrators from that package accept integrand functions of one variable only, that can be a vector in a multidimensional real space. In this case, R2. The values of x and y would have to be assigned in the integrand or change to become x[1] and x[2] in its expression.
bvtnorm <- function(x, mu_x = 10, mu_y = 5, sigma_x = 3, sigma_y = 7, rho = 0.4) {
y <- x[2]
x <- x[1]
1 / (2 * pi * sigma_x * sigma_y * sqrt(1 - rho ^ 2)) *
exp(- 1 / (2 * (1 - rho ^ 2)) * (((x - mu_x) / sigma_x) ^ 2 +
((y - mu_y) / sigma_y) ^ 2 - 2 * rho * (x - mu_x) * (y - mu_y) /
(sigma_x * sigma_y)))
}
library(cubature)
eps <- .Machine$double.eps^0.5
hcubature(bvtnorm, lowerLimit = c(-Inf, 0), upperLimit = c(+Inf,+Inf), tol = eps)
pcubature(bvtnorm, lowerLimit = c(-Inf, 0), upperLimit = c(+Inf,+Inf), tol = eps)
If you need to do a double integral, you could just integrate twice:
bvtnorm <- function(y, mu_x = 10, mu_y = 5, sigma_x = 3, sigma_y = 7, rho = 0.4) {
function(x)
1 / (2 * pi * sigma_x * sigma_y * sqrt(1 - rho ^ 2)) *
exp(- 1 / (2 * (1 - rho ^ 2)) *
(((x - mu_x) / sigma_x) ^ 2 +
((y - mu_y) / sigma_y) ^ 2 - 2 * rho * (x - mu_x) * (y - mu_y) /
(sigma_x * sigma_y)))
}
f3 <- function(y)
{
f2 <- bvtnorm(y = y)
integrate(f2, lower = -Inf, upper = Inf)$value
}
integrate(Vectorize(f3), -Inf, Inf)
#> 1.000027 with absolute error < 1.8e-05
This gives an answer that is pleasingly close to 1, as expected.
Created on 2020-09-05 by the reprex package (v0.3.0)

empty argument error in rootSolve package in R

I am using rootSolve package in R to solve a system of 6 non-linear equations with 6 unknown variables. Here is my model
model <- function(x, parms) c(F1 = x[1] - parms[1] - 1 / ((-parms[7]) * (1 - x[4])),
F2 = x[2] - parms[2] - 1 / ((-parms[7]) * (1 - x[5])),
F3 = x[3] - parms[3] - 1 / ((-parms[7]) * (1 - x[6])),
F4 = x[4] - exp(parms[4] + parms[7] * x[1]) / (1 + exp(parms[4] + parms[7] * x[1]) + exp(parms[5] + parms[7] * x[2]) + exp(parms[6] + parms[7] * x[3])),
F5 = x[5] - exp(parms[5] + parms[7] * x[2]) / (1 + exp(parms[4] + parms[7] * x[1]) + exp(parms[5] + parms[7] * x[2]) + exp(parms[6] + parms[7] * x[3])),
F6 = x[6] - exp(parms[6] + parms[7] * x[3]) / (1 + exp(parms[4] + parms[7] * x[1]) + exp(parms[5] + parms[7] * x[2]) + exp(parms[6] + parms[7] * x[3])),
)
But when I call
new.equi = multiroot(model, start = initial.value, parms = parm)
where I pass value to initial.value and parm, I keep getting the error of
Error in c(F1 = x[1] - parms[1] - 1/((-parms[7]) * (1 - x[4])), F2 = x[2] - : argument 7 is empty
Why is this happening? Why should there be argument 7?
I also tried to specify parameters in the model explicitly, like this, but still get the same error.
model <- function(x) c(F1 = x[1] - 1.265436 - 1 / (2.443700 * (1 - x[4])),
F2 = x[2] - 1.195844 - 1 / (2.443700 * (1 - x[5])),
F3 = x[3] - 1.288660 - 1 / (2.443700 * (1 - x[6])),
F4 = x[4] - exp(4.600528 - 2.443700 * x[1]) / (1 + exp(4.600528 - 2.443700 * x[1]) + exp(3.924360 - 2.443700 * x[2]) + exp(4.643808 - 2.443700 * x[3])),
F5 = x[5] - exp(3.924360 - 2.443700 * x[2]) / (1 + exp(4.600528 - 2.443700 * x[1]) + exp(3.924360 - 2.443700 * x[2]) + exp(4.643808 - 2.443700 * x[3])),
F6 = x[6] - exp(4.643808 - 2.443700 * x[3]) / (1 + exp(4.600528 - 2.443700 * x[1]) + exp(3.924360 - 2.443700 * x[2]) + exp(4.643808 - 2.443700 * x[3])),
)
You have a trailing comma. E.g.:
> c(1,2,)
Error in c(1, 2, ) : argument 3 is empty

Google Maps API V3 - Polygon SMOOTHED edges

Is it possible to smooth the lines/edges for a polygon? It's currently very sharp and angular and it would be great if those angles actually had curvature to them. Any ideas?
Add additional points into your polygon. The more points that are plotted, the more gradual the curve will be.
Here is a smoothing algorithm based on bSpline that worked for me on Android inspired by https://johan.karlsteen.com/2011/07/30/improving-google-maps-polygons-with-b-splines/
public List<LatLng> bspline(List<LatLng> poly) {
if (poly.get(0).latitude != poly.get(poly.size()-1).latitude || poly.get(0).longitude != poly.get(poly.size()-1).longitude){
poly.add(new LatLng(poly.get(0).latitude,poly.get(0).longitude));
}
else{
poly.remove(poly.size()-1);
}
poly.add(0,new LatLng(poly.get(poly.size()-1).latitude,poly.get(poly.size()-1).longitude));
poly.add(new LatLng(poly.get(1).latitude,poly.get(1).longitude));
Double[] lats = new Double[poly.size()];
Double[] lons = new Double[poly.size()];
for (int i=0;i<poly.size();i++){
lats[i] = poly.get(i).latitude;
lons[i] = poly.get(i).longitude;
}
double ax, ay, bx, by, cx, cy, dx, dy, lat, lon;
float t;
int i;
List<LatLng> points = new ArrayList<>();
// For every point
for (i = 2; i < lats.length - 2; i++) {
for (t = 0; t < 1; t += 0.2) {
ax = (-lats[i - 2] + 3 * lats[i - 1] - 3 * lats[i] + lats[i + 1]) / 6;
ay = (-lons[i - 2] + 3 * lons[i - 1] - 3 * lons[i] + lons[i + 1]) / 6;
bx = (lats[i - 2] - 2 * lats[i - 1] + lats[i]) / 2;
by = (lons[i - 2] - 2 * lons[i - 1] + lons[i]) / 2;
cx = (-lats[i - 2] + lats[i]) / 2;
cy = (-lons[i - 2] + lons[i]) / 2;
dx = (lats[i - 2] + 4 * lats[i - 1] + lats[i]) / 6;
dy = (lons[i - 2] + 4 * lons[i - 1] + lons[i]) / 6;
lat = ax * Math.pow(t + 0.1, 3) + bx * Math.pow(t + 0.1, 2) + cx * (t + 0.1) + dx;
lon = ay * Math.pow(t + 0.1, 3) + by * Math.pow(t + 0.1, 2) + cy * (t + 0.1) + dy;
points.add(new LatLng(lat, lon));
}
}
return points;
}

Resources