This lab has four parts: Parts A to D. The four parts together form a sequence of tasks. These parts are not independent (like the previous labs) and should be done one after another. You need to show your tutors all the four parts. All scripts and functions should be reasonably documented.
The on-line assessment is about for-loop, and can be done at any time.
You should make a directory called lab09 to store your files for this lab.
Remark: The real-life application of this problem is missiles. For this lab, let us be innocent and consider strawberries and aliens instead.
The starting point of simulation is mathematical modelling. In ENGG1811, we will always give you the mathematical models. After you have learned enough knowledge in your own discipline, you will be able to come out with your own mathematical model for the work that you do.
The motion of the strawberries in the air is affected by two factors: gravitational pull and air resistance. A common model for air resistance is that the resistive force is proportional to total speed of the object. We assume the launcher, strawberries and alien live in a two-dimension space. The horizontal direction is the x-direction and the vertical direction is the y-direction. The position of the strawberry is therefore defined by its x- and y-coordinates. We use px(t) and py(t) to denote the x- and y-coordinates of the strawberry at time t. In addition, we use vx(t) and vy(t) to denote the velocity of the strawberry in x- and y-directions. The motion of the strawberry obeys the following set of ordinary differential equations:
where m and c are respectively the mass and drag coefficient, and g is the acceleration due to gravity. Since the strawberry has an x- and a y-component in velocity, its total speed is given by √(vx(t)2 + vy(t)2). If you are interested, Equations (3) and (4) are derived from Newton's Second Law. Equation (3) says that the strawberry experiences resistance as it moves in the air in the x-direction. Equation (4) says that the strawberry experiences both gravitational pull and air resistance when it moves in the y-direction.
Given Equations (1)-(4), you can use Euler's forward rule to derive how to calculate the position and velocity at time t + Δ from that at time t. We have derived the equations for you, as follows:
The simulation in Part A allows you to determine the trajectory of the strawberry launched at a given speed and angle. Let us now assume that the alien is located at a height (y-coordinate) of +10, and the alien's can only swallow the strawberries when they are falling down. The trajectory plot in Part A shows that, with a launch angle of 35 degrees, a rough estimate of the landing position of the strawberries will be at the coordinates (96,10). This position is obtained from using visual inspection from the graph. Your task for this Part is to use Matlab to automatically determine the landing position (assuming a landing height of 10) as well as the time to reach the landing position, from the trajectories computed in Part A.We will define the exact meaning of landing using an example in a moment.
We will give you some idea how you can use Matlab to find the landing time and position. The trajectory has 501 numbers per vector so they may be hard to work with. So we will use a small example to illustrate the method. This is a typical problem solving technique. If you can solve the problem for a small example, then you can transfer the solution to a larger instance of the problem. Let us assume that you have three vectors px, py and tv, which have analogous meaning to vecPositionX, vecPositionY and vecTime that we have used above. We made up some values for the elements of these three vectors:
tv = [ 0.6 0.7 1.5 1.6 1.7 1.8 1.9]
px = [ 2.4 2.9 5.7 6.6 7.5 8.4 9.3]
py = [ 9.7 10.1 10.5 10.2 10.1 9.9 9.8]
Note that this example contains the main feature of the problem with the positions in the y-direction dropping from a value above 10 to a value below 10. It means that at some point in time, the strawberry has a height of exactly 10. Instead of determining the exact time of crossing, we use an approximation here. First, we consider only the time instances in the vector tv. Second, we say that landing occurs at time t if the vertical position of the strawberry at time t is closest to the level of 10, but is still at or above the level of 10. For this example, landing occurs at time instant 1.7 because the strawberry is just above the level 10, at a height of 10.1 to be specific. At the next time instance, at time 1.8, the strawberry is below the level of 10, at 9.9 to be specific. This is not exact, but if the time increment is small, the error will not be big. (This is also on the condition that the alien opens its mouth wide!) We say that the landing distance (which measures position in the x-direction) is 7.5 and the landing time is 1.7.
We suggest that you work with this example first to find out what Matlab statements you need to determine the landing distance and landing time from tv, px and py. Once you get this going, you can test whether it works for the vecTime, vecPositionX and vecPositionY vectors.
You can use any method to solve this problem as long as it can be automated. Having done some programming, you may think that you need some loops and if statements to solve this problem. Yes, you can do that, but I want to say that this problem has some special features and you can solve this problem using the Matlab built-in functions find and max. If you do not know how to use these functions, you can click on the help button (white question mark on a blue circle) to look at the help pages of these functions. If you want some more hint, you can click here.
You should try to do this Part using a script, because you will need to write a function to do these steps in Part C. You can keep the script to show your tutor.
Once you have got Part B working, you should write a function to determine the landing distance and landing time (these are the outputs) from the Matlab vectors vecTime, vecPositionX and vecPositionY and a scalar landingLevel (these are the inputs). Note that landingLevel is 10 but we should try to make the function general so that it can work with any landing level.
The beginning of your function should look like this:
[landingPosition,landingTime] = ...
First, let us recap what you have achieved by now.
By combining what you have in Parts A and C, you can determine, for a given launch angle, what the landing position and landing time are. What this means is that, you have all the functions that you need to study how the launch angle impacts on landing position and landing time.
In this part, your task is to write a Matlab script that does the following:
This script does some calculations repeatedly so you need some type of loops (which type?). Once you get your script working, you can plot a graph of landing distance versus landing time. You should get a graph similar to that below.
Let us assume that the alien is at a horizontal distance of 90 from the launcher. You can see from the graph that there are two launch angles that will allow you to feed the alien and one of them will allow you to feed the alien quicker. Your final task is to determine the launch angle to use by using Matlab statements. You can add these statements to the script and write a Matlab disp statement to display the answer. We do not need a very accurate answer. As long as the launch angle is within 1 degree of the correct value, that is acceptable. You can use any method you like to determine the launch angle. The type of method you used in Part B is also helpful here.
--- Happy feeding! ---