#!/usr/bin/perl
#
#

$mypi = 3.14159;

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

  # get the arguments
  $output_file = "square_ex_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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = square wave ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:2][-1.0:1.5] './test_four_b.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 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 square wave function over a single period
#     plus the n=0 terms
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "square_ex_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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = square wave ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:2][-1.0:1.5] './test_four_b.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:7 ";
  $cmd .= "        with lines ls 2 lw 4 t 'A_0 term' ";

  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 square wave over a single period
#     plus the n=0, n=1 terms
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "square_ex_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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = 3 + sin(2 {/Symbol p} t / 5) ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:2][-1.0:1.5] './test_four_b.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:7 ";
  $cmd .= "        with lines ls 2 lw 4 t 'A_0 term' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:16 ";
  $cmd .= "        with lines ls 3 lw 3 t 'B_1 term' ";


  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 square wave function over a single period
#     plus the sum
# 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "square_ex_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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = square wave ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:2][-1.0:1.5] './test_four_b.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:7 ";
#  $cmd .= "        with lines ls 2 lw 4 t 'A_0 term' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:16 ";
#  $cmd .= "        with lines ls 3 lw 3 t 'B_1 term' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:18 ";
  $cmd .= "        with lines ls -1 lw 2  t 'sum n=0 to n=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 square wave function over a single period
#     plus the n=0, n=1, n=3 
# 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "square_ex_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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = square wave ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:2][-1.0:1.5] './test_four_b.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:7 ";
  $cmd .= "        with lines ls 2 lw 4 t 'A_0 term' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:16 ";
  $cmd .= "        with lines ls 3 lw 3 t 'B_1 term' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:30 ";
  $cmd .= "        with lines ls 4 lw 3 t 'B_3 term' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:18 ";
#  $cmd .= "        with lines ls -1 lw 2  t 'sum' ";


  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 square wave function over a single period
#     plus the total of n=0 to n=3 terms
# 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "square_ex_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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = square wave ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:2][-1.0:1.5] './test_four_b.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:7 ";
#  $cmd .= "        with lines ls 2 lw 4 t 'A_0 term' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:16 ";
#  $cmd .= "        with lines ls 3 lw 3 t 'B_1 term' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:30 ";
#  $cmd .= "        with lines ls 4 lw 3 t 'B_3 term' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:32 ";
  $cmd .= "        with lines ls -1 lw 2  t 'sum n=0 to n=3' ";


  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 square wave function over a single period
#     plus the n=0, n=1, n=3, n=5
# 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "square_ex_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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = square wave ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:2][-1.0:1.5] './test_four_b.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:7 ";
  $cmd .= "        with lines ls 2 lw 4 t 'A_0 term' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:16 ";
  $cmd .= "        with lines ls 3 lw 3 t 'B_1 term' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:30 ";
  $cmd .= "        with lines ls 4 lw 3 t 'B_3 term' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:44 ";
  $cmd .= "        with lines ls 5 lw 3 t 'B_5 term' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:18 ";
#  $cmd .= "        with lines ls -1 lw 2  t 'sum' ";


  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 square wave function over a single period
#     plus the total of n=0 to n=5 terms
# 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "square_ex_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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = square wave ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:2][-1.0:1.5] './test_four_b.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:7 ";
#  $cmd .= "        with lines ls 2 lw 4 t 'A_0 term' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:16 ";
#  $cmd .= "        with lines ls 3 lw 3 t 'B_1 term' ";
#  $cmd .= " , ";
#  $cmd .= "                        './test_four_b.out' index 0 using 2:30 ";
#  $cmd .= "        with lines ls 4 lw 3 t 'B_3 term' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using 2:46 ";
  $cmd .= "        with lines ls -1 lw 2  t 'sum n=0 to n=5' ";


  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 Fourier series approximations to a square wave
#     for various numbers of terms
# 
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "square_ex_i.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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = square wave ' \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:2][-0.2:1.2] './square_fit_5.out' index 0 using 3:7 ";
  $cmd .= "        with lines ls 1 lw 4 t 'up to n=5' ";
  $cmd .= " , ";
  $cmd .= "                        './square_fit_10.out' index 0 using 3:7 ";
  $cmd .= "        with lines ls 2 lw 4 t 'up to n=10' ";
  $cmd .= " , ";
  $cmd .= "                        './square_fit_20.out' index 0 using 3:7 ";
  $cmd .= "        with lines ls 3 lw 4 t 'up to n=20' ";
  $cmd .= " , ";
  $cmd .= "                        './square_fit_50.out' index 0 using 3:7 ";
  $cmd .= "        with lines ls -1 lw 4 t 'up to n=50' ";


  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 magnitudes of Fourier coefficients for
#     a square wave
# 
  
  my($output_file, $term_options);

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


  # 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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";

  printf CMDFILE "set multiplot layout 2,1 title 'Fourier components of a square wave' \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 'Cosine coeff A  '  \n";
  printf CMDFILE "set xlabel 'Order n' \n";

  $cmd = "plot [-1:5.5][-1.2:1.2] './calc_square_coeffs.out' index 0 using 1:3 ";
  $cmd .= "        with impulse ls 1 lw 15 t '' ";

  printf CMDFILE "$cmd \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 'Sine coeff B  '  \n";
  printf CMDFILE "set xlabel 'Order n' \n";

  $cmd = "plot [-1:5.5][-1.2:1.2] './calc_square_coeffs.out' index 0 using 1:5 ";
  $cmd .= "        with impulse ls 3 lw 15 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "unset multiplot \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 magnitudes of Fourier coefficients for
#     a square wave
#     Extend plot to larger values of 'n',
#        and show only the cosine terms,
#        and show only positive values
# 
  
  my($output_file, $term_options);

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


  # 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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'Sine coeff B  '  \n";
  printf CMDFILE "set xlabel 'Order n' \n";
  printf CMDFILE "set title 'Fourier components of a square wave ' \n";

  $cmd = "plot [-1:30][-0.2:1.0] './calc_square_coeffs.out' index 0 using 1:5 ";
  $cmd .= "        with impulse ls 3 lw 15 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "unset multiplot \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 magnitudes of Fourier coefficients for
#     a square wave
#     use frequency on the x-axis, not order
# 
  
  my($output_file, $term_options);

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


  # 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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' lw 3  \n";
  printf CMDFILE "set style line 5 lt rgb 'violet' lw 3  \n";

  printf CMDFILE "set multiplot layout 2,1 title 'Fourier components of a square wave' \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 'Cosine coeff A  '  \n";
  printf CMDFILE "set xlabel 'Frequency (rad/s)' \n";

  $cmd = "plot [-1:45.5][-1.2:1.2] './calc_square_coeffs.out' index 0 using (\$1*2.0*$mypi):3 ";
  $cmd .= "        with impulse ls 1 lw 15 t '' ";

  printf CMDFILE "$cmd \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 'Sine coeff B  '  \n";
  printf CMDFILE "set xlabel 'Frequency (rad/s)' \n";

  $cmd = "plot [-1:45.5][-1.2:1.2] './calc_square_coeffs.out' index 0 using (\$1*2.0*$mypi):5 ";
  $cmd .= "        with impulse ls 3 lw 15 t '' ";

  printf CMDFILE "$cmd \n";


  printf CMDFILE "unset multiplot \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 square function over 4 periods
  
  my($output_file, $term_options);

  # get the arguments
  $output_file = "square_ex_a4.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 'sea-green' lw 3  \n";
  printf CMDFILE "set style line 3 lt rgb 'blue' lw 3  \n";
  printf CMDFILE "set style line 4 lt rgb 'dark-orange' 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 'f(t) = square wave ' \n";
  printf CMDFILE "set arrow 1 from 2,0 to 2,1 nohead ls 1 lw 6  \n";
  printf CMDFILE "set arrow 2 from 4,0 to 4,1 nohead ls 1 lw 6  \n";
  printf CMDFILE "set arrow 3 from 6,0 to 6,1 nohead ls 1 lw 6  \n";
#  printf CMDFILE "set label  '{/Symbol w_0} ' at 8.8,-0.5 \n";

  $cmd = "plot [0:8][-1.0:1.5] './test_four_b.out' index 0 using 2:4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using (\$2+2):4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using (\$2+4):4 ";
  $cmd .= "        with lines ls 1 lw 6 t '' ";
  $cmd .= " , ";
  $cmd .= "                        './test_four_b.out' index 0 using (\$2+6):4 ";
  $cmd .= "        with lines ls 1 lw 6 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;
