Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++, Trajectory Your job is to find the distance a cannonball goes. A cannon is pointed at an angle q from horizontal, and it shoots

C++, Trajectory Your job is to find the distance a cannonball goes.

A cannon is pointed at an angle q from horizontal, and it shoots a cannonball at an initial velocity v0. The cannonball has mass m, diameter d, and a drag coefficient of cd.

For this project assume the drag coefficient is effectively constant. Assume that the cannonball does not go very high, with the result that variations in air density

and gravity can be neglected. Use the density of air p = 1.225 kg/m3, gravity g = 9.81 m/s^2, and dt = .0001. When acceleration is constant, an analytical expression for the distance is possible.

Distance = 2 V0^2 sin(q) cos(q) / g

If air drag is included, however, acceleration is not constant, and analytical solutions are not always possible. Numerical solutions, however, is often possible, using numerical integration or timestepping.

Using a finite difference approximation to a differential,

Xnew = Xold +dt * Vxold, Ynew = Yold + dt * Vyold

Vxnew = Vxold + dt * ax, Vynew = Vyold * ay The starting values should be x = 0, y = 0, vx = v0 cos(q), and vy = v0 sin(q).

Acceleration has a component due to gravity, and a component due to drag. Drag acts opposite to the direction the object is moving. You can resolve the components of this vector into the following expressions:

ax = -(D*vx /sqrt(vx^2 +vy^2))/m, ay =-g -(D*vy /sqrt(vx^2 +vy^2))/m

where D= cd * p * V^2 * A/2, and A=pi * d^2/4 with d= diameter of cannonball.

Coding details

Read 5 values from a file data.txt. The 5 values in order will be the cannon angle (in degrees), the cannonball initial velocity, the cannonball mass, the cannonball diameter, and the drag coefficient. You can assume the file always has at least 5 numbers. All 5 numbers may have decimal parts.

To use the sin and cos functions in C++, you will need to include the library. The sin and cosine functions in C++ use radians, so you may need to convert degrees to radians. You can do this conversion as follows: sin(theta * M_PI/180). M_PI is a constant for the value of pi and is part of the library.

You should think about how you will know when to stop the numerical integration.

The easiest way to square a number in C++ is to multiply it by itself. Do not use the caret ^ symbol that is not the power operator in C++.

Sample data files and output:

data.txt

45

100

4

.10

0

Output: 1019.38

Note that this is very close to the analytic solution ignoring drag.

data.txt

45

100

4

.10

.5

Output: 706.242

The drag coefficient on a sphere is about 0.5 for a fair range of velocities. The drag coefficient is higher at slow speeds, and lower at very high speeds.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions