Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

1 solution

In order to solve the linear equations, you may implement the Gaussian Elimination[^] algorithm.
 
Share this answer
 
Comments
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
this is very helpfull
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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