Sample Matlab Code

Have these two files in your current directory or path. To integrate the system type 'car' (assuming the first file is named car.m).

First file:

%
%   First control example: proportional control for cruise control.
%   B. Goodwine, January 14, 1999.  I called this file 'car.m'
%

b = 25;
m = 750;
lam = 0.5;
vd = 40;
kp = 100;
fe = b*vd;    % inital condition for engine 'force'

%   OutputFcn odeplot tells Matlab to plot the output
%   Outputsel [1] tells it to plot only the velocity (the
%   first component of y).
options = odeset('OutputFcn','odeplot','Outputsel',[1]);

%   In the following, t = time, and y is the state vector [v fe].
%   The [0 20] is the time range and [0 fe] are the initial conditions.
%   'proporational' tells this program to look at the file
%   'proportional.m' for the equations of motion.

[t y] = ode45('proportional', [0 20], [0 fe],options,b,m,lam,vd,kp);

Second file:

%
%   The is the 'odefile' called by ode45 to integrate the cruise control
%   equations.  To modify to add integral control, you have to add a
%   integral gain, ki, to the paramter list in the 'odefile' function.
%   You must also add ki to the ode45 function line in the car.m file.
%   The same is true later when you add derivative gain.
% 
%   I called this file 'proportional.m'
%
%   Also, you must change the form of 'out' to reflect the addition of
%   integral and/or derivative gain.
%
%   B. Goodwine, January 16, 1998.
%

function out = odefile(t,y,options,b,m,lam,vd,kp);
out = [-b/m*y(1)+y(2)/m; lam*kp*(vd - y(1)) - lam*y(2)];


Return to the ME 437 homepage.
Last modified: January 16, 1998.