使用ceres进行曲线拟合

本文通过一个具体的非线性最小二乘问题实例介绍了Ceres优化库的使用方法。该实例涉及了C++代码实现,包括如何定义成本函数、构建优化问题、配置求解器以及求解过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文首先通过下面的公式生成一系列的数据。
待拟合曲线函数
然后构造如下所示的最小二乘问题:
在这里插入图片描述
下面来看一下如何使用ceres对该问题进行优化吧!

#include <iostream>
#include <ceres/ceres.h>
#include <glog/logging.h>
#include<chrono>
#include <math.h>
#include <algorithm>
#include <fstream>
using namespace std;
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solve;
using ceres::Solver;
//使用define来定义一个宏
//生成a,b中的随机数
#define random(a,b) (rand()%(b-a+1)+a)
//x表示待优化参数块,residual表示残差块
//通过非线性优化找到函数梯度下降的自变量的取值。
//定义残差块
struct CostFunctor {
    CostFunctor(double x,double y):x_(x),y_(y){
    }
  template <typename T>
  bool operator()(const T* const  m,const T* const  c,T* residual) const {
    //residual[0] = exp(m[0]*T(x_)+c[0])-T(y_);
    residual[0] = (ceres::exp(m[0]*T(x_)+c[0]))-T(y_);
    return true;
  }
private:
    const double x_,y_;
};

int main(int argc, char** argv) {
    const double data[] = {
  0.000000e+00, 1.133898e+00,
  7.500000e-02, 1.334902e+00,
  1.500000e-01, 1.213546e+00,
  2.250000e-01, 1.252016e+00,
  3.000000e-01, 1.392265e+00,
  3.750000e-01, 1.314458e+00,
  4.500000e-01, 1.472541e+00,
  5.250000e-01, 1.536218e+00,
  6.000000e-01, 1.355679e+00,
  6.750000e-01, 1.463566e+00,
  7.500000e-01, 1.490201e+00,
  8.250000e-01, 1.658699e+00,
  9.000000e-01, 1.067574e+00,
  9.750000e-01, 1.464629e+00,
  1.050000e+00, 1.402653e+00,
  1.125000e+00, 1.713141e+00,
  1.200000e+00, 1.527021e+00,
  1.275000e+00, 1.702632e+00,
  1.350000e+00, 1.423899e+00,
  1.425000e+00, 1.543078e+00,
  1.500000e+00, 1.664015e+00,
  1.575000e+00, 1.732484e+00,
  1.650000e+00, 1.543296e+00,
  1.725000e+00, 1.959523e+00,
  1.800000e+00, 1.685132e+00,
  1.875000e+00, 1.951791e+00,
  1.950000e+00, 2.095346e+00,
  2.025000e+00, 2.361460e+00,
  2.100000e+00, 2.169119e+00,
  2.175000e+00, 2.061745e+00,
  2.250000e+00, 2.178641e+00,
  2.325000e+00, 2.104346e+00,
  2.400000e+00, 2.584470e+00,
  2.475000e+00, 1.914158e+00,
  2.550000e+00, 2.368375e+00,
  2.625000e+00, 2.686125e+00,
  2.700000e+00, 2.712395e+00,
  2.775000e+00, 2.499511e+00,
  2.850000e+00, 2.558897e+00,
  2.925000e+00, 2.309154e+00,
  3.000000e+00, 2.869503e+00,
  3.075000e+00, 3.116645e+00,
  3.150000e+00, 3.094907e+00,
  3.225000e+00, 2.471759e+00,
  3.300000e+00, 3.017131e+00,
  3.375000e+00, 3.232381e+00,
  3.450000e+00, 2.944596e+00,
  3.525000e+00, 3.385343e+00,
  3.600000e+00, 3.199826e+00,
  3.675000e+00, 3.423039e+00,
  3.750000e+00, 3.621552e+00,
  3.825000e+00, 3.559255e+00,
  3.900000e+00, 3.530713e+00,
  3.975000e+00, 3.561766e+00,
  4.050000e+00, 3.544574e+00,
  4.125000e+00, 3.867945e+00,
  4.200000e+00, 4.049776e+00,
  4.275000e+00, 3.885601e+00,
  4.350000e+00, 4.110505e+00,
  4.425000e+00, 4.345320e+00,
  4.500000e+00, 4.161241e+00,
  4.575000e+00, 4.363407e+00,
  4.650000e+00, 4.161576e+00,
  4.725000e+00, 4.619728e+00,
  4.800000e+00, 4.737410e+00,
  4.875000e+00, 4.727863e+00,
  4.950000e+00, 4.669206e+00,
};

const int kNumObservations = 67;
 google::InitGoogleLogging(argv[0]);
//设置待优化变量初值
  double m=0,c=0;
  const double initial_m = 0;
  const double initial_c=0;
//构建优化问题
  Problem problem;
  // Set up the only cost function (also known as residual). This uses
  // auto-differentiation to obtain the derivative (jacobian).
  
  for(int i=0;i<kNumObservations;i++)
  { 
      openfile<<data[2*i]<<"           "<<data[2*i+1]<<endl;
      //定义残差块,误差类型,输入维度,输出维度
      CostFunction* cost_function =new AutoDiffCostFunction<CostFunctor, 1,1,1>(new CostFunctor(data[2*i],data[2*i+1]));
      //problem.AddResidualBlock(cost_function, nullptr,&m,&c );
      //添加残差块
      problem.AddResidualBlock(cost_function,    new ceres:: CauchyLoss(0.5),&m,&c );
 }
  
      //优化问题添加残差块,不使用核函数
  //配置求解器
  ceres::Solver::Options options;
  options.linear_solver_type=ceres::DENSE_QR;//增量方程求解
  options.minimizer_progress_to_stdout=true;//输出到cout
  ceres::Solver::Summary summary;
  //开始求解
  ceres::Solve(options,&problem,&summary);
  //输出求解过程
  cout<<summary.BriefReport()<<endl;
  //输出优化后的参数
  std::cout << "m: " << initial_m<< " -> " << m << "\n";
 std::cout << "c: " << initial_c<< " -> " << c << "\n";
 openfile.close();
  return 0;
}

cmake_minimum_required(VERSION 2.6)
project(ceres_learn)

set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT 0)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)

find_package( Ceres REQUIRED)
include_directories( ${CERES_INCLUDE_DIRS})
add_executable(ceres_learn curve_fitting.cpp)
target_link_libraries(ceres_learn ${CERES_LIBRARIES})

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43257155

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值