LoginSignup
0
1

More than 5 years have passed since last update.

Euler法による数値解析

Posted at

Euler法とは

Euler法とは、常微分方程式の数値解析に使用される方法の1つです。
微分方程式では、ある関数の次の値を求めることが難しいので、様々な数値解を求める方法があります。今回は、そのEuler法をC言語で記述し、データをgunuplotで表記します。また、対象とする常微分方程式は、RC回路のコンデンサ電圧における回路方程式とします。この回路では、直流電源を用いるとします。まず、Cのソースコードを示します。

#include<stdio.h>
#include<math.h>

float func(float t, float Vc) {

    return 100000 - 1000 * Vc;

}//オイラー法用

int main(void) {

    float Vc, t, step, R, C, end;
    int error;
    FILE *fp;

    R = 10;
    C = 0.0001;
    step = ((R*C) / 100.0)*50;
    Vc = 0.0;
    end = 0.01;

    if ((error = fopen_s(&fp,"Euler_data_50.txt", "w")) != 0) {

        printf("\nThis file can't opened \n\n");

        return 0;

    }

    for (t = 0.0; t <= end; t = t + step) {

        fprintf(fp, "%f %f\n", t, Vc);
        Vc = Vc + step * func(t, Vc);

    } //Euler法用

    fclose(fp);

    return 0;
}

上記のソースコードを実行すると、時間領域におけるRC回路のコンデンサ電圧の値がtextファイルとして出てきます。
この出てきたファイルをgunuplotで実行させるとRC回路のコンデンサ電圧におけるEuler法を用いた数値解をプロットする事が出来ます。

以下に、簡単なGunuplotのソースコードを示します。


se grid
se xrange [0:0.01]
se yrange [0:110]
se xlabel "s[sec]"
se ylabel "Vc[V]"
plot "Euler_data.txt" using 1:2 with lines



0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1