Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | File Members

model_sph.cpp

Go to the documentation of this file.
00001 
00006 #include <math.h>
00007 #include "vario_model.h"
00008 
00018 double spherical(double n, double s, double r, double d)
00019 {
00020         if (d > r)
00021                 return n + s;
00022         return n + s * (3.0 / 2.0 * fabs(d) / r - pow(fabs(d) / r, 3.0) / 2);
00023 }
00024 
00025 
00026 //
00027 // following three functions are defined according to GSL Nonlinear Least-Squares Fitting Solver
00028 // Related link: http://www.gnu.org/software/gsl/manual/gsl-ref_37.html#SEC476
00029 //
00030 
00031 
00032 int sph_f(const gsl_vector *x, void *params, gsl_vector *f)
00033 {
00034         size_t n = ((LSFParam *)params)->n;
00035         double *dist = ((LSFParam *)params)->dist;
00036         double *vario = ((LSFParam *)params)->vario;
00037 
00038         double sill = gsl_vector_get(x, 0);
00039         double range = gsl_vector_get(x, 1);
00040         double nugget = gsl_vector_get(x, 2);
00041 
00042         for (size_t i = 0;i < n;i++)
00043         {
00044                 double Yi = spherical(nugget, sill, range, dist[i]);
00045                 gsl_vector_set(f, i, Yi - vario[i]);
00046         }
00047 
00048         return GSL_SUCCESS;
00049 }
00050 
00051 int sph_df(const gsl_vector *x, void *params, gsl_matrix *J)
00052 {
00053         size_t n = ((LSFParam *)params)->n;
00054         double *dist = ((LSFParam *)params)->dist;
00055         double *vario = ((LSFParam *)params)->vario;
00056 
00057         double sill = gsl_vector_get(x, 0);
00058         double range = gsl_vector_get (x, 1);
00059         double nugget = gsl_vector_get(x, 2);
00060 
00061         for (size_t i = 0;i < n;i++)
00062         {
00063                 if (dist[i] > range)
00064                 {
00065                         // df / d(sill)
00066                         gsl_matrix_set(J, i, 0, 1.0);
00067                         // df / d(range)
00068                         gsl_matrix_set(J, i, 1, 0.0);
00069                 }
00070                 else
00071                 {
00072                         double sp = fabs(dist[i]) / range;
00073                         double tp = pow(sp, 3.0);
00074                         // df / d(sill)
00075                         gsl_matrix_set(J, i, 0, 3.0 / 2.0 * sp - tp / 2.0);
00076                         // df / d(range)
00077                         gsl_matrix_set(J, i, 1, 3.0 / 2.0 * sp / range - 3.0 / 2.0 * tp / range);
00078                 }
00079                 // df / d(nugget)
00080                 gsl_matrix_set(J, i, 2, 1.0); 
00081         }
00082         return GSL_SUCCESS;
00083 }
00084 
00085 int sph_fdf(const gsl_vector *x, void *params, gsl_vector *f, gsl_matrix *J)
00086 {
00087         sph_f(x, params, f);
00088         sph_df(x, params, J);
00089         return GSL_SUCCESS;
00090 }

Generated on Thu Aug 11 13:01:33 2005 for Kriging sample by  doxygen 1.4.3