Projects‎ > ‎

Matlab Simulations

In class today, we will be learning a little bit about Matlab and doing some simple simulations using Matlab. 

Skim the first four chapters of Allen Downey's book, Physical Modeling in MATLABfor details about MATLAB. 

Complete the following exercises: 
  1. Downey Exercise 3.1 (p. 14): fibonacci1 script
  2. Downey Exercise 3.3 (p. 20): car_update script
  3. Downey Exercise 4.1 (p. 26): car_loop script
  4. Downey Exercise 4.2 (p. 27): car_loop script with plotting.  Also try initial values of a & b = 10000
  5. Experiment with the following simulation of the trajectory of a hit baseball. For what angle is the horizontal  distance maximized? 
% Model of trajectory of hit baseball. Assumes no air drag. 
% What angle maximizes the distance travelled? 

clf % clear the previous figure
hold on % allow plotting of multiple figures
angle = 30; % angle baseball hit, in degrees
velocity = 50 % initial velocity of baseball, in meters/sec
rads = angle * pi / 180;
velocity_x(1) = velocity * cos(rads); % horizontal velocity of baseball
velocity_y(1) = velocity * sin(rads); % vertical velocity of baseball
x(1) = 0; % x position of batter
y(1) = 1; % assume baseball hit 1 meter off ground
dt = 0.1; % time step for simulation, in seconds
g = 9.8; % gravitational constant, in meters/sec^2
i = 1 % iteration index
while y(i) > 0 
    x(i+1) = x(i) + velocity_x(i)*dt; % Update x position 
    y(i+1) = y(i) + velocity_y(i)*dt; % Update y position
    velocity_y(i+1) = velocity_y(i) - g*dt; % y velocity changes due to gravity
    velocity_x(i+1) = velocity_x(i); % x velocity doesn't change (assume no air drag)
    plot(x(i), y(i), 'r.-');  % display a red dot for each point, and connect them with lines
    i = i + 1; % change index for next iteration
x(i) % Display the final x value.

6. Below is a simple model of a DC motor.  Implement this model in MATLAB by making a simulation motor.m similar to the baseball trajectory simulation above.  Note that the Greek omega is the angular velocity (in radians/second) and the Greek tau is torque (in Newton-meters).  In one time step dt, the rotation changes by omega*dt radians, where there are 2*pi radians in 360 degrees. Also in one time step, omega changes by (tau/m)*dt, where m is the moment of inertia of the motor. 

Assume that you run the model until the rotation is 90 degrees. 

You may assume the following values: 

V_terminal: 5 Volts
K_motor: 0.3 Newton-meter/amp
R: 25 ohms
m (moment of inertia for motor): 0.0001 Kilogram-meter^2
dt = 0.001 (simulation time step)

7. In a file motor_control_prop.m, develop a proportional control motor controller for the motor model from #6. Introduce a variable K_prop that controls the proportional gain. Design your program so that it runs for several simulated seconds and displays the last time t_last_bad the angle was more than 0.1 degrees away from 90 degrees. Fiddle with K_prop to minimize t_last_bad.  What are your values of K_prop and t_last_bad for the least t_last_bad you observe?

8.  In a file motor_control_prop_deriv.m, extend your proportional control motor controller from #7 to include derivative control, whose gain is controlled by the variable K_deriv. Fiddle with K_prop and K_derv in an attempt to minimize t_last_bad.  What are your values of K_prop, K_deriv, and t_last_bad for the least t_last_bad you observe?