Last active
February 28, 2018 18:54
-
-
Save wilsenhc/bbbc4c6ee7bfdecba7f94cef81c0959f to your computer and use it in GitHub Desktop.
SPLINES
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
#include <list> | |
using namespace std; | |
string colors[13] = {"blueviolet", "cadetblue", "coral", "cyan", "darkorchid", "darkred", | |
"goldenrod", "hotpink", "lime", "orange", "tomato", "darkgreen", "red"}; | |
void NaturalCubicSpline(double x[], double y[], int N, double b[], double c[], double d[]) | |
{ | |
double h[N]; | |
double alpha[N]; | |
double l[N]; | |
double u[N]; | |
double z[N]; | |
// Paso 1 | |
for (int i = 0; i <= N-1; i++) | |
h[i] = x[i+1] - x[i]; | |
// Paso 2 | |
for (int i = 1; i <= N-1; i++) | |
alpha[i] = ((3e0/h[i])*(y[i+1] - y[i])) - ((3e0/h[i-1])*(y[i] - y[i-1])); | |
// Paso 3 | |
l[0] = 1; | |
u[0] = 0; | |
z[0] = 0; | |
// Paso 4 | |
for (int i = 1; i <= N-1; i++) | |
{ | |
l[i] = (2e0*(x[i+1] - x[i-1])) - (h[i-1]*u[i-1]); | |
u[i] = h[i]/l[i]; | |
z[i] = (alpha[i] - (h[i-1]*z[i-1]))/l[i]; | |
} | |
// Paso 5 | |
l[N] = 1; | |
z[N] = 0; | |
c[N] = 0; | |
for (int i = N-2; i >= 0; i--) | |
{ | |
c[i] = z[i] - (u[i]*c[i+1]); | |
b[i] = ((y[i+1] - y[i])/h[i]) - (h[i]*(c[i+1]+(2e0*c[i])))/3e0; | |
d[i] = (c[i+1] - c[i])/(3e0*h[i]); | |
} | |
} | |
int DIST_MAX; | |
int main() | |
{ | |
int N; | |
cin >> N; | |
double n = N; | |
double* x = new double[N]; | |
double* y = new double[N]; | |
double* t = new double[N]; | |
double* dt = new double[N]; | |
double* bx = new double[N]; | |
double* cx = new double[N]; | |
double* dx = new double[N]; | |
double* by = new double[N]; | |
double* cy = new double[N]; | |
double* dy = new double[N]; | |
double j = 0e0; | |
for (int i = 0; i < N; i++, j++) | |
{ | |
cin >> x[i]; | |
cin >> y[i]; | |
} | |
DIST_MAX = 0; | |
dt[0] = 0; | |
for (int i = 1; i < N; i++) | |
{ | |
dt[i] = sqrt( pow(x[i]-x[i-1],2) + pow(y[i]-y[i-1],2) ); | |
DIST_MAX = DIST_MAX + dt[i]; | |
} | |
for (int i = 0; i < N; i++) | |
{ | |
t[i] = 0; | |
for (int j = 1; j <= i; j++) | |
{ | |
t[i] = t[i] + dt[j]; | |
} | |
t[i] = t[i] / DIST_MAX; | |
} | |
NaturalCubicSpline(t, x, N, bx, cx, dx); | |
NaturalCubicSpline(t, y, N, by, cy, dy); | |
/* ===================================================== | |
* Imprimir los Spline listos para graficar con SageMath | |
* */ | |
char letter = 'a'; | |
for (int i = 0; i < N-1; i++) | |
{ | |
cout << "WX" << letter << "(t) = " | |
<< x[i] << " + " | |
<< "(" << bx[i] << ")*(t-(" << t[i] << ")) + " | |
<< "(" << cx[i] << ")*(t-(" << t[i] << "))^2 + " | |
<< "(" << dx[i] << ")*(t-(" << t[i] << "))^3" << endl; | |
cout << "WY" << letter << "(t) = " | |
<< y[i] << " + " | |
<< "(" << by[i] << ")*(t-(" << t[i] << ")) + " | |
<< "(" << cy[i] << ")*(t-(" << t[i] << "))^2 + " | |
<< "(" << dy[i] << ")*(t-(" << t[i] << "))^3" << endl; | |
letter++; | |
} | |
cout << endl; | |
letter = 'a'; | |
for (int i = 0; i < N-1; i++) | |
{ | |
cout << "W" << letter << " = parametric_plot(" | |
<< "(WX" << letter << "(t), " | |
<< "WY" << letter << "(t)), " | |
<< "(t," << t[i] << ", " << t[i+1] << "), " | |
<< "color='" << colors[i%13] << "', " | |
<< "thickness=2" | |
<< ")" << endl; | |
letter++; | |
} | |
cout << endl; | |
letter = 'a'; | |
for (int i = 0; i < N-1; i++, letter++) | |
cout << "W" << letter << " + "; | |
cout << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 | |
29 56 | |
14 34 | |
4 44 | |
19 64 | |
45 52 | |
57 55 | |
61 65 | |
51 63 | |
40 36 | |
23 4 | |
51 32 | |
70 56 | |
62 33 | |
46 4 | |
108 64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 | |
91 14 | |
120 26 | |
102 36 | |
80 16 | |
101 3 | |
140 29 | |
169 63 | |
142 48 | |
135 63 | |
115 44 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 | |
126 23 | |
154 34 | |
165 36 | |
191 47 | |
192 62 | |
156 19 | |
156 4 | |
167 8 | |
172 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment