/* * ball.cpp --- はねるボール * http://nalab.mind.meiji.ac.jp/~mk/program/ode_prog/ball.cpp * cc -I /usr/local/include ball.cpp * ./a.out > ball.data * gnuplot で plot "ball.data" with lp */ #include <iostream> #include <math.h> #include <Eigen/Dense> using namespace Eigen; double m, g, Gamma, e; VectorXd f(double t, VectorXd x) { VectorXd y(4); y(0) = x(2); y(1) = x(3); y(2) = - Gamma / m * x(2); y(3) = - g - Gamma / m * x(3); return y; } int main(void) { int n, N; double tau, Tmax, t,pi; VectorXd x(4),k1(4),k2(4),k3(4),k4(4); pi = 4 * atan(1.0); m = 100; g = 9.8; Gamma = 1.0; e = 1.0; Tmax = 20; N = 1000; tau = Tmax / N; x << 0,0,50*cos(pi*50/180),50*sin(pi*50/180); for (n = 0; n < N; n++) { t = n * tau; k1 = tau * f(t, x); k2 = tau * f(t+tau/2, x+k1/2); k3 = tau * f(t+tau/2, x+k2/2); k4 = tau * f(t+tau, x+k3); x = x + (k1 + 2 * k2 + 2 * k3 + k4) / 6; if (x(1)<0) { x(1) = - x(1); x(3) = - x(3); } std::cout << x(0) << " " << x(1) << std::endl; } }
コンパイル&実行 |
curl -O http://nalab.mind.meiji.ac.jp/~mk/program/ode_prog/ball.cpp c++ -O -I /usr/local/include ball.cpp ./a.out > ball.data |
gnuplot で描画 |
gnuplot> plot "ball.data" with lp |