// ================= // || Worksheet 14 // || In this exercise, we are going be solving the 1d KDV (Korteweg De Vries) equation. // d/dt f(x,t) = - d^3/dx^3 f(x,t) - 6 k f(x,t) d/dx f(x,t) // ================= // see also, e.g. https://en.wikipedia.org/wiki/Korteweg%E2%80%93de_Vries_equation #include "program.h" using namespace std; using namespace cv; void INSERT_KDV_SOLITON(Mat img, float x0, float c) { float val; for(int x =0; x < img.cols; x++) { val = 0.5*sqrt(c)*(x-x0); val = 0.5*c/(cosh(val)*cosh(val)); for(int y =0; y < img.rows; y++) { img.at(x,y) += val; } } } int worksheetfourteen(Mat& src) { Mat input = Mat::zeros(200,200, CV_32F ); // insert two solitons! INSERT_KDV_SOLITON(input,20,3); INSERT_KDV_SOLITON(input,60,1); // a window to show our outputs cv::namedWindow( "current frame", WINDOW_AUTOSIZE ); imshow( "current frame", input); waitKey(); int MAX_NUMBER_OF_FRAMES = 5000; // dissipation factor. float k = 0.01f; // this is of the initial data Mat prev_frame = input.clone() ; Mat colorized_frame; // repeat this for each of the frames. for (int n = 0; n < MAX_NUMBER_OF_FRAMES; n++) { // Do the numerical calculations here! // ??? Mat frame; // lets colorize our image too, so that it looks better. frame.convertTo(colorized_frame,CV_8UC1, 127,127); applyColorMap(colorized_frame, colorized_frame,COLORMAP_AUTUMN); // lets also make it 4 times the size, so we dont strain our eyes. resize(colorized_frame,colorized_frame,Size(colorized_frame.cols*4,colorized_frame.rows*4 ) ); // display the current frame. cv::imshow( "current frame", colorized_frame); //wait for 'esc' key press for 300ms. If 'esc' key is pressed, break loop if (waitKey(10) == 27) { cout << "esc key is pressed by user" << endl; break; } // need to update prev_frame = frame.clone(); } return 0; }