cvl-robot's diary

研究ノート メモメモ https://github.com/dotchang/

C言語で微分の数値計算

もっといい方法ありそう。

#include < iostream >
#include < float.h >

float func(float x)
{
return x*x; // どんな関数でもいい
}

int main(int argc, char* argv[])
{
float x, df_dx;
x = 0.5; // 適当

df_dx = (fx(x+FLT_EPSILON)-fx(x-FLT_EPSILON))/(2*FLT_EPSILON);
std::cout << df_dx << std::endl;

return 0;
}