6.2 System Simulation 285
6.2.3 System simulation examples
Example 6.11.
The system shown here is simple RC circuit, with re-
sistance R = 5 ×10
3
ohms, and capacitance C = 10
−6
farads. Also, in this example we will take the applied
voltage to be a constant, v(t) = 3 volts, i.e., a step
input. Using the charge, q, as the generalized displace-
ment we get that Lagrange’s equation of motion for
the system is
R ˙q +
q
C
= v. (a)
If we assume that the initial charge is q(0) = q
0
, then the analytical solution
to this linear first-order differential equation is
q(t) = Cv(1 −e
−t/τ
) + q
0
e
−t/τ
, (b)
where τ = RC is called the time constant.
To solve the differential equation (a) using the function ride we use the
following procedure.
• Construct an ‘m-file’ that describes the differential equation (a) as an
implicit differential equation (IDE). Here, m-files are MATLAB/Octave
script files. The m-file ex1 ide.m, shown in Listing 1, provides the IDEs
for this problem. Line 1 in this listing declares the function, its output
and input variables. Lines 3, 4, 5 initializes the model parameters. Line
7 computes the ‘residual’ of the IDEs at time t, i.e., Phi = Φ(y, ˙y, t). For
this problem y(1) = y = q, and yp(1) = ˙y = ˙q.
• Construct an m-file that computes the Jacobian of the IDEs. This m-file is
called ex1 jacobian.m, and its content is shown in Listing 2. Line 1 of this
listing gives the declaration of the function. The Jacobian J = dΦ/dy =
1/C is on line 7, and the term M = dΦ/d ˙y = R is on line 8.
• Construct an m-file that establishes the initial conditions, interval of in-
tegration, and options required by the function ride. Listing 3 shows the
commands used to solve the IDEs using ride. The command on line 1 of
this listing clears all variables and functions from the MATLAB/Octave
interpreter.
The model parameters are initialized on lines 2, 3, and 4.
The integration interval is defined in the vector tspan on line 5. Here,
tspan is defined as a vector of 100 equally spaced points from 0 to 0.05.
Thus, we seek the solution to the IDE in the interval 0 ≤ t ≤ 0.05 seconds.
The initial charge is given by y0 = q(0) = 0 on line 6.
The initial current is given by yp0 = ˙q(0) = v/R on line 7. It is easy to
verify that these initial conditions are consistent, i.e., Φ(y(0), ˙y(0), 0) = 0.