Вы находитесь на странице: 1из 4

IC250 Laboratory Assignment Template

B. D. Chaudhary
August 20, 2015

IC250 Programming and Data Structure Practicum


Lab Assignment No. 2

1 Introduction/Problem Context
In this assignment we will plot the frequency response of a series RLC circuit. You will be using arrays
(static or dynamic) and the complex.h header file for complex number declaration and arithmetic.
The inputs are the Resistance, inductance and capacitance values.
The required outputs include
1. Plot of impedance as a function of frequency.
2. Plot of the voltage waveform across R, L and C for the series circuit.

2 Task Description
You are required to write a C program which reads in the inputs given in section 1 and generate the plots
mentioned. Write a programs which generates impedance vs. frequency curve. Use gnuplot for plotting
the curves.
As you would have seen in the IC160 course an inductor and capacitor when connected together
can give rise to resonance. Resonance is the state when the magnitudes of the capacitive and inductive
impedances are the same and occurs at the frequency
1
0 = .
LC

(1)

The behaviour at resonance differs for parallel and series circuit and is briefly described below for series
circuits.

3 Series resonance
R

V = Vm sin( t)
Figure 1: Series RLC circuit
Answer the following questions. Work out these answers by hand. The input to the circuit is sinusoidal
as shown in Fig. 1.
1. Find the impedance of the circuit.
2

2. Find the resonant frequency if L = 0.001H and C = 0.001F.


3. Find the expression for voltage across each of the elements (resistance, capacitance and inductance).
Answers to the above questions will help you for writing the programs below.

3.1

Part 1

1. Write a program for the following. Represent all impedances (except resistance), currents and
voltages as complex numbers. Use complex.h - use the complex variable multiplication available
in this header file for phasor multiplication. Plot the magnitude of the impedance as a function of
frequency.
Procedure
1. For a given input, determine the value of 0 .
2. The impedence Z = R + j L +

1
j C .

3. From this, compute the magnitude of Z. Use the cabs() function.


4. You can also compute the phase of Z. Use carg() function.
5. For various values of , determine the magnitude of Z. Take care that the values of taken
includes the resonant frequency 0 . For instance, you can take 100 values below and above the
value of 0 . Store the values in an array. Write the array to file.
6. Plot the magnitude of Z versus , using gnuplot. Commands for this are given below.

3.2

Part 2

1. Find the current I and hence the voltage across each of the elements at the resonant frequency. Plot
all the waveforms in the same graph.
2. Plot the voltage waveforms at a frequency (1) half the resonant frequency (2) 1.5 times the resonant
frequency.
Procedure
1. Determine the current I in the circuit. This can be done as I = V /Z, where I,V and Z are complex
quantities. From this, determine the voltages across each of the elements (namely Vr ,Vc and Vl )
when = 0 . The voltages have to be computed across t.
2. Vary t across a range of values an determine the voltages. The time period T = 2 / . Vary t across
three time periods with an increment of one-thousandths of T .
3. Store the voltages in three arrays and write as a multi-column file. Plot the above voltages using
gnuplot.

3.3

GNUPLOT

Read the gnuplot documentation. You can go through the following links
http://people.duke.edu/~hpgavin/gnuplot.html
http://www.gnuplot.info/docs/tutorial.pdf
In this section you will see how to call gnuplot from a C program. Gnuplot is used for plotting graphs.
The data to be plotted is written in a file. For example to plot the impedance as a function of frequency
you need to create a data file with two columns separated by a space. The first column contains frequency
and second the impedance values.
The commands for gnuplot are stored in a character array. The data generated is written into a file
which gnuplot will access and plot. The code segment is given below (taken from
http://stackoverflow.com/questions/3521209/making-c-code-plot-a-graph-automatically).
char * commandsForGnuplot1[] = {"set term x11 1","set title \"Impedance vs
frequency\"", "set key outside", "plot data.temp with line"};

char * commandsForGnuplot[] = {"set term x11 0", "set title \"Voltage waveforms\"",
"set key outside", "plot for [col=2:4] data.temp using 0:col with line"};

FILE * temp = fopen("data.temp", "w");

The commands for gnuplot are written in double quotes with each command separated by a comma.
The set term command is used to open a new figure window, plot command for plotting the data in
data.temp file and the with command to indicate that points should be connected by a line. Notice that in
the second set of commands for is used inside plot - this is because the file has multiple columns and the
for command will plot multiple curves on the same graph. The command using fixes the variable on the
xaxis. The set key outside command puts the legend of the graph outside the plot area.
The following code segment writes the data into the file and the second for loop starts gnuplot and
generates the graphs. Remember to close the data file and gnuplot stream using fclose and pclose, respectively.
FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
for (ind=0;ind<i;ind++)
fprintf(temp, "%lf %lf %lf %lf\n", time[ind],
cimag(VR[ind]),cimag(VL[ind]),cimag(VC[ind]) );
for (ind=0;ind<4;ind++)
fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[ind]);
fclose(temp);
pclose(gnuplotPipe);

4 Testing
Choose atleast two sets of R, L, C and verify the resonant frequency, impedance variation, voltage/current
waveforms.

Вам также может понравиться