Docsity

Approximating Derivatives from Discrete Data: Centered and One-Sided Differences, Lab Reports of Meteorology

Toggle sidebar
bg1
Computing the Gradient: It’s All Uphill From Here
(Atmo 336, Fall 2007)
Getting Started. In a previous lab (see Integrating the Hypsometric Equation—The Trape-
zoidal Rule) we introduced a simple method for computing an integral when all you know is
a discrete set function values at a finite number of points. Here we do the opposite—that
is, instead of computing an integral we’ll compute a derivative. And for our application we
consider this: we suppose that we are given a gridded set of pressure measurements and we
want to compute the gradient of the pressure from our data.
The Method. We’ve actually been computing derivatives from a gridded dataset already—in
our discussion of kinematics. To be specific, we’ve been
1 2
4
3
∂u
∂x ≈u2−u1
∆x
∂u
∂y ≈u4−u3
∆y
etc.
estimating the derivatives of the velocity components
using approximations such as ∂u/∂x ≈(u2−u1)/∆x,
∂u/∂y ≈(u4−u3)/∆y, etc. Here we want to do the same
thing.....we just want to be a bit more careful about it.
A 1D example. Suppose we have a function f(x)
whose value is known at a set of discrete grid points
··· , xi−2∆x , xi−∆x , xi, xi+ ∆x , xi+ 2∆x , ···
as illustrated below. We then want to approximate df/dx using only the known values of fon
the grid.
xi−2∆xxi−∆xxixi+ ∆xxi+ 2∆x
(a) Using Taylor series, show that
f(xi+ ∆x)−f(xi−∆x)
2∆x=df
dx
¯
¯
¯xi
+O((∆x)2) (1)
where the O((∆x)2) term refers to an error term whose size varies with the square of ∆x(times
some other stuff). As ∆xis made smaller and smaller the error will also become smaller and
smaller—quadratically in fact. We therefore expect that for sufficiently small ∆xour formula
(1) provides an accurate approximation of the derivative.
Often we try to simplify matters by using a shorthand notation in which our grid points are
numbered from left to right, starting at one. The ith point on the grid is then xi= (i−1) ∆x
and the points to right and left of this point are xi+ ∆x=xi+1 and xi−∆x=xi−1. To further
simplify things we let the function evaluated at the ith point be denoted by f(xi) = fi. The
pf3
pf4
pf5

Partial preview of the text

Download Approximating Derivatives from Discrete Data: Centered and One-Sided Differences and more Lab Reports Meteorology in PDF only on Docsity!

Computing the Gradient: It’s All Uphill From Here (Atmo 336, Fall 2007)

Getting Started. In a previous lab (see Integrating the Hypsometric Equation—The Trape- zoidal Rule) we introduced a simple method for computing an integral when all you know is a discrete set function values at a finite number of points. Here we do the opposite—that is, instead of computing an integral we’ll compute a derivative. And for our application we consider this: we suppose that we are given a gridded set of pressure measurements and we want to compute the gradient of the pressure from our data.

The Method. We’ve actually been computing derivatives from a gridded dataset already—in our discussion of kinematics. To be specific, we’ve been

1 2

4

3

∂u ∂x ≈^

u 2 −u 1 ∆x ∂u ∂y ≈^

u 4 −u 3 ∆y etc.

estimating the derivatives of the velocity components using approximations such as ∂u/∂x ≈ (u 2 − u 1 )/∆x, ∂u/∂y ≈ (u 4 − u 3 )/∆y, etc. Here we want to do the same thing.....we just want to be a bit more careful about it.

A 1D example. Suppose we have a function f (x) whose value is known at a set of discrete grid points

· · · , xi − 2∆x , xi − ∆x , xi , xi + ∆x , xi + 2∆x , · · ·

as illustrated below. We then want to approximate df /dx using only the known values of f on the grid.

xi − 2∆x xi − ∆x xi xi + ∆x xi + 2∆x

(a) Using Taylor series, show that

f (xi + ∆x) − f (xi − ∆x) 2∆x

df dx

xi

  • O((∆x)^2 ) (1)

where the O((∆x)^2 ) term refers to an error term whose size varies with the square of ∆x (times some other stuff). As ∆x is made smaller and smaller the error will also become smaller and smaller—quadratically in fact. We therefore expect that for sufficiently small ∆x our formula (1) provides an accurate approximation of the derivative.

Often we try to simplify matters by using a shorthand notation in which our grid points are numbered from left to right, starting at one. The ith^ point on the grid is then xi = (i − 1) ∆x and the points to right and left of this point are xi + ∆x = xi+1 and xi − ∆x = xi− 1. To further simplify things we let the function evaluated at the ith^ point be denoted by f (xi) = fi. The

function values at the neighboring points are then f (xi+1) = fi+1 and f (xi− 1 ) = fi− 1. Putting this all together, a shorthand version of our approximation then looks like

df dx

i

fi+1 − fi− 1 2∆x

The one-sided variants. The method described above is referred to as a centered difference approximation, since it approximates a derivative at the ith^ point using the two grid points to either side. (That is, the approximation point xi is centered between the two grid points used in the difference.) Unfortunately, sometimes the data on either side of our point of interest is not actually available. For instance, suppose we’re at the leftmost edge of our 1D grid (i.e., at the point labeled x 1 ). At this point we have a neighboring grid point to our right but no neighboring point to the left. So what do we do now? Well, we’ll just have to modify our approximation.

(b) Show using Taylor series that

f (xi + ∆x) − f (xi) ∆x

df dx

xi

  • O(∆x)

where the notation O(∆x) means that in this case our error varies linearly with ∆x (times some other stuff). For small ∆x this linear approximation is less accurate than our centered difference—since for small ∆x an O((∆x)^2 ) error is smaller than a O(∆x) error—but it has the avantage of only requiring data to our right.

(c) Similarly, show also that

f (xi) − f (xi − ∆x) ∆x

df dx

xi

  • O(∆x)

which then only requires data to the left.

The approximations described in (b) and (c) are referred to as one-sided differences since they require information only from the point to the left of our point of interest or else from the point to the right (but not from both). The advantage of these approximations is that we can use them at the grid edges. But since the one-sided approximations are less accurate than the centered difference (for small ∆x at least), we’ll want to stick with our centered approximation wherever possible.

Finally, in terms of our shorthand notation the one-sided differences look like

df dx

i

fi+1 − fi ∆x

and

df dx

i

fi − fi− 1 ∆x

The 2D Case. The 1D method described above extends to the 2D (or even 3D) case more or less directly. The only change is that now when we take a derivative in the x-direction we

the shared ATMO336 directory to your home directory. The first script is used to define the pressure field, while the second script estimates the error in your gradient calculations. With a few exceptions the two scripts are already done for you and will not need to be modified (again, with a few exceptions).

(d) As our test pressure field we’ll use

p(x, y) = cos(x) cos(y) (7)

You’ll need the gradient of this function later when computing the error, so go ahead and evaluate the gradient now [as a function of (x, y)] and add the result to the get error.m script where indicated. Note that get error.m already knows about the x and y coordinates on the grid. So to indicate the x-coordinate, for instance, you can just use x(i, j). (And similarly y(i, j) gives the y-coordinate.)

Defining the grid. Now to set up our grid. For our computational domain we’ll consider a rectangle given by − 3 π/ 2 ≤ x ≤ π/2 and −π/ 2 ≤ y ≤ π/2. Note that if L = π is the domain width in the y-direction, then the domain width in the x-direction is 2L. We’ll want the grid spacing to be the same in both directions (i.e, we want ∆x = ∆y), so we’ll end up using twice as many grid intervals in the x-direction as in the y-direction (as illustrated below).

2 L = 2N ∆x

L = N ∆y

Now suppose that the computational domain consists of N grid intervals in the y-direction (so that L = N ∆y) and 2N grid intervals in the x-direction (so that 2L = 2N ∆x). And suppose that the number of grid points in the y-direction is ny and the number of grid points in the x-direction is nx. Then how are nx and ny related to N? (No need to write this down.)

The discretized gradient. Your friendly neighborhood TA (FNTA) has given you a function script (defined in get fields.m) that defines the pressure field for you on the grid described above. The function actually defines any of three different pressure fields, depending on the

value of a flag that’s specified in the function call. (Our test problem (7) corresponds to flag = 1). The syntax for the function call looks something like

[x, y, p] = get fields (N, flag);

where N is the number of grid intervals in the y-direction (as above) and where flag is either 1, 2 or 3. The returned variables are then the x-coordinate, the y-coordinate and the pressure field. Each of the returned variables is defined on the grid points and is thus a function of the two indices i and j (i.e., x(i, j), etc).

With that, we can now compute our discretized derivatives. You’ll want to put the following in a script (say gradient.m) since you’ll need to modify various aspects of this calculation later on. To be concrete we’ll let our grid feature 32 intervals in the y-direction and 64 in the x-direction. A script to compute the derivatives might then look like

N = 32; [x, y, p] = get fields(N, 1); dx = ; dy = ; nx = ; ny = ;

%% centered differencing for interior points for i=2:nx- for j=1:ny dpdx(i, j) = ; end end for i=1:nx for j=2:ny- dpdy(i, j) = ; end end

%% one-sided differencing for grid edges for j=1:ny i = 1; dpdx(i, j) = ; i = nx; dpdx(i, j) = ; end for i=1:nx j = 1; dpdy(i, j) = ; j = ny; dpdy(i, j) = ; end

where of course there are lots of things for you to fill in.

script—from 1 to 2 ... and then eventually to 3). Make a plot for each case and save the results to grad 2.jpg and grad 3.jpg. And copy to the figure directory to turn in.

Would your answers to the three questions given above be any different for these cases?

And as always, find the person two seats down to the right and argue over whose figures are best.