Prompt:
(The expression for calculating Fibonacci numbers is given.) Translate this expression into MATLAB and store your code in a file named fibonacci1. At the prompt, set the value of n to 10 and then run your script. The last line of your script should assign the value of Fn to ans. (The correct value of F10 is 55).
Program:
Prompt: car update that updates the number of cars in each location from one week to the next. The precondition is that the variables a and b contain the number of cars in each location at the beginning of the week. The postcondition is that a and b have been modified to reflect the number of cars that moved.
Imagine that you are the owner of a car rental company with two locations, Albany and Boston. Some of your customers do “one-way rentals,” picking up a car in Albany and returning it in Boston, or the other way around.
Over time, you have observed that each week 5% of the cars in Albany are dropped off in Boston, and 3% of the cars in Boston get dropped off in Albany. At the beginning of the year, there are 150 cars at each location. Write a script called
To test your program, initialize a and b at the prompt and then execute the script. The script should display the updated values of a and b, but not any intermediate variables.
Program:
Prompt: car loop that uses a for loop to run car update 52 times. Remember that before you run car update, you have to assign values to a and b. For this exercise, start with the values a = 150 and b = 150.
Create a script named
Program:
Prompt:
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 figurehold on % allow plotting of multiple figuresangle = 30; % angle baseball hit, in degreesvelocity = 50 % initial velocity of baseball, in meters/secrads = angle * pi / 180;velocity_x(1) = velocity * cos(rads); % horizontal velocity of baseballvelocity_y(1) = velocity * sin(rads); % vertical velocity of baseballx(1) = 0; % x position of battery(1) = 1; % assume baseball hit 1 meter off grounddt = 0.1; % time step for simulation, in secondsg = 9.8; % gravitational constant, in meters/sec^2i = 1 % iteration indexwhile y(i) > 0x(i+1) = x(i) + velocity_x(i)*dt; % Update x positiony(i+1) = y(i) + velocity_y(i)*dt; % Update y positionvelocity_y(i+1) = velocity_y(i) - g*dt; % y velocity changes due to gravityvelocity_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 linesi = i + 1; % change index for next iterationendx(i) % Display the final x value.
Program:
Prompt:
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)
and then,
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?
Program (proportional control):
No comments:
Post a Comment