如何使用 GSL 库将雅可比矩阵引用为空指针

How to refer Jacobi matrix as a null pointer using GSL library

我尝试学习使用 GSL 库,但我被函数 "gsl_odeiv2_system" 所困(参见 https://www.gnu.org/software/gsl/doc/html/ode-initval.html#c.gsl_odeiv2_system

他们说并不是每个步进函数都需要雅可比矩阵来计算积分,所以我尝试在没有雅可比矩阵的情况下重新制作他们给出的示例,但当他们说时我不明白该怎么做:"the jacobian element of the struct can be replaced by a null pointer for those algorithms)".

函数是这样写的:

gsl_odeiv2_system sys = {func, jac, 2, &mu};

Jac 论证是这样写的:

int
jac (double t, const double y[], double *dfdy,
 double dfdt[], void *params) {
(void)(t); /* avoid unused parameter warning */
double mu = *(double *)params;
gsl_matrix_view dfdy_mat = gsl_matrix_view_array (dfdy, 2, 2);
gsl_matrix * m = &dfdy_mat.matrix;

gsl_matrix_set (m, 0, 0, 0.0);
gsl_matrix_set (m, 0, 1, 1.0);
gsl_matrix_set (m, 1, 0, -2.0*mu*y[0]*y[1] - 1.0);
gsl_matrix_set (m, 1, 1, -mu*(y[0]*y[0] - 1.0));

dfdt[0] = 0.0;
dfdt[1] = 0.0;
return GSL_SUCCESS;
}

他们的意思是你应该上交f.e。 NULL 作为 system 的第二个参数。如下图。

#include <gsl/gsl_odeiv2.h>
#include <gsl/gsl_errno.h>

int f (double t, const double y[], double x[], void *params) {
  //Some calculation
  // x[0] = ...;
  // x[1] = ...;
  return GSL_SUCCESS;
}

struct parameters {
  double alpha;
  int step;
  parameters (double a, int s) : alpha(a), step(s) {}
};

int main () {

  parameters p(1.0,1);
  gsl_odeiv2_system system = {f, NULL, 2, &p};
  gsl_odeiv2_driver * driver = gsl_odeiv2_driver_alloc_y_new (
    &system, gsl_odeiv2_step_rkf45, 1.e-6, 1.e-6, 0.);

  ...

  return 0;
}