I am trying to solve a nonlinear system using Ceres Solver by Google. The example below comes from this page: http://terpconnect.umd.edu/~petersd/460/html/newtonex1z.html
I first create a class called MatlabExample
, where I compute the residuals
and jacobians
:
class MatlabExample
: public SizedCostFunction<2,2> {
public:
virtual ~MatlabExample() {}
virtual bool Evaluate(double const* const* parameters,
double* residuals,
double** jacobians) const {
double x1 = parameters[0][0];
double x2 = parameters[0][1];
residuals[0] = 2*x1+x1*x2-2;
residuals[1] = 2*x2-x1*pow(x2,2)-2 ;
if (jacobians != NULL && jacobians[0] != NULL) {
jacobians[0][0] = 2+x2;
jacobians[0][1] = x1;
jacobians[1][0] = -pow(x2,2);
jacobians[1][1] = 2-2*x1*x2;
}
return true;
}
};
The main file is as follows:
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
double x[] = { 0.0,0.0 };
Problem problem;
CostFunction* cost_function = new MatlabExample;
problem.AddResidualBlock(cost_function, NULL, &x);
Solver::Options options;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
return 0;
}
When compiling, I got a Segmentation fault: 11
error. Any ideas?
You are accessing the jacobians array wrong. Here is why.
When you added the residual block, you told Ceres that the cost function only depends on one parameter block of size 2 and produces a residual of size 2.
The jacobians array is an array of row-major jacobians. One for each parameter block. So in this case it is of size 1, and contains a pointer to size 4 array that should contain a row major Jacobian.
Your Jacobian filling code should instead read
if (jacobians != NULL && jacobians[0] != NULL) {
jacobians[0][0] = 2+x2;
jacobians[0][1] = x1;
jacobians[0][2] = -pow(x2,2);
jacobians[0][3] = 2-2*x1*x2;
}