tst_cplx.c


/*
** Program TST_CPLX
**
** Illustrates use of various complex number functions.  You may
** find these complex routines useful in other course work.
**
** Examines response of series R = 1k followed by parallel
** combination of C = 1590 pFd and L = 16.0 uHy at 100 points
** over the range of 750 kHz - 1250 kHz
**
** Note that the project file must consist of
**	a:cmplx.c
**	a:tst_cplx.c
**
** copyright P. H. Anderson, MSU, 12 Mar 91; 13 April 95; 5 May 96
/**********/

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include "a:cplx.h"

#define START_FREQ 0.75e6
#define END_FREQ  1.25e6

FILE *f1;

main()
{
   int n;
   float freq, w;

   float C=1590e-12, L=16e-6, R=1e3;

   struct Complex z1, z2, z3, z_para, z_tot, v_in, v_out, ratio;

   clrscr();
   f1 = fopen ("out.dta", "wt");

   for (n=0; n<100; n++)
   {
      freq = START_FREQ +  (END_FREQ - START_FREQ) * n/100;
      w = 2.0 * 3.14159 * freq;

      v_in.r = 1.0;   v_in.i = 0.0;  /* source defined */

      z1.r = 0.0;  z1.i = -1/(w*C);
      z2.r = 0.0;  z2.i = w*L;	    /* components defined */
      z3.r = R;    z3.i = 0.0;

      z_para = C_para (z1, z2);
      z_tot = C_add (z_para, z3);

	 ratio = C_div (z_para, z_tot);  /* voltage division */
      v_out = C_mul (ratio, v_in);

      printf ("%.4e\t%.4e\n", freq, C_mag(v_out));
      fprintf (f1, "%.4e,%.4e\n", freq, C_mag(v_out));
   }
   fclose (f1);
}