MATLab Tutorial #3
MATLab Tutorial #3
MATLab Tutorial #3
MATLAB Tutorial #3
dy
+ 2y = u(t) u(t 1)
dt
over the range 0 t 5. We will assume that the initial value y(0) = 0. The first step is
to write the equation in the form:
dy
= 2y + u(t) u(t 1)
dt
Next we need to create a MATLAB m-file to calculate dy/dt. From the MATLAB
window select: File New M-File. Enter the following:
function dy = f(t,y)
dy = -2*y + (t>=0)-(t>=1);
This creates a function that calculates the value of dy (actually dy/dt). The function is
called f and the file should be named: f.m when it is saved since all MATLAB
functions should be saved in a file with the same name as the function. Convince
yourself that the right hand side of the equation for dy corresponds to the expression
above. Make sure that you save this function in your working directory or one that is in
your path.
Once the function for dy/dt has been created we need to specify the end points of the time
range (0 t 5) and the initial value of y (zero). This is done in the MATLAB command
window with the following commands:
>> t= [0 5];
>> inity=0;
The ode45 command can now be called to compute and return the solution for y along
with the corresponding values of t using:
>> [t,y]=ode45(@f, t, inity);
Note that the @f refers to the name of the function containing the function. The solution
can be viewing with:
>> plot(t,y)
as shown in figure 1.
d2 y
dy
+
3
+ 2y = cos 2t
dt 2
dt
dy
=1
dt t =0
x=
dy
dt
d2 y
dy
= 3 2y + cos 2t
2
dt
dt
or in terms of x:
dx
= 3x 2y + cos 2t
dt
We will use the ode45 command to solve the system of first order differential equations:
dx
= 3x 2y + cos 2t
dt
dy
=x
dt
We place these two equations into a column vector, z, where:
x
z=
and
dz
=
dt
dx
dt
dy
dt
0];
9. In the model window select Simulation Start. You will hear a beep when the
simulation is complete. Double click on the Scope icon and it will open and display
the output of the sine wave block. Try clicking on the binocular icon in the Scope
window. This should cause the axes to readjust as shown in figure 7.
then enter the value 0.01 into the Fixed Step Size entry. Click OK and run the
simulation once again. Note that the display is now smooth.
14. Drag the Pulse Generator from the Source sub-library into the model window.
Double click the Pulse Generator block and modify the parameters as shown in figure
9 below. Click OK.
dy
+ 2y = u(t) u(t 1)
dt
First we need to simulate the single one-second pulse. Since the simulation will run for
0t5, we can use the pulse generator to generate a five second pulse with a 1 second
pulse width. Since it will not repeat within the five seconds, this simulates a single pulse.
Remove the integrator from the model shown in figure 12 and connect the pulse
generator directly to the scope input. Change the pulse generator to have the settings as
shown in figure 13. The 20% pulse width corresponds to one second as needed.
Change the simulation time in the configuration parameters to five seconds and simulate
the system. Specify fixed-step samples of 0.01 seconds. Verify that the pulse generator
is outputting the desired pulse for the differential equation above.
dy
= 2y + u(t) u(t 1) = 2y + p(t)
dt
where p(t) is the one second pulse. The right hand side of this equation can be modeled
in Simulink using the model shown in figure 14. The subtraction block and the gain
block are found in the Math Operations sub-library.
The labels on the wires are inserted by double clicking on the wires and typing in the text.
By double clicking on the gain block you can set its constant to two. If the input to the
gain block is y, then the output of the subtractor is dy/dt. By passing this output through
an integrator, the input y is found. The model in figure 15 should now simulate the
original differential equation.
d2 y
dy
+ 3 + 2y = cos 2t
2
dt
dt
in Simulink. This equation can be re-written as:
d2 y
dy
= 3 2y + cos 2t
2
dt
dt
and modeled in Simulink as shown in figure 16. In order to get the three input subtractor,
use the two input subtractor selected above. Double click on the block and change the
List of Signs to: + - - . This will result in a block that adds the top input and subtracts
the second and third inputs.
>> plot(t,z(:,2))
>> hold on
>> plot(0:.01:10, simout, 'r')
The two solutions appear in figure 20. Note that the time values for the simout plot are
determined by the fixed spacing of 0.01 specified in the configuration parameters in
Simulink.
Now the steady state portions of the solutions are the same. However, the initial transient
response is not. This is because the two solutions were obtained with different initial
conditions. In the earlier command line solution the initial conditions: y(0) = 0 and
dy
= 1 were used. The integrator blocks in Simulink allow for the inclusion of
dt t =0
initial conditions. The second integrator outputs the value of y. Thus, the default initial
condition of zero is correct. However the first integrator outputs dy/dt. Double click on
the first integrator and change the initial condition to one. After simulating and replotting both solutions the curves in figure 22 are obtained. Note that the solutions now
match. This confirms that either method will provide a correct solution.