Python辅导 | MATH 3018/6141 – Numerical methods


Coursework 2: MATH 3018/6141 – Numerical methods
Due: 15:00 Thursday 12th December 2019
In this coursework you will implement given numerical algorithms. The assessment
will be based on
• correct implementation of the algorithms to solve the problems (task 1) – 5
• check simple case (task 2) and more complex case (task 3) in 2d and in 3d (task
4) – 2, 1, and 2 marks
• choice and justification of algorithms – 2 marks
• documentation of code, unit testing, robustness and error checking of code – 3
The deadline is 15:00, Thursday 12th December 2019 (week 11). For late submissions there is a penalty of 10% of the total marks for the assignment per day after the
assignment is due, for up to 5 days. No marks will be obtained for submissions that are
later than 5 days.
Your work must be submitted electronically via Blackboard. Only the Python files
needed to produce the output specified in the tasks below is required.
Figure 1: Locations of 20 hairs in the x, z plane under the force of
gravity alone. The boundary value problem for each hair is solved
by fixing the location where the hair meets the head.
1 Modelling hair
A major use of numerical methods is in the creation of computer graphics in films,
games, and other media. The generation of “realistic” animations of, for example,
smoke, or water, or explosions, are typical problems. One case that remains difficult is
the simulation of hair, particularly under external forces such as wind and rain.
2 Model
The first reasonable model of human hair used the beam equation applied to each hair
individually. This coursework follows that model. The model we use here makes a
number of simplifying assumptions:
1. The human head is a sphere with radius 10 cm.
2. Each hair meets the head normal to the sphere.
3. The forces acting on each hair are gravity (in the −z direction) and a constant
wind (in the +x direction).
We want to contruct the location of each hair. We will use the parameter s, which
measures arclength along the hair, with s = 0 being the point where the hair meets
the head and s = L being the point at the free end of the hair. Here we assume that
L = 4 cm is the constant hair length.
The model works separately for each hair. It uses a mixture of coordinate systems.
The location of the hair will be displayed in the (x, y, z) coordinate system. The location of the hair will depend on the value of the independent variable s along its length.
The independent variable s takes the value s = L at the free end of the hair and, at the
location where the hair meets the head, takes the value s = 0. The spatial position that
the hair joins the head (at s = 0) is given by
x(0) = R cos(θ0) cos(φ0), (1a)
y(0) = −R cos(θ0) sin(φ0), (1b)
z(0) = R sin(θ0). (1c)
Here θ0 and φ0 are coordinates on the head: θ0 is the latitude (angle away from horizontal) and φ0 is longitude (angle away from the x-direction).
In contrast the angles θ(s) and φ(s) describe the local orientation (in space) of a
given hair. The fact that the hair comes out of the head at right angles is given by the
boundary conditions
θ(0) = θ0, (2a)
φ(0) = φ0. (2b)
The hair then bends under the influence of gravity and the wind force. Bending is
described by the orientation (θ(s), φ(s)) changing with s, as we go along the length of
the hair. A completely rigid hair would have constant θ(s) and φ(s). The simplified
bending model leads to a system of 2 ODEs for each individual hair, which read
= sfg cos(θ) + sfx cos(φ) sin(θ), (3a)

= −sfx sin(φ) sin(θ). (3b)
Here fg is a parameter describing the force from gravity relative to the bending stiffness
of the hair. An appropriate value for human hair is fg = 0.1 cm−3
and we use this here.
The other parameter, fx, describes the force from the wind relative to the bending
stiffness of the hair. In addition, the hair can move freely at its end (at s = L), which
leads to the boundary conditions

(L) = 0, (4a)

(L) = 0. (4b)
We can determine θ(s) and φ(s), by solving the BVP defined in equation (3) with the
two sets of boundary conditions found in equations (2) and (4). Once the angles θ(s)
and φ(s) have been determined, the (x, y, z) coordinates of the hair can be found by
solving the ODEs
= cos(θ) cos(φ), (5a)
= − cos(θ) sin(φ), (5b)
= sin(θ), (5c)
with initial conditions given by the location at which the hair emerges from the head
via equation (1).
Note: When the initial angle φ0 ≡ 0 the system simplifies, as φ(s) ≡ 0. In this
case we need only solve the second order boundary value problem corresponding to
equation (3a) with boundary conditions (2a) and (4a).
3 Task
1. First assume that φ0 ≡ 0. Write a function that, given L, R, fx, fg, and a list
of values of θ0 which specify where the hairs meet the head, returns the (x, z)
coordinates of the hairs.
2. Fix φ = 0 (work within the (x, z) plane) and fx = 0 (no force due to the wind).
We fix fg = 0.1 throughout. Compute and plot the location of 20 different hairs,
with L = 4, which meet the head with position (determined by equation (1))
with values of θ0 evenly spaced on the interval [0, π]. Plot the locations in the
(x, z) plane.
3. Repeat the previous task with fx = 0.1.
4. Now allow φ0 to vary freely. Write a function that given L, R, fx, fg, and a list
of values of θ0 and φ0 which specify where the hairs meet the head, returns the
(x, y, z) coordinates of the hairs.
5. Compute the locations of 100 hairs, with the force due to the wind set to fx =
0.05. These should meet the head at a 10 × 10 grid of θ, φ locations, where θ0 is
evenly spaced on [0, 0.49 π] and φ0 is evenly spaced on [0, π]. Plot the results in
the (x, z) plane, the (y, z) plane, and in 3 dimensions.
3.1 Summary and assessment criteria
You should submit the code electronically as noted above.
The primary assessment criteria will be a general code that solves the hair-modelling
problem. In top-level code comments the reasons for the choice of algorithm should be
clearly given, stating the (dis)advantages of this and alternative methods.
The secondary assessment criteria will be the clarity and robustness of your code.
Your functions and scripts must be well documented with appropriate internal comments describing inputs, outputs, what the function does and how it does it. The code
should also be well structured to make it easy to follow. Input must be checked and sensible error messages given when problems are encountered. The clarity of the output
(such as plots or results printed to the command window) is also important.
Code efficiency is not important unless the algorithm takes an exceptional amount
of time to run.
The original paper that uses a “physical” approach to hair modelling is by Anjyo et
al. from 1992. A survey of hair modelling approaches is given by Bartails et al. (2006).
The particular model used here is a generalisation of Howison’s book on Practical Applied Mathematics. For a broader range of modelling techniques applied to computer
graphics, Ron Fedkiw’s webpage is a good place to start.