Fortran Coding help
Fall 2017 Fortran Project The Fortran project for this term will consist of the following two separate parts.
1. Create a Table of Trigonometric Values Write a program called trigtable.f95 that creates (prints to the console) a nice table of the values of sine, cosine, tangent, secant, cosecant, and cotangent at all even degree multiples of 10º and 15º between 0º and 360º. (The mod function may be useful here.) You may do this using arrays or without, depending on your preference.
The table should have a header row and a separator row. The angle should be shown as an integer in a width 4 field and each trigonometric value in the table should have width 8 with 4 digits to the right of the decimal place. Some functions will give infinite results for certain angles, so check and print INF for those results. Below is the output for the first 90º.
t sin(t) cos(t) tan(t) sec(t) csc(t) cot(t) - ------ ------ ------ ------ ------ ------ 0 0.0000 1.0000 0.0000 1.0000 INF INF 10 0.1736 0.9848 0.1763 1.0154 5.7588 5.6713 15 0.2588 0.9659 0.2679 1.0353 3.8637 3.7321 20 0.3420 0.9397 0.3640 1.0642 2.9238 2.7475 30 0.5000 0.8660 0.5774 1.1547 2.0000 1.7321 40 0.6428 0.7660 0.8391 1.3054 1.5557 1.1918 45 0.7071 0.7071 1.0000 1.4142 1.4142 1.0000 50 0.7660 0.6428 1.1918 1.5557 1.3054 0.8391 60 0.8660 0.5000 1.7321 2.0000 1.1547 0.5774 70 0.9397 0.3420 2.7475 2.9238 1.0642 0.3640 75 0.9659 0.2588 3.7321 3.8637 1.0353 0.2679 80 0.9848 0.1736 5.6713 5.7588 1.0154 0.1763 90 1.0000 0.0000 INF INF 1.0000 0.0000
2. Determine Trajectory Information by Simple Numerical Integration Background A ball thrown or shot from the ground at an initial speed and at a given angle above horizontal will travel a certain distance, while it's vertical position first increases and then decreases until it hits the ground. For higher velocities, the drag (wind resistance) on the ball can be a significant factor.
Simple trigonometry shows that, for an overall initial speed S0 and angle θ, the initial x and y components of velocity will be x'(0) = S0 cos θ, and y'(0) = S0 sin θ. Further, basic fluid mechanics says that the drag force on a sphere is given by Fd = ½ Cd ρ A v2 where
Cd is the coefficient of drag (about 0.47 for a sphere)
ρ is the density of the fluid (1.225 kg/m3 for air at sea level)
A is the frontal area of the sphere (π r2)
v is the velocity
Completing a free-body diagram of the ball and doing a little algebra, we can determine the acceleration in the x and y directions to be
ax = x''(t) = –½ Cd ρ A x'(t)2 / m
ay = y''(t) = –½ Cd ρ A y'(t)2 / m – g
where m is the mass of the ball and g is the acceleration due to gravity (about 9.81 m/s2).
Project Write a program called trajectory.f95 that asks the user for speed S0 and angle θ of a launched projectile, which you can assume to be a 1 kg ball with a 0.1 m diameter. Use that info to calculate the trajectory travelled before it hits the ground. Use simple numerical integration to solve the system. For our purposes, that means
a. Set initial values at time t = 0. Assume that the ball starts at x0 = 0 and y0 = 0. The x and y components of velocity are given above.
b. Start a loop that will run until the ball hits the ground. c. Update the time so that t = t + Δt. d. Call functions from a module that return the x and y components of acceleration, x''(t) and y''(t).
(More on this below.) e. "Integrate" to find the values of velocity and position at the next time step. In other words
x'(t+Δt) = x'(t) + Δt x''(t) and x(t+Δt) = x(t) + Δt x'(t)
And similarly for the y-direction. f. Check whether the ball has fallen back to the ground (y ≤ 0) and repeat if it has not. Terminate
the loop if it has.
A smaller Δt will result in more accurate results. You can experiment with different values for Δt, but test with Δt = 0.01 sec.
Use double-precision for all real calculations to numerical error. Write the time, x position, y position, x velocity, and y velocity at each time step to an output file with header lines for column labels and units. The output file should be named trajectory.txt. One can infer the table header and format specifications from the example below showing the first several lines of the file. Note that the example shown is for an initial velocity of 10 m/s and angle of 45º, but the program should work for any positive initial velocity and starting angle between 0º and 90º.
time x y x_dot y_dot (sec) (m) (m) (m/s) (m/s) 0.000 0.000 0.000 7.071 7.071 0.010 0.071 0.070 7.070 6.972 0.020 0.141 0.138 7.069 6.873 0.030 0.212 0.206 7.068 6.773 0.040 0.283 0.273 7.067 6.674
Write a module called accelerations.f95 with some variables and two functions. The module should define double-precision constants for gravity, the density of air, the coefficient of drag on a sphere, and π. It should also define double-precision variables for the radius and mass of the ball. The first function takes the speed in the x-direction as an input variable and returns the acceleration in the x- direction. The second function does the same in the y-direction. Those are the two functions to be called in step d above.
- 1. Create a Table of Trigonometric Values
- 2. Determine Trajectory Information by Simple Numerical Integration
- Background
- Project