Skip to content

Instantly share code, notes, and snippets.

@TonyMooori
Last active March 9, 2016 06:56
Show Gist options
  • Save TonyMooori/f2fb9be12f93d3f47faa to your computer and use it in GitHub Desktop.
Save TonyMooori/f2fb9be12f93d3f47faa to your computer and use it in GitHub Desktop.
オイラー法(Euler's Method)による1階微分方程式の実装例です
/* Euler法による微分方程式 f' - 2f = x の計算 */
#include <stdio.h>
#include <math.h>
double g(double x,double fx){
return x + 2.0 * fx;
}
int main(void){
int k; // ループカウンタ
int N = 10000; // 繰り返し数
double dx = 1e-3; // 刻み幅Δx
double f = 1.0; // 関数f(x)の値(初期値)
double x_0 = 0.0; // 初期値の時のxの値
double x_k; // x_kの値
for( k = 1 ; k <= N ; k++ ){
x_k = x_0 + k * dx; // x_kの値を求める
f = f + g(x_k,f) * dx; // f_k + g(x_k,f(x_k))をfに代入
// 値の表示
printf("%lf,%e\n",x_k,f);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment