15,939,960 members
See more:
Hi guys!
I have searched all over the web for an implementation of a b-cubic spline in c#. All I found was some math libraries and I need to implement the code by myself. To compute the spline coefficients for n knots I need to solve a system of n-1 linear equations so:
First: I don't know how to solve linear equations in C#. Only in matlab so if someone have helpful info it will be great.
Second: Is anyone familiar with spline coefficients calculation code?

Best regards,
Posted
Updated 2-May-12 22:27pm
v2

## Solution 1

In order to solve the linear equations, you may implement the Gaussian Elimination[^] algorithm.

maorizyo 3-May-12 4:40am
i know how to solve linear equtions
but i dont know how to do this in c#.
CPallini 3-May-12 4:51am
Well if you know how to solve the equations and you know the C# language then I don't see any problem. However, you may find available implementations of the Gaussian elimination method, see, for instance:
http://www.koders.com/csharp/fidE14F7B7AAE5A1B97C66DC7D2504141D6EFBA0536.aspx
maorizyo 3-May-12 4:52am
thanks a lot
CPallini 3-May-12 4:53am
You are welcome.
maorizyo 13-May-12 9:32am
hi again
after a little bit of quering found algorithm in c language that computes the coefficients.
when i run the c project it works perfectly.
but when i convert the code to c# i get only NaN values instead the coefficients.
here my code:

class spl
{
int K, J; /* Loop counter */
int N; /* INPUT: Number of points -1 */
double[] X=new double[20]; /* x-coordinates of points */
double[] Y=new double[20]; /* y-coordinates of points */
double[] H=new double[20]; /* Differences in abscissa */
double[] D=new double[20]; /* Difference quotients */
double[] C=new double[20]; /* Superdiagonal elements */
double[] A=new double[20]; /* Subdiagonal elements */
double[] B=new double[20]; /* Diagonal elements */
double[] V=new double[20]; /* Column vector */
double[] M=new double[20]; /* */
double SD, SDD; /* S'(x_0) and S''(x_N) */
double SDN, SDDN; /* S'(x_N) and S''(x_N) */
double T; /* */
double[][] S=new double[20][];
/* Coefficients for polynomials */
double Z; /* Value of spline S(x) */
int Ncase; /* Choice of condition */
double W;
int q; /* For comparison to end input */
double x; /* Independent variable */

public spl()
{
}
public spl(double[] x,double[] y)
{
for (int i = 0; i < 20; i++) { S[i] = new double[4]; }
N = x.Length - 1;
H[0] = X[1] - X[0]; /* Difference in abscissa */
D[0] = Convert.ToDouble((Y[1] - Y[0]) / H[0]); /* Difference quotient */

for (K = 1; K <= N - 1; K++)
{
H[K] = X[K + 1] - X[K]; /* Differences in abscissa */
D[K] = Convert.ToDouble((Y[K + 1] - Y[K]) / H[K]);
X[K] = Convert.ToDouble(X[K]);/* Difference quotients */
A[K] = H[K]; /* Subdiagonal elements */
B[K] = 2 * (H[K - 1] + H[K]); /* Diagonal elements */
C[K] = H[K]; /* Superdiagonal elements */
}

for (K = 1; K <= N - 1; K++) V[K] = 6.0 * (D[K] - D[K - 1]);
// natural_coeffs();
clamped_spline();

}
public void natural_coeffs()
{
M[0] = 0.0;
M[N] = 0.0;
gaussian_elimination();

M[0] = 0;
M[N] = 0;

}
public void gaussian_elimination()
{

for (K = 2; K <= N - 1; K++)
{
T =Convert.ToDouble( A[K - 1] / B[K - 1]);
B[K] -= T * C[K - 1];
V[K] -= T * V[K - 1];
}

M[N - 1] =Convert.ToDouble( V[N - 1] / B[N - 1]);

for (K = N - 2; K >= 1; K--) M[K] = Convert.ToDouble((V[K] - C[K] * M[K + 1]) / B[K]);
compute_coeffs();
}
public void compute_coeffs()
{

for (K = 0; K <= N - 1; K++)
{
S[K][0] = Y[K];
S[K][1] =Convert.ToDouble( D[K] - H[K] * (2.0 * M[K] + M[K + 1]) / 6.0);
S[K][2] =Convert.ToDouble( M[K] / 2.0);
S[K][3] = Convert.ToDouble((M[K + 1] - M[K]) / (6.0 * H[K]));
}

}
public void clamped_spline()
{
B[1] -= H[0] / 2.0;
V[1] -= 3.0 * (D[0] - SD);
B[N - 1] -= H[N - 1] / 2.0;
V[N - 1] -= 3.0 * (SDN - D[N - 1]);

gaussian_elimination();
/* Back substitution is used to fin

Top Experts
Last 24hrsThis month
 merano99 90 Richard Deeming 65 Graeme_Grant 60 OriginalGriff 50 Wendelius 25
 OriginalGriff 790 Pete O'Hanlon 725 Richard Deeming 690 merano99 335 Dave Kreskowiak 215

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900