//
// Paul Noga
// A program that calculates the complex ratio, Vout/Vin,
// for given values of R1, R2, C1, C2 and a range of frequencies.
//
#include
#include
/* declare 'complex' structure */
struct complex
{
float r; /* real part */
float i; /* imaginary part */
};
/* function for adding complex numbers */
struct complex comp_add (struct Complex a, struct Complex b)
{
struct complex result;
result.r = a.r + b.r;
result.i = a.i + b.i;
return(result);
}
/* function for subtracting complex numbers */
struct complex comp_sub (struct complex a, struct complex b)
{
struct complex result;
result.r = a.r - b.r;
result.i = a.i - b.i;
return(result);
}
/* function for multiplying complex numbers */
struct complex comp_multi (struct complex a, struct complex b)
{
struct complex result;
result.r = a.r*b.r - a.i*b.i;
result.i = a.r*b.i + b.r*a.i;
return(result);
}
/* function for dividing complex numbers */
struct complex comp_divide (struct complex a, struct complex b)
{
struct complex result;
result.r = (a.r*b.r + a.i*b.i)/(b.r*b.r + b.i*b.i);
result.i = (a.i*b.r - a.r-b.i)/(b.r*b.r + b.i*b.i);
return(result);
}
int main()
{
/* declare variables */
double r1; /* value of resistor r1 */
double r2; /* value of resistor r2 */
double c1; /* value of capacitor c1 */
double c2; /* value of capacitor c2 */
double freq_start; /* first frequency value (hz) */
double freq_end; /* final frequency value (hz) */
double freq_interval; /* intervals between frequency steps (hz) */
double freq_num; /* number of frequencies in list */
double freq; /* frequency values in hertz */
double omega; /* frequency values in rad. per second */
int i = 0; /* counter */
char input_file[20]; /* name of input file */
char output_file[20]; /* name of output file */
FILE *ifp;
FILE *ofp;
/* declaration of arrays */
double array_f[100]; /* list of omega values */
/* input values for components */
printf("Welcome to my program for calculating the complex ratio Vout/Vin for two parallel Resistor-Capacitor components in series at different frequencies\n\n");
printf("What is the value of resistor R1 (ohms) ? ");
scanf("%lf", &r1);
printf("What is the value of capacitor C1 (farads) ? ");
scanf("%lf", &c1);
printf("What is the value of resistor R2 (ohms) ? ");
scanf("%lf", &r2);
printf("What is the value of capacitor C2 (farads) ? ");
scanf("%lf", &c2);
printf("What is your first frequency value (Hz) ? ");
scanf("%lf", &freq_start);
printf("What is your final frequency value (Hz) ? ");
scanf("%lf", &freq_end);
printf("What frequency intervals would you like your results (Hz) ? ");
scanf("%lf", &freq_interval);
/* name input file */
printf("Under what file name would you like to save these values (*.txt) ? ");
scanf("%s", input_file);
/* calculate number of frequencies in list */
freq_num = (freq_end - freq_start)/freq_interval;
/* write input file */
ofp=fopen(input_file, "w");
fprintf(ofp, "%10.2e ohms\n", r1);
fprintf(ofp, "%10.2e ohms\n", c1);
fprintf(ofp, "%10.2e ohms\n", r2);
fprintf(ofp, "%10.2e ohms\n", c2);
/* write frequency values loop */
for(freq=freq_start; freq<=freq_end; freq=freq+freq_interval, i++)
{
/* convert hertz into angular frequency */
omega=freq*2*PI;
array_f[i] = omega;
fprintf(ofp, "%.2lf", array_f[i]);
}
/* close input file */
fclose(ofp);
/* get values from input file */
ifp=fopen("input.txt", "r");
fscanf(ifp, "%lf", &r1);
fscanf(ifp, "%lf", &c1);
fscanf(ifp, "%lf", &r2);
fscanf(ifp, "%lf", &c2);
for(i=0; i<=freq_num; i++);
{
fscanf(ifp, "%lf", &array_f[i]);
}
/* name output file */
printf("What would you like to name the output file (*.txt) ? ");
scanf("%s", output_file);
/* write to output file */
ofp=fopen(output_file, "w");
for(i=0; i