#!/usr/bin/perl
#
#

##########################################
#  plot a function over a long time
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "test_four_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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set ylabel 'Value  '  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  printf CMDFILE "set title '' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [-0.5:40.5][-1.2:1.2] './test_four_long.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t 'f(t)' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four.out' index 0 using 2:10 ";
#  $cmd .= "        with lines ls -1 lw 3 t 'sin({/Symbol w_1})' ";

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



##########################################
#  plot a function over one period
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "test_four_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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set ylabel 'Value  '  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  printf CMDFILE "set title '' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [-0.5:10.5][-1.2:1.2] './test_four.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t 'f(t)' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four.out' index 0 using 2:10 ";
#  $cmd .= "        with lines ls -1 lw 3 t 'sin({/Symbol w_1})' ";

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



##########################################
#  plot a function over one period
#    and the first Fourier component
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "test_four_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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set ylabel 'Value  '  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  printf CMDFILE "set title '' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [-0.5:10.5][-1.2:1.2] './test_four.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t 'f(t)' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four.out' index 0 using 2:12 ";
  $cmd .= "        with lines ls 2 lw 4 t 'B_1 sin({/Symbol w_1} 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`;
  }


##########################################
#  plot a function over one period
#    and the first two Fourier components 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "test_four_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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set ylabel 'Value  '  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  printf CMDFILE "set title '' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [-0.5:10.5][-1.2:1.2] './test_four.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t 'f(t)' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four.out' index 0 using 2:12 ";
  $cmd .= "        with lines ls 2 lw 4 t 'B_1 sin({/Symbol w_1} t)' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four.out' index 0 using 2:17 ";
  $cmd .= "        with lines ls 3 lw 3 t 'B_2 sin({/Symbol w_2} 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`;
  }



##########################################
#  plot a function over one period
#    and the SUM of the first two Fourier components 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "test_four_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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set ylabel 'Value  '  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  printf CMDFILE "set title '' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [-0.5:10.5][-1.2:1.2] './test_four.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t 'f(t)' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four.out' index 0 using 2:(\$12+\$17) ";
  $cmd .= "        with lines ls -1 lw 3 t 'sum of 2 terms' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four.out' index 0 using 2:17 ";
#  $cmd .= "        with lines ls 2 lw 3 t 'B_2 sin({/Symbol w_2} 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`;
  }



##########################################
#  plot a function over one period
#    and the first three Fourier components 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "test_four_f.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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set ylabel 'Value  '  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  printf CMDFILE "set title '' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [-0.5:10.5][-1.2:1.2] './test_four.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t 'f(t)' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four.out' index 0 using 2:12 ";
  $cmd .= "        with lines ls 2 lw 4 t 'B_1 sin({/Symbol w_1} t)' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four.out' index 0 using 2:17 ";
  $cmd .= "        with lines ls 3 lw 3 t 'B_2 sin({/Symbol w_2} t)' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four.out' index 0 using 2:22 ";
  $cmd .= "        with lines ls 4 lw 3 t 'B_3 sin({/Symbol w_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`;
  }



##########################################
#  plot a function over one period
#    and the SUM of the first three Fourier components 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "test_four_g.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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set ylabel 'Value  '  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  printf CMDFILE "set title '' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [-0.5:10.5][-1.2:1.2] './test_four.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t 'f(t)' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four.out' index 0 using 2:(\$12+\$17+\$22) ";
  $cmd .= "        with lines ls -1 lw 3 t 'sum of 3 terms' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four.out' index 0 using 2:17 ";
#  $cmd .= "        with lines ls 2 lw 3 t 'B_2 sin({/Symbol w_2} 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`;
  }




##########################################
#  plot a function over one period
#    and the SUM of the first five Fourier components 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "test_four_h.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";

  # revert to some oldish default colors
  printf CMDFILE "set style line 1 lt rgb 'red' lw 3  \n";
  printf CMDFILE "set style line 2 lt rgb 'green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'cyan' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";


  printf CMDFILE "set grid \n";
  printf CMDFILE "set key bot right \n";
#  printf CMDFILE "set xzeroaxis  lw 3.5 \n";
#  printf CMDFILE "set yzeroaxis  lw 3.5 \n";
  printf CMDFILE "set ylabel 'Value  '  \n";
  printf CMDFILE "set xlabel 'Time (s)' \n";
  printf CMDFILE "set title '' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [-0.5:10.5][-1.2:1.2] './test_four.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t 'f(t)' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four.out' index 0 using 2:(\$12+\$17+\$22+\$27+\$32) ";
  $cmd .= "        with lines ls -1 lw 3 t 'sum of 5 terms' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four.out' index 0 using 2:17 ";
#  $cmd .= "        with lines ls 2 lw 3 t 'B_2 sin({/Symbol w_2} 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`;
  }












exit 0;
