Click here to Skip to main content
15,917,329 members

Comments by maorizyo (Top 3 by date)

maorizyo 13-May-12 9:32am View    
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
maorizyo 3-May-12 4:52am View    
thanks a lot
this is very helpfull
maorizyo 3-May-12 4:40am View    
i know how to solve linear equtions
but i dont know how to do this in c#.