int main(int argc, char **argv) { enum Constexpr {n_points = 1000}; double mu = 1.0; gsl_odeiv2_system sys = {ode_func, ode_jac, 2, &mu}; gsl_odeiv2_driver * d= gsl_odeiv2_driver_alloc_y_new( &sys, gsl_odeiv2_step_rk8pd, 1e-6, 1e-6, 0.0 ); int i; double t = 0.0; double t1 = 100.0; /* Initial condition: f = 0 with speed 0. */ double y[2] = {1.0, 0.0}; double dt = t1 / n_points; double datax[n_points]; double datay[n_points]; for (i = 0; i < n_points; i++) { double ti = i * dt; int status = gsl_odeiv2_driver_apply(d, &t, ti, y); if (status != GSL_SUCCESS) { fprintf(stderr, "error, return value=%d\n", status); break; } /* Get output. */ printf("%.5e %.5e %.5e\n", t, y[0], y[1]); datax[i] = y[0]; datay[i] = y[1]; } /* Cleanup. */ gsl_odeiv2_driver_free(d); /* Plot. */ if (argc > 1 && argv[1][0] == '1') { plsdev("xwin"); plinit(); plenv( gsl_stats_min(datax, 1, n_points), gsl_stats_max(datax, 1, n_points), gsl_stats_min(datay, 1, n_points), gsl_stats_max(datay, 1, n_points), 0, 1 ); plstring(n_points, datax, datay, "*"); plend(); } return EXIT_SUCCESS; }
/* Redraw routine for a display line. */ int wl_dldraw(displine *dline) { int oldcolumn; oldcolumn = dline->wcur_col; wl_setcur(dline, 1, 1); plstring(dline->dl_chars); wl_setcur(dline, 1, oldcolumn); return 0; }
bool plotNoiseStandardDeviation(const hoNDArray< std::complex<T> >& m, const std::vector<std::string>& coilStrings, const std::string& xlabel, const std::string& ylabel, const std::string& title, size_t xsize, size_t ysize, bool trueColor, hoNDArray<float>& plotIm) { try { size_t CHA = m.get_size(0); GADGET_CHECK_RETURN_FALSE(coilStrings.size() == CHA); hoNDArray<double> xd, yd, yd2; xd.create(CHA); yd.create(CHA); size_t c; for (c = 0; c < CHA; c++) { xd(c) = c+1; yd(c) = std::sqrt( std::abs(m(c, c)) ); } double maxY = Gadgetron::max(&yd); yd2 = yd; std::sort(yd2.begin(), yd2.end()); double medY = yd2(CHA / 2); // increase dot line to be 1 sigma ~= 33% double medRange = 0.33; if (maxY < medY*(1 + medRange)) { maxY = medY*(1 + medRange); } hoNDArray<unsigned char> im; im.create(3, xsize, ysize); Gadgetron::clear(im); plsdev("mem"); plsmem(im.get_size(1), im.get_size(2), im.begin()); plinit(); plfont(2); pladv(0); plvpor(0.15, 0.75, 0.1, 0.8); plwind(0, CHA+1, 0, maxY*1.05); plcol0(15); plbox("bcnst", 0.0, 0, "bcnstv", 0.0, 0); std::string gly; getPlotGlyph(0, gly); // circle plstring(CHA, xd.begin(), yd.begin(), gly.c_str()); // draw the median line pllsty(1); double px[2], py[2]; px[0] = 0; px[1] = CHA+1; py[0] = medY; py[1] = medY; plline(2, px, py); pllsty(2); py[0] = medY*(1 - medRange); py[1] = medY*(1 - medRange); plline(2, px, py); py[0] = medY*(1 + medRange); py[1] = medY*(1 + medRange); plline(2, px, py); plmtex("b", 3.2, 0.5, 0.5, xlabel.c_str()); plmtex("t", 2.0, 0.5, 0.5, title.c_str()); plmtex("l", 5.0, 0.5, 0.5, ylabel.c_str()); // draw the legend std::vector<PLINT> opt_array(CHA), text_colors(CHA), line_colors(CHA), line_styles(CHA), symbol_numbers(CHA), symbol_colors(CHA); std::vector<PLFLT> symbol_scales(CHA), line_widths(CHA), box_scales(CHA, 1); std::vector<const char*> symbols(CHA); PLFLT legend_width, legend_height; std::vector<const char*> legend_text(CHA); std::vector<std::string> legends(CHA); size_t n; for (n = 0; n < CHA; n++) { opt_array[n] = PL_LEGEND_SYMBOL; text_colors[n] = 15; line_colors[n] = 15; line_styles[n] = (n % 8 + 1); line_widths[n] = 0.2; symbol_colors[n] = 15; symbol_scales[n] = 0.75; symbol_numbers[n] = 1; symbols[n] = gly.c_str(); std::ostringstream ostr; ostr << n+1 << ":" << coilStrings[n]; legends[n] = ostr.str(); legend_text[n] = legends[n].c_str(); } pllegend(&legend_width, &legend_height, PL_LEGEND_BACKGROUND, PL_POSITION_OUTSIDE | PL_POSITION_RIGHT, 0.02, // x 0.0, // y 0.05, // plot_width 0, // bg_color 15, // bb_color 1, // bb_style 0, // nrow 0, // ncolumn CHA, // nlegend &opt_array[0], 0.05, // text_offset 0.5, // text_scale 1.0, // text_spacing 0.5, // text_justification &text_colors[0], (const char **)(&legend_text[0]), NULL, // box_colors NULL, // box_patterns &box_scales[0], // box_scales NULL, // box_line_widths &line_colors[0], &line_styles[0], &line_widths[0], &symbol_colors[0], &symbol_scales[0], &symbol_numbers[0], (const char **)(&symbols[0]) ); plend(); outputPlotIm(im, trueColor, plotIm); } catch (...) { GERROR_STREAM("Errors happened in plotNoiseStandardDeviation(...) ... "); return false; } return true; }
template <typename T> EXPORTGTPLPLOT bool plotCurves(const std::vector<hoNDArray<T> >& x, const std::vector<hoNDArray<T> >& y, const std::string& xlabel, const std::string& ylabel, const std::string& title, const std::vector<std::string>& legend, const std::vector<std::string>& symbols, size_t xsize, size_t ysize, T xlim[2], T ylim[2], bool trueColor, bool drawLine, hoNDArray<float>& plotIm) { try { GADGET_CHECK_RETURN_FALSE(x.size()>0); GADGET_CHECK_RETURN_FALSE(y.size()>0); GADGET_CHECK_RETURN_FALSE(x.size() == y.size()); T minX = xlim[0]; T maxX = xlim[1]; T minY = ylim[0]; T maxY = ylim[1]; plsdev("mem"); hoNDArray<unsigned char> im; im.create(3, xsize, ysize); Gadgetron::clear(im); plsmem(im.get_size(1), im.get_size(2), im.begin()); plinit(); plfont(2); pladv(0); if (legend.size() == x.size()) { plvpor(0.11, 0.75, 0.1, 0.9); } else { plvpor(0.15, 0.85, 0.1, 0.9); } T spaceX = 0.01*(maxX - minX); T spaceY = 0.05*(maxY - minY); plwind(minX - spaceX, maxX + spaceX, minY - spaceY, maxY + spaceY); plcol0(15); plbox("bgcnst", 0.0, 0, "bgcnstv", 0.0, 0); // int mark[2], space[2]; //mark[0] = 4000; //space[0] = 2500; //plstyl(1, mark, space); size_t num = x.size(); size_t n; hoNDArray<double> xd, yd; // draw lines for (n = 0; n < num; n++) { size_t N = y[n].get_size(0); xd.copyFrom(x[n]); yd.copyFrom(y[n]); if (drawLine) { int c; getPlotColor(n, c); plcol0(c); pllsty(n % 8 + 1); plline(N, xd.begin(), yd.begin()); } std::string gly; if(symbols.size()>n) { gly = symbols[n]; } else getPlotGlyph(n, gly); plstring(N, xd.begin(), yd.begin(), gly.c_str()); } plcol0(15); plmtex("b", 3.2, 0.5, 0.5, xlabel.c_str()); plmtex("t", 2.0, 0.5, 0.5, title.c_str()); plmtex("l", 5.0, 0.5, 0.5, ylabel.c_str()); // draw the legend if (legend.size() == x.size()) { std::vector<PLINT> opt_array(num), text_colors(num), line_colors(num), line_styles(num), symbol_numbers(num), symbol_colors(num); std::vector<PLFLT> symbol_scales(num), line_widths(num), box_scales(num, 1); std::vector<std::string> glyphs(num); std::vector<const char*> symbols(num); PLFLT legend_width, legend_height; std::vector<const char*> legend_text(num); for (n = 0; n < num; n++) { int c; getPlotColor(n, c); getPlotGlyph(n, glyphs[n]); opt_array[n] = PL_LEGEND_SYMBOL | PL_LEGEND_LINE; text_colors[n] = 15; line_colors[n] = c; line_styles[n] = (n%8+1); line_widths[n] = 0.2; symbol_colors[n] = c; symbol_scales[n] = 0.75; symbol_numbers[n] = 1; symbols[n] = glyphs[n].c_str(); legend_text[n] = legend[n].c_str(); } pllegend(&legend_width, &legend_height, PL_LEGEND_BACKGROUND, PL_POSITION_OUTSIDE | PL_POSITION_RIGHT | PL_POSITION_TOP, 0.02, // x 0.0, // y 0.05, // plot_width 0, // bg_color 15, // bb_color 1, // bb_style 0, // nrow 0, // ncolumn num, // nlegend &opt_array[0], 0.05, // text_offset 0.35, // text_scale 1.0, // text_spacing 0.5, // text_justification &text_colors[0], (const char **)(&legend_text[0]), NULL, // box_colors NULL, // box_patterns &box_scales[0], // box_scales NULL, // box_line_widths &line_colors[0], &line_styles[0], &line_widths[0], &symbol_colors[0], &symbol_scales[0], &symbol_numbers[0], (const char **)(&symbols[0]) ); } plend(); outputPlotIm(im, trueColor, plotIm); } catch (...) { GERROR_STREAM("Errors happened in plotCurves(xlim, ylim) ... "); return false; } return true; }
void plot1( int type, const char *x_label, const char *y_label, const char *alty_label, const char * legend_text[], const char *title_label, const char *line_label ) { int i; static PLFLT freql[101], ampl[101], phase[101]; PLFLT f0, freq; PLINT nlegend = 2; PLINT opt_array[2]; PLINT text_colors[2]; PLINT line_colors[2]; PLINT line_styles[2]; PLFLT line_widths[2]; PLINT symbol_numbers[2], symbol_colors[2]; PLFLT symbol_scales[2]; const char *symbols[2]; PLFLT legend_width, legend_height; pladv( 0 ); // Set up data for log plot f0 = 1.0; for ( i = 0; i <= 100; i++ ) { freql[i] = -2.0 + i / 20.0; freq = pow( 10.0, freql[i] ); ampl[i] = 20.0 * log10( 1.0 / sqrt( 1.0 + pow( ( freq / f0 ), 2. ) ) ); phase[i] = -( 180.0 / M_PI ) * atan( freq / f0 ); } plvpor( 0.15, 0.85, 0.1, 0.9 ); plwind( -2.0, 3.0, -80.0, 0.0 ); // Try different axis and labelling styles. plcol0( 1 ); switch ( type ) { case 0: plbox( "bclnst", 0.0, 0, "bnstv", 0.0, 0 ); break; case 1: plbox( "bcfghlnst", 0.0, 0, "bcghnstv", 0.0, 0 ); break; } // Plot ampl vs freq plcol0( 2 ); plline( 101, freql, ampl ); plcol0( 2 ); plptex( 1.6, -30.0, 1.0, -20.0, 0.5, line_label ); // Put labels on plcol0( 1 ); plmtex( "b", 3.2, 0.5, 0.5, x_label ); plmtex( "t", 2.0, 0.5, 0.5, title_label ); plcol0( 2 ); plmtex( "l", 5.0, 0.5, 0.5, y_label ); // For the gridless case, put phase vs freq on same plot if ( type == 0 ) { plcol0( 1 ); plwind( -2.0, 3.0, -100.0, 0.0 ); plbox( "", 0.0, 0, "cmstv", 30.0, 3 ); plcol0( 3 ); plline( 101, freql, phase ); plstring( 101, freql, phase, "#(728)" ); plcol0( 3 ); plmtex( "r", 5.0, 0.5, 0.5, alty_label ); } // Draw a legend // First legend entry. opt_array[0] = PL_LEGEND_LINE; text_colors[0] = 2; line_colors[0] = 2; line_styles[0] = 1; line_widths[0] = 1.; // note from the above opt_array the first symbol (and box) indices // do not have to be specified // Second legend entry. opt_array[1] = PL_LEGEND_LINE | PL_LEGEND_SYMBOL; text_colors[1] = 3; line_colors[1] = 3; line_styles[1] = 1; line_widths[1] = 1.; symbol_colors[1] = 3; symbol_scales[1] = 1.; symbol_numbers[1] = 4; symbols[1] = "#(728)"; // from the above opt_arrays we can completely ignore everything // to do with boxes. plscol0a( 15, 32, 32, 32, 0.70 ); pllegend( &legend_width, &legend_height, PL_LEGEND_BACKGROUND | PL_LEGEND_BOUNDING_BOX, 0, 0.0, 0.0, 0.10, 15, 1, 1, 0, 0, nlegend, opt_array, 1.0, 1.0, 2.0, 1., text_colors, (const char **) legend_text, NULL, NULL, NULL, NULL, line_colors, line_styles, line_widths, symbol_colors, symbol_scales, symbol_numbers, (const char **) symbols ); }