#!/usr/bin/perl
#



############################################################################
# Make a graph showing force as a function of displacement for a spring
#
#
  
  # create the data file 
  $data_file = "./spring_force.dat";
  open(DATA_FILE, ">$data_file") || die("can't open file $data_file");
  $k = 6.0;
  for ($x = -3; $x <= 3; $x += 0.5) {
    $y = -$k*$x;
    printf DATA_FILE " %8.3f %8.3f \n", $x, $y;
  }
  close(DATA_FILE);
  my($output_file, $term_options);


  $output_file = "./spring_force_a.ps";
  $term_options = "postscript color enhanced '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 ylabel 'Force exerted by spring (N)' \n";
  printf CMDFILE "set xlabel 'Displacement from spring rest position (m)' \n";
  printf CMDFILE "set title '' \n";
  
  $cmd = "plot [-4:4][-23:23]  '$data_file' using 1:2  ";
  $cmd .=  "    with points lc 1 lw 2 ps 2 pt 3  t '' ";

  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`;
  }


############################################################################
# Make a simple graph showing x(t) 
#
#
  
  # create the data file 
 if (0 == 1) {
  $data_file = "./spring_force.dat";
  open(DATA_FILE, ">$data_file") || die("can't open file $data_file");
  $k = 6.0;
  for ($x = -3; $x <= 3; $x += 0.5) {
    $y = -$k*$x;
    printf DATA_FILE " %8.3f %8.3f \n", $x, $y;
  }
  close(DATA_FILE);
 }

  my($output_file, $term_options);

  $output_file = "./illus_coeffs_a.ps";
  $term_options = "postscript color enhanced '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 ylabel 'displacement of block (m)' \n";
  printf CMDFILE "set xlabel 'time (s)' \n";
  printf CMDFILE "set title 'Position of an object in SHM ' \n";

  # amplitude (m)
  $a_coeff = 0.60;
  # angular frequency (rad/s)
  $omega = 1.0;
  # phase offset (rad)
  $phase = 0.0;
  
  $cmd = "plot [-10:10][-1.2:1.2] $a_coeff*sin($omega*x + $phase)   ";
  $cmd .=  "    with lines lc 1 lw 4   t '' ";

  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`;
  }


############################################################################
# Make a simple graph showing x(t), but also a version 
#   with modified amplitude
#
#
  
  # create the data file 
 if (0 == 1) {
  $data_file = "./spring_force.dat";
  open(DATA_FILE, ">$data_file") || die("can't open file $data_file");
  $k = 6.0;
  for ($x = -3; $x <= 3; $x += 0.5) {
    $y = -$k*$x;
    printf DATA_FILE " %8.3f %8.3f \n", $x, $y;
  }
  close(DATA_FILE);
 }

  my($output_file, $term_options);

  $output_file = "./illus_coeffs_b.ps";
  $term_options = "postscript color enhanced '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 ylabel 'displacement of block (m)' \n";
  printf CMDFILE "set xlabel 'time (s)' \n";
  printf CMDFILE "set title 'Position of an object in SHM ' \n";

  # amplitude (m)
  $a_coeff = 0.60;
  # angular frequency (rad/s)
  $omega = 1.0;
  # phase offset (rad)
  $phase = 0.0;
  
  $cmd = "plot [-10:10][-1.2:1.2] $a_coeff*sin($omega*x + $phase)   ";
  $cmd .=  "    with lines lc 1 lw 4   t '' ";
  $cmd .=  " , ";
  $cmd .=  "                      1.5*$a_coeff*sin($omega*x + $phase)   ";
  $cmd .=  "    with lines lc 3 lw 4   t 'increase A' ";


  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`;
  }


############################################################################
# Make a simple graph showing x(t), but also a version 
#   with modified angular frequency
#
#
  
  # create the data file 
 if (0 == 1) {
  $data_file = "./spring_force.dat";
  open(DATA_FILE, ">$data_file") || die("can't open file $data_file");
  $k = 6.0;
  for ($x = -3; $x <= 3; $x += 0.5) {
    $y = -$k*$x;
    printf DATA_FILE " %8.3f %8.3f \n", $x, $y;
  }
  close(DATA_FILE);
 }

  my($output_file, $term_options);

  $output_file = "./illus_coeffs_c.ps";
  $term_options = "postscript color enhanced '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 ylabel 'displacement of block (m)' \n";
  printf CMDFILE "set xlabel 'time (s)' \n";
  printf CMDFILE "set title 'Position of an object in SHM ' \n";

  # amplitude (m)
  $a_coeff = 0.60;
  # angular frequency (rad/s)
  $omega = 1.0;
  # phase offset (rad)
  $phase = 0.0;
  
  $cmd = "plot [-10:10][-1.2:1.2] $a_coeff*sin($omega*x + $phase)   ";
  $cmd .=  "    with lines lc 1 lw 4   t '' ";
  $cmd .=  " , ";
  $cmd .=  "                      $a_coeff*sin(1.3*$omega*x + $phase)   ";
  $cmd .=  "    with lines lc 3 lw 4   t 'increase {/Symbol w}' ";


  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`;
  }



############################################################################
# Make a simple graph showing x(t), but also a version 
#   with modified phase offset (positive)
#
#
  
  # create the data file 
 if (0 == 1) {
  $data_file = "./spring_force.dat";
  open(DATA_FILE, ">$data_file") || die("can't open file $data_file");
  $k = 6.0;
  for ($x = -3; $x <= 3; $x += 0.5) {
    $y = -$k*$x;
    printf DATA_FILE " %8.3f %8.3f \n", $x, $y;
  }
  close(DATA_FILE);
 }

  my($output_file, $term_options);

  $output_file = "./illus_coeffs_d.ps";
  $term_options = "postscript color enhanced '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 ylabel 'displacement of block (m)' \n";
  printf CMDFILE "set xlabel 'time (s)' \n";
  printf CMDFILE "set title 'Position of an object in SHM ' \n";
  printf CMDFILE "set arrow 1 from 1.57,0.75 to 0.57,0.75 ls -1 lw 2 \n";

  # amplitude (m)
  $a_coeff = 0.60;
  # angular frequency (rad/s)
  $omega = 1.0;
  # phase offset (rad)
  $phase = 0.0;
  
  $cmd = "plot [-10:10][-1.2:1.2] $a_coeff*sin($omega*x + $phase)   ";
  $cmd .=  "    with lines lc 1 lw 4   t '' ";
  $cmd .=  " , ";
  $cmd .=  "                      $a_coeff*sin($omega*x + 1.0+$phase)   ";
  $cmd .=  "    with lines lc 3 lw 4   t 'add positive {/Symbol j}' ";


  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`;
  }


############################################################################
# Make a simple graph showing x(t), but also a version 
#   with modified phase offset (negative)
#
#
  
  # create the data file 
 if (0 == 1) {
  $data_file = "./spring_force.dat";
  open(DATA_FILE, ">$data_file") || die("can't open file $data_file");
  $k = 6.0;
  for ($x = -3; $x <= 3; $x += 0.5) {
    $y = -$k*$x;
    printf DATA_FILE " %8.3f %8.3f \n", $x, $y;
  }
  close(DATA_FILE);
 }

  my($output_file, $term_options);

  $output_file = "./illus_coeffs_e.ps";
  $term_options = "postscript color enhanced '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 ylabel 'displacement of block (m)' \n";
  printf CMDFILE "set xlabel 'time (s)' \n";
  printf CMDFILE "set title 'Position of an object in SHM ' \n";
  printf CMDFILE "set arrow 1 from 1.57,0.75 to 2.57,0.75 ls -1 lw 2 \n";

  # amplitude (m)
  $a_coeff = 0.60;
  # angular frequency (rad/s)
  $omega = 1.0;
  # phase offset (rad)
  $phase = 0.0;
  
  $cmd = "plot [-10:10][-1.2:1.2] $a_coeff*sin($omega*x + $phase)   ";
  $cmd .=  "    with lines lc 1 lw 4   t '' ";
  $cmd .=  " , ";
  $cmd .=  "                      $a_coeff*sin($omega*x + (-1.0+$phase))   ";
  $cmd .=  "    with lines lc 3 lw 4   t 'add negative {/Symbol j}' ";


  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;
