【コピペあり】C言語を使いオイラー法で微分方程式を解く
#include <stdio.h>
int main(void){
float v1, y1, yh, vh, c1, c2;
float v0=0;
float y0=1;
float k=22;
float m=1;
float R=1.2;
float dt=0.0274;
float t=0;
int i=0;
c1 = k/m;
c2 = R/m;
while(i<300){
printf("i回目 = %d\n", i);
printf("t = %f\n", t);
printf("v0 = %f\n", v0);
printf("y0 = %f\n", y0);
printf("\n");
//仮半数先
yh = y0 + v0 * 0.5 * dt;
vh = v0 + (-c1*y0 -c2*v0) * 0.5 * dt;
v1 = v0 + (-c1*yh -c2*vh) * dt;
y1 = y0 + vh * dt;
v0 = v1;
y0 = y1;
i++;
t = i * dt;
}
}