Skip to content

Instantly share code, notes, and snippets.

@feliwir
Created May 11, 2016 12:05
Show Gist options
  • Save feliwir/61fc8b1a583297939074f15a13c54f13 to your computer and use it in GitHub Desktop.
Save feliwir/61fc8b1a583297939074f15a13c54f13 to your computer and use it in GitHub Desktop.
void computeCubicSplineCoefficients(double** a, double* x, double* y, int n)
{
double** matrix = new double*[n-1];
for (int i=0; i<n-1; i++)
matrix[i] = new double[n-1];
double* sol = new double[n-1];
double* rhs = new double[n-1];
//create our h array
double* h = new double[n-1];
for(int i=0;i<n-1;++i)
{
h[i] = x[i+1]-x[i];
std::cout << h[i] << std::endl;
}
//fill the matrix here
for(int i=0;i<n-1;++i)
{
if(i-1>=0)
matrix[i][i-1]=h[i+1];
matrix[i][i]= 2*(h[i+1]+h[i+2]);
if(i+1<=n+1)
matrix[i][i+1]=h[i+2];
rhs[i] = 3*((y[i+2]-y[i+1])/h[i+1]-(y[i+1]-y[i])/h[i]);
}
for(int r=0;r<n-1;++r)
{
for(int c=0;c<n-1;++c)
std::cout << matrix[r][c] << "\t";
std::cout << std::endl;
}
// solve
solveByGaussSeidel(sol, matrix, rhs, n-1);
for(int i=0;i<n-1;++i)
{
std::cout << sol[i]<< std::endl;
}
// cleanup
for (int i=0; i<n-1; i++)
delete [] matrix[i];
delete [] matrix;
delete [] sol;
delete [] rhs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment