Control_Tutorials_for_MATLAB_and_Simulink_-_Time-response_Identification_of_an_RC_Circuit
Control_Tutorials_for_MATLAB_and_Simulink_-_Time-response_Identification_of_an_RC_Circuit
SYSTEM
PID
ROOT LOCUS
FREQUENCY
STATE-SPACE
DIGITAL
SIMULINK
MODELING
CONTROL
Contents
Equipment needed
Purpose
Extensions
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 1/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
Equipment needed
USB cable
Breadboard
Jumper wires
Simulink for visualization and for comparison to our resulting simulation model
output.
Purpose
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 2/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
To begin, we assume a direction for the current and then apply Kirchhoff’s
Voltage Law (loop law). Current flows from a higher potential to a lower
potential, therefore, the direction of the current will be clockwise in this case
(shown below).
The loop law states that the sum of voltages around a closed loop must equal
zero. Thus, the loop law produces the following governing equation for the
circuit.
(1)
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 3/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
a system and is derived by first taking the Laplace transform of a given integro-
differential equation, while assuming zero initial conditions ( ).
(2)
Next we must perform some algebra to re-arrange the above into the form of its
output divided by its input. In this case, our input is and our output is
. Therefore, we must eliminate the current from the above since it is
neither an input nor an output, and we must introduce the output into the
above equation. Solving the above equation for we arrive at the following.
(3)
Then we can recognize that the output voltage (across the capacitor) is
. Taking the Laplace transform and again solving for ,
we arrive at the following.
(4)
Setting the two previous equations equal to one another, we can eliminate .
(5)
Then re-arranging into the desired form of output divided by input, we produce
the resulting transfer function model.
(6)
(7)
In this form we can see by inspection how the parameters of the circuit affect its
response. Specifically, the values of the resistor and capacitor affect the circuit's
speed of response as indicated by their influence on the circuit's time constant,
. The circuit components, however, cannot influence the circuit's
steady-state performance as indicated by the fact that the DC gain always
equals 1.
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 4/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
In this experiment we will record the output voltage of the RC circuit for a step in
input voltage. Based on the resulting time response of the output voltage, we will
fit a model to the data. This approach is sometimes referred to as blackbox
modeling or data-driven modeling. After we have generated such a model, we
will compare it to the first-principles derived model we created previously.
Hardware setup
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 5/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
In this experiment, the values of the resistor and capacitor are chosen such that
the circuit's time response is slow enough that the Arduino/Simulink setup can
sample the circuit at a fast enough rate to give a clear picture of the circuit's
output. If the sampling rate is at least 10 times faster than the circuit's time
constant, we will be able to get a clear picture of the response. In this
experiment, we chose and to produce a circuit with
time constant second. This is a low power application so most
resistors and capacitors available will work fine, just make sure that the
capacitor is rated for at least 5 Volts.
Software setup
In this experiment, we will employ Simulink to read the data from the board and
to plot the data in real time. In particular, we will employ the IO package from the
MathWorks. For details on how to use the IO package, refer to the following link.
The Simulink model we will use is shown below and can be downloaded here,
where you may need to change the port to which the Arduino board is
connected (the port is COM5 in this case).
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 6/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
The Arduino Analog Read block reads the output voltage data via the Analog
Input A0 on the board. Double-clicking on the block allows us to set the Pin to 0
from the drop-down menu. We also will set the Sample Time to "0.1". This is 10
times faster than the circuit's time constant and hence is sufficiently fast. The
other blocks in the model can also be set to have a Sample Time of "0.1" (or left
as "-1"). In the downloadable model, the sample time is set to the variable Ts
which needs to be defined in the MATLAB workspace by typing Ts = 0.1
before the model can be run. The Gain block on the Analog Input is included to
convert the data into units of Volts (by multiplying the data by 5/1023). This
conversion can be understood by recognizing that the Arduino Board employs a
10-bit analog-to-digital converter, which means (for the default) that an Analog
Input channel reads a voltage between 0 and 5 V and slices that range into
pieces. Therefore, 0 corresponds to 0 V and 1023 corresponds to
5 V.
The given Simulink model then plots the commanded input voltage and
recorded output voltage on a scope and also writes the output data, as an array,
to the MATLAB workspace for further analysis. The Arduino Digital Write block,
the Arduino Analog Read block, the Arduino IO Setup block, and the Real-Time
Pacer block are all part of the IO package. The remaining blocks are part of the
standard Simulink library, specifically, they can be found under the Math,
Sources, and Sinks libraries.
Once the Simulink model has been created, it can then be run to collect the
input voltage and output voltage data. Executing the following code at the
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 7/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
MATLAB command line will generate the graph shown below. Note that we plot
only the last five seconds of the run, thereby excluding any discharging of the
capacitor that may have taken place.
plot(0:0.1:5,ei(51:101),'r--');
hold on
plot(0:0.1:5,eo_act(51:101),'b*-');
xlabel('time (seconds)')
ylabel('voltage (Volts)')
title('RC Circuit Step Response')
legend('input','experimental output','Location','SouthEast')
axis([0 5 0 5.1])
Parameter identification
Based on the on the above figure, we can fit a model to the recorded data.
Recognizing that the observed data has the shape of a first-order step
response, we will assume the following standard first-order model for the circuit
(happens to match our first-principles model).
(8)
We can then identify the system parameters and from the recorded
response data. Specifically, the steady-state value of the response indicates that
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 8/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
plot(0:0.1:5,eo_act(51:101),'b*-');
xlabel('time (seconds)')
ylabel('voltage (Volts)')
title('RC Circuit Step Response')
legend('experimental output','Location','SouthEast')
axis([0 5 0 5])
Since the step input occurs at approximately 0.10 seconds and the output
reaches 3.16 Volts at approximately 1.16 seconds, the time constant can be
identified as seconds. In reality, we only have data at
intervals of 0.1 seconds, but using a datatip in the MATLAB plot allows us to
interpolate between the samples. Using a smaller sample period would allow us
to improve the accuracy of our identification of the system parameters. In
conclusion, our resulting blackbox model is the following.
(9)
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 9/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
In the this section we will validate the two circuit models we have derived, the
first-principles model and the blackbox model, by comparing the step responses
predicted by the models to actual data recorded from the circuit.
(10)
We will generate a Simulink model of this governing equation by first solving the
equation for its highest-order "derivative."
(11)
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 10/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
In order to make our Simulink model cleaner, we can create a subsystem for our
first-principles model. Specifically, we can select the blocks highlighted in the
above figure and then right-click. From the resulting menu we then choose
Create Subsystem from Selection. We can also change the color of the block
by choosing Format > Background Color > Light Blue from the resulting
menu. Labeling the subsystem as "RC Circuit Model" gives us the Simulink
model shown below.
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 11/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
Going a step further, we can also create a mask for this subsystem by right-
clicking on its block and selecting Mask > Create Mask... from the resulting
menu. The creation of a mask for the subsystem allows us to specify the
subsystem's parameters (R, C, and q_init) from within Simulink without delving
into specific blocks. Specifically, right-click on the RC Circuit Model and select
Mask > Edit Mask.... Under the panel Icon & Ports, type the following
commands in the Icon drawing commands window as shown.
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 12/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
Next, click on panel Parameters & Dialog tab and select or drag the Edit icon
in the left palette to add parameters to the Dialog box shown below. For the first
parameter, enter "Resistance" under the Prompt column and "R" under the
Name column. Repeat this process two more times for the "Capacitance" and
the "Initial charge on capacitor" as shown below.
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 13/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
This window is then closed and the settings saved by clicking the OK button. If
you double-click on RC Circuit Model again, you will find that the mask with
desired parameters has been generated. The values of R, C, and q_int are set
to match the parameters of our physical system. For this experiment, recall that
we are employing a resistor, a capacitor, and we are observing
the step response when when the capacitor is initially discharged.
Running this model, we can then compare the simulated output voltage eo_sim
to the actual experimental output voltage eo_act. Recall that the Pulse
Generator block is set to output 0 for the first 5 seconds of the run in order to
discharge the capacitor completely before stepping to 1. Entering the following
code in the MATLAB command window will generate the figure shown below.
plot(0:0.1:5,eo_act(51:101),'b*-');
hold on
plot(0:0.1:5,eo_sim(51:101),'r--');
xlabel('time (seconds)')
ylabel('voltage (Volts)')
title('RC Circuit Step Response')
legend('experiment','model (first principles)','Location','So
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 14/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
The above figure shows pretty good, though not exact, agreement between the
output voltage predicted by our first-principles model and the physical circuit.
We will discuss the accuracy of this model momentarily, but let's first investigate
the performance of our blackbox model.
(12)
We can generate the blackbox model in Simulink using the Transfer Function
block. Double-clicking on the Transfer Function block we can set Numerator
coefficients equal to the variable "K" and the Denominator coefficients equal
to "[tau 1]" where tau is also a variable, the time constant.
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 15/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
We can then create a subsystem with a mask for this blackbox model in the
same manner we did for the first-principles model. The resulting Simulink model
is shown below and can be downloaded here. Remember to set the parameters
for the blackbox subsystem parameters K and tau.
Recall in the above that our first principles model did not exactly predict the
output generated by the actual physical circuit. One factor contributing to this
difference could be that the components (the resistor and capacitor) have actual
values that differ from their nominal labels. We can improve our model by
measuring the actual resistance and capacitance using an Ohmmeter and a
capacitance meter, respectively.
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 16/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
plot(0:0.1:5,eo_act(51:101),'b*-');
hold on
plot(0:0.1:5,eo_fp(51:101),'r--');
plot(0:0.1:5,eo_bb(51:101),'k--');
xlabel('time (seconds)')
ylabel('voltage (Volts)')
title('RC Circuit Step Response')
legend('experiment','model (first principles)','model (black
Examining the above, the blackbox model matches the data extremely well,
while the addition of more accurate estimates of R and C did not improve the
agreement with the first-principles model significantly. It is hypothesized that the
error in the first principles model is likely due to the unmodeled resistance and
capacitance of the input and output channels connected to the circuit. Such
loading effects could be captured by the blackbox model since the true response
appears to fit the form of a first-order model well.
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 17/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
It is generally the case that a blackbox model will be quite accurate when it is
applied to the same situation (input, environmental conditions, etc.) from which it
was derived. A blackbox model can, however, break down if it is applied under
different conditions. This is in particular true if the model doesn't accurately
capture the underlying structure of the system. We will, therefore, test our
models under different conditions in the next section. In this case since we know
that the structure of our first-principles model matches the structure of our
blackbox model (both first order) and since the resistance and capacitance likely
won't change much (at least over a short time period), our blackbox model
should still perform well. A primary advantage of employing a blackbox model,
aside from its accuracy, is that one does not need to understand the underlying
system well in order to derive a model for it.
In the previous section we examined how well our models predicted the RC
circuit's step response. That case examined the system's response for a forcing
input of a 5-Volt step in input voltage and zero initial conditions (no initial charge
on the capacitor). In this section we will investigate how well the models predict
the RC circuit's free response. This case will assume no forcing input, ,
and an initial charge on the capacitor. This process will illustrate how to deal
with initial conditions in our models.
To begin, we will solve for the theoretical free response we will expect from the
RC circuit. Specifically, we will solve the governing equation we derived from
first principles using the Laplace transform. This governing equation is repeated
below.
(13)
(14)
(15)
(16)
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 18/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
Recalling that for this case , we can rearrange the differential equation as
follows.
(17)
(18)
(19)
Substituting for and solving the equation for gives the following.
(20)
Applying inverse Laplace transform, we then have the time response of the
charge on the capacitor.
(21)
Again using the definition of capacitance, we then have the output response of
the RC circuit for some initial charge and no forcing input voltage.
(22)
The above equation represents the theoretical free response expected from the
RC circuit based on the first-principles model we derived. We would like to
compare the prediction of the first-principles model with that of the blackbox
model and the performance of the actual physical circuit. One challenge is that
the blackbox model is in the form of a transfer function which does not address
initial conditions. This can be addressed by running the existing Simulink model
from zero initial conditions and then bringing the models and physical circuit to
the desired initial state (level of capacitor charge). For our existing Simulink
model with Pulse Generator input, we could run the model for 15 seconds where
for the first 5 seconds the input is 0 to discharge the capacitor completely, for
the next 5 seconds the input is 1 in order to charge the capacitor completely (
Volts), and the final 5 seconds represents the free response as the input
drops back to 0. You can change the simulation run time in the toolbar of the
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 19/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
window of your Simulink model, or via the drop-down menu Simulation >
Model Configuration Parameters under the Solver menu. Running the
existing model under these conditions and executing the following MATLAB
commands will generate the figure shown below.
plot(0:0.1:5,eo_act(101:151),'b*-');
hold on
plot(0:0.1:5,eo_fp(101:151),'r--');
plot(0:0.1:5,eo_bb(101:151),'k--');
xlabel('time (seconds)')
ylabel('voltage (Volts)')
title('RC Circuit Free Response')
legend('experiment','model (first principles)','model (black
Examining the above, the blackbox model still provides better agreement with
the recorded output voltage from the physical RC circuit than does the first-
principles model. Recall that this difference is attributed to the fact that the first-
principles model does not capture the effects of the input and output channel.
The results from the blackbox model, however, do not seem to agree with the
experimental data in this case as well as it did with the step response
experiment. This difference is anticipated since the blackbox model was derived
based on an identical step response experiment, rather than for the conditions
of this free response experiment. That being said, the blackbox model still
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 20/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
agrees with the physical data very well. This is attributable to the fact that the
physical RC circuit is simple, approximately linear, and relatively time invariant.
(23)
Extensions
An interesting change that can be made to this experiment is to replace the fixed
resistor with a variable resistor (a trimmer potentiometer) to observe directly how
the circuit's response changes with resistance. This can also be done with a
variable capacitor, or by even swapping the resistor and capacitor with fixed
components of different values.
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 21/22
4/30/2018 Control Tutorials for MATLAB and Simulink - Time-response Identification of an RC Circuit
http://ctms.engin.umich.edu/CTMS/index.php?aux=Activities_RCcircuitA 22/22