gnu plot assignment

4
IC250 Laboratory Assignment Template B. D. Chaudhary August 20, 2015 1

Upload: divyanshuverma

Post on 28-Jan-2016

222 views

Category:

Documents


0 download

DESCRIPTION

GNU plot assignment

TRANSCRIPT

Page 1: GNU plot assignment

IC250 Laboratory Assignment Template

B. D. Chaudhary

August 20, 2015

1

Page 2: GNU plot assignment

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

ω0 =1

LC. (1)

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

circuits.

3 Series resonance

R L C

V =Vm sin(ωt)

I

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

Page 3: GNU plot assignment

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 induc-

tance).

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+ 1jω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

Page 4: GNU plot assignment

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

x−axis. 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, respec-

tively.

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.

4