(Will add more screenshots soon...)
We had several assignments from the book:
- Exercise 2.1: fibonacci1 script
- Exercise 2.3: car_update script
- Exercise 3.1: car_loop script
- Exercise 3.2: car_loop script with plotting. Also try initial values of a & b = 10000
- Exercise 3.5: fibonacci2 sequence script
- Exercise 4.6: plotting fibonacci ratios
- Experiment with the following simulation of the trajectory of a hit baseball. For what angle is the horizontal distance maximized?
-------------------------------------------------------------------------------------------------------------------------------------------------------------
For Exercise 2.1 , we broke down the Fibonacci term formula into smaller parts (by assigning variables to represent each part) to make the program easier to debug. We also allowed the user to enter the desired value of n (to determine the nth fibonacci number). I've read about and learned the very basics of Python and realized program languages can be different yet similar.
Exercises 2.3, 3.1, 3.2 combined: We were given two locations (Albany and Boston) and the rates of car movements (3% and 5% respectively). We used a for loop to continue the exchange until a certain number of weeks. (The user was able to type in his/her desired number of weeks.) Since the total number of cars was 20,000 (by the conservation of cars), we assigned b(i+1) as 20,000 - a(i+1).
Exercise 3.5: This was our second Fibonacci sequence assignment, where we had to use a for loop to compute the first 10 elements of the sequence. We allowed the user to enter the number of terms, so this program would work for any term of the sequence.
Exercise 4.6: This was our third Fibonacci sequence assignment, and here, this program computes a vector (X) with the first n elements of a Fibonacci sequence and then computes a new vector (R) that contains the ratios
of consecutive Fibonacci numbers. The user was able to enter the value of n. This was one of the more challenging programs to write because I found it challenging to create vectors (I really had to read and reread the section on vectors to better understand the concepts). We discovered that the value of convergence was about 1.618 (golden ratio).
Trajectory of a Hit Baseball
Program:
Although this was not the best method, I used the "trial and error" method. From physics, I had learned that 45 degrees would give the largest horizontal distance. However, using this program, 44 degrees gave a larger horizontal distance value than 45 degrees did. So, I substituted in different values (close to 45 degrees) for the angle and determined that the largest x(i) or the horizontal distance was 262.8410 (when angle = 45.5 degrees).
I will try to determine a better way to solve this program, perhaps by using some maximum function/tool.
Exercise 4.6: This was our third Fibonacci sequence assignment, and here, this program computes a vector (X) with the first n elements of a Fibonacci sequence and then computes a new vector (R) that contains the ratios
of consecutive Fibonacci numbers. The user was able to enter the value of n. This was one of the more challenging programs to write because I found it challenging to create vectors (I really had to read and reread the section on vectors to better understand the concepts). We discovered that the value of convergence was about 1.618 (golden ratio).
Trajectory of a Hit Baseball
Program:
% 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 = 45.5 % 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
end
x(i) % Display the final x value.
Although this was not the best method, I used the "trial and error" method. From physics, I had learned that 45 degrees would give the largest horizontal distance. However, using this program, 44 degrees gave a larger horizontal distance value than 45 degrees did. So, I substituted in different values (close to 45 degrees) for the angle and determined that the largest x(i) or the horizontal distance was 262.8410 (when angle = 45.5 degrees).
I will try to determine a better way to solve this program, perhaps by using some maximum function/tool.
No comments:
Post a Comment