Y2 Computational

Physics Laboratory

Exercise 1

Programming in C++: a problem in ballistics

Aims and objectives:

  • To write a simple program in C++ to model the ballistic trajectory of a projectile
  • To regain basic familiarity of concepts and knowledge acquired in the Y1 C++ lab, in the framework of a (straightforward) problem-based learning approach
  • 1 mark given for overall presentation

Duration: 2 weeks

Hand-in date: At the start of session in week beginning 17th October


Introduction

The aim of this first exercise is to bring you gently back up to speed with what you learned in the Y1 C++ module. This will be done using a problem-based learning approach. You will be writing a short program in C++ to output information on the trajectory of a ballistic projectile. You will also be required to derive some simple equations, and comment critically on a few aspects of the results.

This may sound a little daunting. Your recollection of some aspects of C++ may have faded over the summer break! The intention here is to choose a straightforward problem, where the length and complexity of code required is fairly simple, so you can gain confidence and lay the groundwork necessary for later exercises in the lab. There are a variety of useful links near the end of the exercise, which will help you to ‘gather up’ the information on syntax, basic procedures, etc., that are needed to write the code. Look at these carefully. We have, however, refrained from giving you a step-by-step recipe to follow; an approach like that would, ultimately, limit improvement in your understanding and basic grasp of the fundamentalsand hurt your confidence later when you have to tackle much harder problems.

You will also be using MathCAD or Excel to produce graphical output of data. In later exercises, this may extend to simple, preparatory analysis as well. However, the main focus in the lab will be on gaining experience, and confidence, programming in C++.

The problem: ballistic motion of a projectile

A projectile is launched at an angle  with respect to a horizontal plane, with an initial velocity u (Figure 1). For any given u, the horizontal distance to impact, X, the maximum height reached, Y, and the time taken to reach maximum altitude, T, are all functions of the angle. These expressions, given in terms of u,  and the acceleration due to gravity, g, are straightforwardly derived from Newton’s laws of motion.

Figure 1: Ballistic trajectory of projectile

The problemwhat is required?

  1. Write a C++ program to output a four-column file, containing estimates of X, Y and T as a function of . Do so for a velocity u of your choosing.
  1. Use MathCAD or Excel to produce carefully labelled plots of each parameter versus .
  1. Produce a new version of your code to output, as a function of time, the speed for the case where u = 35 m/s and  = 32.5 degrees. (This will require you to derive a new expression.) Again, produce a carefully labelled plot of the results.

Your write-upwhat is needed?

  1. You should include in your notebook clearly laid out derivations of the expressions for X, Y and T. [1.5 marks]
  1. You should include a printout of your code, and a brief description of it. Detail should be sufficient to allow the marker to easily follow, and understand, the logic and function of the code. The code itself should be well commented. [3 marks]
  1. You should include the plots of each of X, Y and T against . [1.5 marks]
  1. Deduce from the plot of X against  the angle of launch that gives the largest distance to impact. Support this conclusion with an algebraic proof, which makes use of the equations of motion. [1.5 mark]
  1. You should also include the plot of speed against time, and a derivation of the expression for the speed. [1.5 marks]
Getting started: some useful information

To help you get started, this section contains some useful links, including pages from the Y1 C++ course, plus a few key reminders. In this exercise, and those to follow in the lab, the Y1 course pages should be used as your main information resource on C++. Internet links to this course are shown below by the ‘links’ graphic.

Where do I start?

Begin by mapping out a flow chart of the basic calculations, or functions (‘collection’ of calculations or individual procedures), which the program will require. Think about the order things will need to be done in.

‘Algorithms’

Examples of some simple programs, to help jog your memory, are:

‘A simple C++ program’

‘Some examples of simple C++ codes’ [From the module pages]

Some key ingredients

  1. You must declare all variables you use; remember, these come in different types. You need to think about which type is best suited to the job of describing a parameter, e.g., is it an integer or a real number?

‘Declaration of variables’

  1. You will need to perform some basic algebraic and trigonometric calculations.

‘The mathematical function library in C++’

  1. The problem requires that you repeat the basic computations many times over, for all different possible angles of launch. The way to do this in the code is to use a loop, containing a ‘for’ statement. This will loop back through the same bit of codeto repeat calculations, as requiredwhile at the same time you increment the value of the angle used in them.

‘The for statement’

‘Increment and decrement operators’

  1. Dependent on how you output the information to file, you may need to store the values computed at all angles in arrays. If you output ‘on the fly’, in the loop in which your calculations are performed, you only need store the parameters as individual variables. Next time through the loop they will be over-written. But that does not matter if the values have already been written to file. If output is made outside the calculation loop, you will need to keep all the values in arrays. At some stage during the module you will need to incorporate arrays into your programming.

‘Arrays’

  1. You will need to output your results to file. Information on how to do this is contained in:

‘Streams and External Files’

‘Writing to an output file’ [From the module pages]