#!/usr/bin/perl
#

  $output_file = "drag_example.ps";
  $term_options = "postscript color 'Helvetica' 18 ";

  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot left \n";
  printf CMDFILE "set xlabel 'Time (seconds) ' \n";
  printf CMDFILE "set ylabel 'Velocity (m/s)' \n";
  printf CMDFILE "set title 'An object falling through air' \n";
  
  $cmd = "plot [0:100][-500:0] -100*(1.0 - exp(-x/10)) ";
  $cmd = $cmd . "   with lines lw 5 t 'k = 0.10' ";
  $cmd .= " , ";
  $cmd .= " -200*(1.0 - exp(-x/20)) ";
  $cmd .= "   with lines lw 5 t 'k = 0.05' ";
  $cmd .= " , ";
  $cmd .= " -400*(1.0 - exp(-x/40)) ";
  $cmd .= "   with lines lw 5 t 'k = 0.02' ";
  $cmd .= " , ";
  $cmd .= " -9.8*x ";
  $cmd .= "   with lines lc -1 lw 5 t 'k = 0' ";
  

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


exit 0;
