• No se han encontrado resultados

OBJECIONES PERSONALES

In document APOLOGÉTICA EXPOSITIVA (página 160-167)

Predicando y Enseñando Como un Apologeta Expositivo

OBJECIONES PERSONALES

So far we have used ode45 for a system of first-order equations and for a single second-order equation. The next logical step is a system of second-order equa-tions, and the next logical example is a projectile. A “projectile” is an object propelled through space, usually toward, and often to the detriment of, a target.

If a projectile stays in a plane, we can think of the system as two-dimensional, with x representing the horizontal distance traveled and y representing the height or altitude. So now instead of a skydiver, think of a circus performer being fired out of a cannon.

According to the Wikipedia, the record distance for a human cannonball is 56.5 meters (almost 186 feet).

http://en.wikipedia.org/wiki/Human_cannonball

10.6 Two dimensions 121

Here is a general framework for computing the trajectory of a projectile in two dimensions using ode45:

function res = projectile(t, W) P = W(1:2);

V = W(3:4);

dPdt = V;

dVdt = acceleration(t, P, V);

res = [dPdt; dVdt];

end

function res = acceleration(t, P, V)

g = -9.8; % acceleration of gravity in m/s^2 res = [0; g];

end

The second argument of the rate function is a vector, W, with four elements. The first two are assigned to P, which represents position; the last two are assigned to V, which represents velocity. P and V are vectors with elements for the x and y components.

The result from acceleration is also a vector; ignoring air resistance (for now), the acceleration in the x direction is 0; in the y direction it’s g. Other than that, this code is similar to what we saw in Section 10.3.

If we launch the human projectile from an initial height of 3 meters, with ve-locities 40 m/s and 30 m/s in the x and y directions, the ode45 call looks like this:

ode45(@projectile, [0,10], [0, 3, 40, 30]);

And the result looks like this:

0 1 2 3 4 5 6 7

You might have to think a little to figure out which line is which. It looks like the flight time is about 6 seconds.

122 Second-order systems

Exercise 10.3 Extract the x and y components of position, plot the trajectory of the projectile, and estimate the distance traveled.

Exercise 10.4 Add air resistance to this simulation. In the skydiver scenario, we estimated that the drag constant was 0.2, but that was based on the assump-tion that the skydiver is falling flat. A human cannonball, flying head-first, probably has a drag constant closer to 0.1. What initial velocity is needed to achieve the record flight distance of 65.6 meters? Hint: what is the optimal launch angle?

10.7 What could go wrong?

What could go wrong? Well, vertcat for one. To explain what that means, I’ll start with catenation, which is the operation of joining two matrices into a larger matrix. “Vertical catenation” joins the matrices by stacking them on top of each other; “horizontal catenation” lays them side by side.

Here’s an example of horizontal catenation with row vectors:

>> x = 1:3

x = 1 2 3

>> y = 4:5

y = 4 5

>> z = [x, y]

z = 1 2 3 4 5

Inside brackets, the comma operator performs horizontal catenation. The ver-tical catenation operator is the semi-colon. Here is an example with matrices:

>> X = zeros(2,3)

X = 0 0 0

0 0 0

>> Y = ones(2,3)

Y = 1 1 1

1 1 1

>> Z = [X; Y]

10.7 What could go wrong? 123

Z = 0 0 0

0 0 0

1 1 1

1 1 1

These operations only work if the matrices are the same size along the dimension where they are glued together. If not, you get:

>> a = 1:3

a = 1 2 3

>> b = a’

b = 1 2 3

>> c = [a, b]

??? Error using ==> horzcat

All matrices on a row in the bracketed expression must have the same number of rows.

>> c = [a; b]

??? Error using ==> vertcat

All rows in the bracketed expression must have the same number of columns.

In this example, a is a row vector and b is a column vector, so they can’t be catenated in either direction.

Reading the error messages, you probably guessed that horzcat is the function that performs horizontal catenation, and likewise with vertcat and vertical catenation.

These operations are relevant to projectile because of the last line, which packs dPdt and dVdt into the output variable:

function res = projectile(t, W) P = W(1:2);

V = W(3:4);

dPdt = V;

dVdt = acceleration(t, P, V);

res = [dPdt; dVdt];

end

As long as both dPdt and dVdt are column vectors, the semi-colon performs

124 Second-order systems

vertical catenation, and the result is a column vector with four elements. But if either of them is a row vector, that’s trouble.

ode45expects the result from projectile to be a column vector, so if you are working with ode45, it is probably a good idea to make everything a column vector.

In general, if you run into problems with horzcat and vertcat, use size to display the dimensions of the operands, and make sure you are clear on which way your vectors go.

10.8 Glossary

parallel functions: Two or more functions defined side-by-side, so that one ends before the next begins.

nested function: A function defined inside another function.

outer function: A function that contains another function definition.

inner function: A function defined inside another function definition. The inner function can access the variables of the outer function.

catenation: The operation of joining two matrices end-to-end to form a new matrix.

10.9 Exercises

Exercise 10.5 The flight of a baseball is governed by three forces: gravity, drag due to air resistance, and Magnus force due to spin. If we ignore wind and Magnus force, the path of the baseball stays in a plane, so we can model it as a projectile in two dimensions.

A simple model of the drag of a baseball is:

Fd= −1

2 ρ v2 A Cd

where Fd is a vector that represents the force on the baseball due to drag, Cd

is the drag coefficient (0.3 is a reasonable choice), ρ is the density of air (1.3 kg/m3 at sea level), A is the cross sectional area of the baseball (0.0042 m2), v is the magnitude of the velocity vector, and ˆV is a unit vector in the direction of the velocity vector. The mass of the baseball is 0.145 kg.

For more information about drag, see http: // en. wikipedia. org/ wiki/

Drag_ ( physics).

10.9 Exercises 125

• Write a function that takes the initial velocity of the baseball and the launch angle as input variables, uses ode45 to compute the trajectory, and returns the range (horizontal distance in flight) as an output variable.

• Write a function that takes the initial velocity of the baseball as an input variable, computes the launch angle that maximizes the range, and returns the optimal angle and range as output variables. How does the optimal angle vary with initial velocity?

• When the Red Sox won the World Series in 2007, they played the Colorado Rockies at their home field in Denver, Colorado. Find an estimate of the density of air in the Mile High City. What effect does this have on drag?

Make a prediction about what effect this will have on the optimal launch angle, and then use your simulation to test your prediction.

• The Green Monster in Fenway Park is about 12 m high and about 97 m from home plate along the left field line. What is the minimum speed a ball must leave the bat in order to clear the monster (assuming it goes off at the optimal angle)? Do you think it is possible for a person to stand on home plate and throw a ball over the Green Monster?

• The actual drag on a baseball is more complicated than what is captured by our simple model. In particular, the drag coefficient varies with velocity.

You can get some of the details from The Physics of Baseball; you also might find information on the web. Either way, specify a more realistic model of drag and modify your program to implement it. How big is the effect on your computed ranges? How big is the effect on the optimal angles?

Robert K. Adair, Harper Paperbacks, 3rd Edition, 2002.

126 Second-order systems

Chapter 11

Optimization and Interpolation

11.1 ODE Events

Normally when you call ode45 you have to specify a start time and an end time. But in many cases, you don’t know ahead of time when the simulation should end. Fortunately MATLAB provides a mechanism for dealing with this problem. The bad news is that it is a little awkward. Here’s how it works:

1. Before calling ode45 you use odeset to create an object called options that contains values that control how ode45 works:

options = odeset(’Events’, @events);

In this case, the name of the option is Events and the value is a function handle. When ode45 runs, it will invoke events after each timestep.

You can call this function anything you want, but the name events is conventional.

2. The function you provide has to take the same input variables as your rate function. For example, here is an event function that would work with projectile from Section 10.6

function [value,isterminal,direction] = events(t,X) value = X(2); % Extract the current height.

isterminal = 1; % Stop the integration if height crosses zero.

direction = -1; % But only if the height is decreasing.

end

128 Optimization and Interpolation

eventsreturns three output variables:

valuedetermines when an event occurs. In this case value gets the second element of X, which is understood to be the height of the projectile. An

“event” is a point in time when this value passes through 0.

directiondetermines whether an event occurs when value is increasing (direction=1), decreasing (direction=-1, or both direction=0.

isterminal determines what happens when an event occurs. If isterminal=1, the event is “terminal” and the simulation stops. If isterminal=0, the simulation continues, but ode45 does some additional work to make sure that the solution in the vicinity of the event is accurate, and that one of the estimated values in the result is at the time of the event.

3. When you call ode45, you pass options as a fourth argument:

ode45(@projectile, [0,10], [0, 3, 40, 30], options);

Exercise 11.1 How would you modify events to stop when the height of the projectile falls through 3m?

In document APOLOGÉTICA EXPOSITIVA (página 160-167)