実現したいこと
c++で書かれた放物型方程式のプログラムを動かし、アニメーションを表示すること。
発生しているエラー
プログラムをコンパイル、実行すると、グラフが描かれたウィンドウが一瞬表示するのですが、それが一瞬で消えてしまいます。
該当のソースコード
C++
1 2// program parabolic1 3 4#define _USE_MATH_DEFINES 5#include <iostream> 6using namespace std; 7#include <cmath> 8#include "matplotlibcpp.h" 9 10namespace plt = matplotlibcpp; 11 12void initparam(double u[], 13 int & nx, 14 double &dx, 15 double & dt, 16 int & niter, 17 int &gmode) 18{ 19 cerr << "Enter nx, tmax, dt:"; 20 double tmax; 21 cin >> nx >> tmax >> dt; 22 dx = 1.0/nx; 23 niter = (tmax+dt/2)/dt; 24 cerr << "Enter graphics mode 0: no G\n" 25 << " 1: animation\n" 26 << " 2: no-erase)\n"; 27 cin >> gmode; 28 cout << "nx = " << nx << " dt = " << dt << " niter =" << niter << "\n"; 29 cout << "gmode = " << gmode << "\n"; 30 int i; 31 for (i=0;i<=nx; i++) u[i] = 0; 32 u[(nx+1)/2] = nx; 33} 34 35void push_system(double u[], 36 double unew[], 37 int nx, 38 double dx, 39 double dt) 40{ 41 double lambda = dt/(dx*dx); 42 int i; 43 for (i=1; i<nx; i++){ 44 unew[i] = u[i] + lambda*(u[i-1]-2*u[i]+u[i+1]); 45 } 46 for(i=1; i<nx; i++){ 47 u[i] = unew[i]; 48 } 49} 50 51void show_graphics(double u[], 52 int nx, 53 double dx, 54 int gmode, 55 double xmin, 56 double xmax, 57 double ymin, 58 double ymax, 59 int first) 60{ 61 if (gmode == 0) return; 62 std::vector<double> x, y; 63 if (gmode == 1) { 64 plt::clf(); 65 } 66 int i; 67 for(i=0;i<nx+1;i++){ 68 x.push_back(i*dx); 69 y.push_back(u[i]); 70 } 71 plt::plot(x,y); 72 plt::xlim(xmin,xmax); 73 plt::ylim(ymin,ymax); 74 plt::pause(0.1); 75} 76 77int main() 78{ 79#define NMAX 10001 80 static double u[NMAX], unew[NMAX]; 81 double dx, dt; 82 int nx, niter, gmode, iter; 83 initparam(u, nx, dx, dt, niter, gmode); 84 show_graphics(u, nx, dx, gmode, 0.0, 1.0, 0.0, 10.0, 1); 85 for (iter = 1; iter <=niter; iter++){ 86 push_system(u, unew, nx, dx, dt); 87 show_graphics(u, nx, dx, gmode, 0.0, 1.0, 0.0, 10.0, 0); 88 } 89 return 0; 90 } 91 92 93
ツール
WindouwsPCでubuntu 18.04を使っています。
グラフの表示ではXmingが立ち上がります。
xeyesや、アニメーションでないグラフは普通に表示することが出来ます。
うまく説明が出来ておらず申し訳ありません。
ご回答いただけましたら幸いです。
よろしくお願いいたします。
あなたの回答
tips
プレビュー