Piecewise function
On the surface of the earth the graviational pull is approximately g0 = 9.82 [m/s2 ]. If you move up towards
space the gravitational pull declines with the height, h, above the ground according to the equation
gheight (h) = g0
R2
(R + h)2
(3.3)
where R = 6.371 106 [m] is the average radius of the earth. Similarly if you dig yourself into the ground there
is less mass of the earth underneath you and the graviational pull is weaker and declines as a function of the
depth, d, of the hole you are digging:
d
gdepth (d) = g0 1
(3.4)
R
This is a rough approximation as the earth is not a ball; nor is the density of the earth constant. For more
details, visit the wikipedia page: en.wikipedia.org/wiki/Gravity_of_Earth. These two equations can be
combined to describe the graviational pull at some distance, x = R + h = R d, from the center of the earth.
R2
g0 x2 if R x
(3.5)
gdistance (x) =
g0
if 0 x < R
R
Problem definition
Write a function that, given the distance x to the center of the earth, computes the graviational pull of the
earth.
Solution template
function g = gravitationalPull(x)
% Insert your code here
Input
d
Output
g
Example
Suppose we are given x = 1.78 106 [m] as input to the function. Since 1.78 106 < 6.371 106 , inserting 1.78 106
into equation 3 yields
gdistance (1.78 106 ) = g0
1.78 106
1.78 106
= 9.82
= 2.7436 [m/s2 ]
R
6.371 106
(3.6)
Expected output
gravitationalPull(1.78e6)
ans =
2.7436
Hand in on CodeJudge
The assignment must be handed in on CodeJudge.
Discussion and futher analysis
What will happen if you give a vector of distances as input to your function? Probably it will not work, because
the function expects a single number as input. How could you write the function such that it outputs a vector
of gravitational pull when given a vector of distances as input? Think about how this could be implemented
using vector indexing operations.
If you change your function so that it works correctly with vector input and output, you can try out making a
plot of the function using the following code:
x = 0:1e4:10e6;
plot(x, gravitationalPull(x));
Figure 3.1: A plot of the gravitational pull from the center of the earth to a distance of 10 000.
3D