Suppose we need an approximation of f(x) using f and its partial

advertisement
Chapter 6
________________________________________________________________
APPLICATIONS OF COMPUTER ALGEBRA IN SURVEYING
COMPUTATIONS
6.1
Introduction
Although Solver handles many adjustment problems efficiently, it may be unsuitable
in certain situations, e.g. when analytical derivation is needed. Moreover, one should be able
to check spreadsheet results using independent methods. Hence this chapter provides the
analytical, calculus-based theory behind variation of coordinates and its numerical
implementation, while introducing a symbolic computation package eminently suitable for
such work. The mathematics needed (Taylor-series expansion of vector functions) is
provided in Section (6.2), while (6.3) covers resection adjustment as a generic problem to be
solved analytically Mathematica implementation is given in (6.4), while (6.5) further
illustrates the power of computer algebra via applications in other surveying / geodesy topics.
6.2
Taylor Series Expansion of a Function with n Independent Variables
Consider, in Rn, two nearby points x = (x1, x2, … xn)T and x0, where x = x0 + x. Note
that x is the small displacement needed to reach x from x0. For a smooth (scalar) function
f(x), Taylor’s Theorem gives an approximation of f(x) using (i) values of f and its first
derivative, both evaluated at x0, and (ii) the small quantities x, as follows:
f(x)  f(x0 + x) = f(x0) +
n
f
 x
j 1
x j + H.O.T.
(6.1)
j x x
0
where H.O.T. = “Higher Order Terms” containing products such as x1x2 or higher powers
of these small x’s; H.O.T. should therefore contribute less than the first-order terms.
To approximate not only one, but m functions f1(x), f2(x),…, fm(x), they can be collectively
viewed as components of a vector f(x). Then, applying (6.1) to each component of f(x),
 f1
 x
 1
f(x) = f(x0 + x) = f(x0) +  f 2
 x1

.....
 f m
 x
 1
f1
f1 
.....
x2
xn 

f 2 
f 2
x + H.O.T.
.....
xn 
x2

.....
..... 
f m 
......
xn  x  x
, or
0
f(x) = f(x0) + A x + H.O.T.
(6.2a)
where
Aij = f i
x j
(6.2b)
x  x0
i.e. matrix A gets its i-j-th entry by evaluating the derivative of i-th function w.r.t. j-th
variable at the provisional coordinates, where i = 1,…, m and j = 1,…, n.
6.3
Variation of Coordinates via Series Expansion
Returning to familiar surveying terms, consider a resection situation with redundant
targets, such as that depicted in Section 3.5.2. Many (m) angles are measured, while the
objective is to obtain the best set of (n) coordinates (i.e. eastings and northings) for the
unknown station(s), fitting the observed data as closely as possible. We assume that
redundant data are taken, hence m > n. The observed data are arranged into a column vector
1 
 
=  2
 ... 
 
 m 
The least-squares solution for the coordinates will be denoted by x, which is unknown at this
point. For the example in Section 3.5.2, x is just [EU, NU]T, thus n = 2. For a general survey
network involving multiple unknown stations, x would be a longer list of coordinates and
hence n > 2. In any case, the least-squares condition can be stated as
  f(x)
(6.3)
where the “” sign means “is to have minimum squared differences from…”. The RHS of
(6.3) are the calculated values of the angles (or/and distances) based on the best coordinates
x. In words, (6.3) requires that the best coordinates x must minimize the total squared
residuals between the observed values and their calculated versions (given by formulas such
as (6.4)). Except for leveling, (6.3) are difficult to solve directly due to their non-linear
nature: for angular observations, f is usually an ArcCos (or the difference of two ArcTan’s
though not recommended); for length measurements, f would involve the  and ( )2 functions.
As a concrete example, let f1 denote the calculated value of angle A-U-B (i.e.1) in Fig. 3.13,
then
UA  UB  UA UB cos f1
As a function of coordinates, angle f1 is therefore

E A  EU E B  EU   N A  NU N B  NU 
f1 ( EU , N U )  arccos 
 E A  EU 2  N A  N U 2 E B  EU 2  N B  N U 2





(6.4)
To find the solution x, it is useful to adopt a new viewpoint first. x must be near the
provisional coordinates x0 assuming only small random errors in the survey. Let the unknown
but small separation be x, i.e.
x = x0 + x
It suffices to solve the equivalent problem of finding x, the appropriate “correction vector”
to add to x0 to obtain x. To solve for anything usually requires equation(s) on it, and the
governing equations for x are derived by expanding RHS of (6.3) using (6.2), as follows:
  f(x0 + x)
or
  f(x0) + A x + H.O.T.
i.e.
A x – [ – f(x0)] + H.O.T.  0
(6.5)
 contains observed numbers, while A and f(x0) also contain nothing but numerical values
after evaluating f and its derivatives at the provisional coordinates. The only unknown is x,
appearing in (6.5) not only linearly after A , but also in higher powers in H.O.T. Rephrasing
(6.5) as a more familiar looking equation for x,
Minimize || A x – k + H.O.T. ||2
(6.6)
where k   – f(x0) is a m1 vector containing numbers only. If we compare and contrast
(6.6) with (1.3), there is “almost” a solution. The smallness of x must be exploited now: not
being able to solve (6.6) fully as it stands, let us ignore the H.O.T. for the moment, reducing
the problem to a linear one solved by (1.5) [or (1.6) if the problem has weights]. Of course,
the solution so obtained will not solve (6.6) exactly, but since we have modified the
problem only slightly (dropping terms at least an order smaller than those kept), the
solution should also only differ slightly. If unconvinced, one may try a quadratic equation
with a tiny cubic term added, for example, x2 + 9x + 5 + 0.0003x3 = 0; an approximate
solution (given by the quadratic formula with a = 1, b = 9, c = 5) should not deviate from the
full solution by much. The idea is illustrated in Fig. 6.1. For weighted problems, suppose
each angular observation i has weight wi, and therefore the associated diagonal weight
matrix is W = Diag[w1, w2, …, wm]. The “first-order” solution using (1.6) is then

T

x = A W A
1
T
A Wk
(6.7)
Though approximate, this solution (shown in purple in Fig. 6.1) is still very useful as it helps
construct a new and better set of provisional coordinates:
xnew = x0 + x
(6.8)
The LHS of (6.8) can now be used as a new x0 in (6.6), which is closer to x than before. Fig.
6.1 illustrates this process, showing that the provisional solution becomes closer to the least
square one after “updating”. The aforementioned procedure and Fig. (6.1) is repeated until a
tion
al so
lu
ision
prov
(ex
ac
t)
Old
x0
x0 =
x
ut
,b )
ate to x
xim er
pro los
(ap you c
ts
x = least squar
es
x
ge
Fig. 6.1
New
x0
x
solution
desired convergence is achieved, say, when none of the updated coordinates will differ by
more than 0.1 mm from previous values. This process is called variation of coordinates, and
naturally calls for computer programming.
Improvement of provisional coordinates using (approximate) x
However, before proceeding to any programming, it is noted from (6.4) that since 1
is a rather nested and non-linear function of EU and NU, hence the derivatives in (6.2b) could
be lengthy and error-prone to obtain by hand. The work must also be repeated many times for
other measured angles and/or lengths. Ordinary programming languages such as Fortran or
BASIC can do little to alleviate this computational burden, hence we advocate the use of
symbolic computation software (e.g. Mathematica, Maple V, REDUCE, DERIVE,
MACSYMA, MuMath, etc.) to perform the analytical (and numerical) work. A sample
Mathematica worksheet for resection adjustments on the example in Section 3.5.2 is included
in the next section. Adequate comment lines are given, so that one should be able to learn the
basic Mathematica commands involved by way of example. As familiarity and interest grow,
the reader should then be able to solve other adjustment problems using Mathematica, and
will probably find such software useful for other areas of his/her academic or professional
career as well.
Exercises
1. Symbolic computation software is recommended for this problem.
(a) The distance formula between two stations i and j is
Dij 
x
 xi    y j  y i 
2
j
2
Derive the “linearized form” of this equation, i.e. to first order, what is the change
in Dij given small changes in the 4 coordinate variables xi, yi, xj, and yj?
(b) The azimuth formula between two stations i and j is
 x j  xi 


y

y
j
i


 ij  tan 1 
Derive the linearized form of this equation in terms of the coordinate variables.
Can you suggest any disadvantages of doing (a) and (b) by the traditional penciland-paper approach?
2. Use Mathematica to perform variation of coordinates for the quadrilateral given in
Smith and Varnes (1987), which was adjusted by Excel in Chapter 3. The following
are requested:
 Stop the iterations when the adjusted coordinates do not change by more than 0.1
mm in any direction.
 Use the same angular weights as given in Table 3.2, but give all lengths a weight
of 100g/10 + e12-g, where g = a random number between 1 and 24.
Include all input and output, and add adequate comment statements to explain your
program. Also find out how you can make Mathematica give answers to n decimal
places for n > 2. Reporting answers to 3 decimal places (coordinates in meters) is
common in surveying.
A Quick Mathematica Guide
You can download a trial version of Mathematica at:
http://www.wolfram.com/products/mathematica/trial.cgi
Input Basics:
 Double click Mathematica icon and you will see the input screen (“notebook”).
 Type in commands and brackets appear on the right side of the notebook (“cell brackets”)
 Usually, you put groups of commands in different cell brackets, so you can run each cell
bracket individually. Put related commands together in the same bracket so that it is
easier to run the program with just a few brackets.
 To create a new separate bracket: click below the last command so that a horizontal line
appears, and start typing below it.
 To clear all variables: enter Remove["Global`*"]
Syntax basics:

Built-in Mathematica functions and constants are always capitalized;
 Always use square brackets [ ], not ( ), to enclose the argument of a function;
 Variable names are case-sensitive.
Examples: I, E, Pi, Infinity;
Exp[x],Sqrt[x],Log[x],Sin[x],Cos[x],Tan[x],Cot[x],ArcSin[x],ArcCos[x],ArcTan[x],ArcCot[
x],Abs[x]

Parentheses ( ) are only used to group arithmetic expressions, e.g. (a+b)/12
 = is used for assignment, e.g. x=3
 Use == to define equations (to be solved, usually), e.g.
Solve[x^2==5,x]
To use the last output, you can use % to represent it:
In: 2+3
Out: 5
In: 2* %
Out: 10
N[…] give the approximate value of the argument
Defining your own functions:
The syntax is slightly tricky; remember two things:
1. There must be an underscore (_) after each variable in square brackets on LHS (but
not on the RHS);
2. You must use := not = to join LHS and RHS.
Examples:
MyFun[x_]:= x^3
Tom[r_,s_]:= r + s^2
Vectors: entered as “Lists”:
A list is a pair of curly brackets {} enclosing the components separated by commas:
y = {1,2,3} v = {a,b,c}
You can do simple component-wise operations on lists using +, -, *, /, ^, for example:
y+v gives {1+a,2+b,3+c}; y-v gives {1-a,2-b,3-c}; y*2 gives {2,4,6}; y^2 gives {1,4,9}
v/3 gives {a//3, b/3, c/3}
You can also use Table to prepare a list whose components are a function of the index:
v1 Table i^2, i, 1, 3
1, 4, 9
SubscriptBox k, 1
DisplayForm
make subscript
k1
v2 Table SubscriptBox x, i
DisplayForm, i, 1, 5
x1, x2, x3, x4, x5
Join v1, v2
make a longer list by joining two
1, 4, 9, x1, x2, x3, x4, x5
Matrices: entered as “List of lists”: e.g.
m
1, 0, 0 , 0, 2, 0 , 0, 0, 3
1, 0, 0 , 0, 2, 0 , 0, 0, 3
MatrixForm m
1 0 0
0 2 0
0 0 3
Easier way to create a diagonal matrix:
DiagonalMatrix 1, 2, 3
1, 0, 0 , 0, 2, 0 , 0, 0, 3
MatrixForm %
1 0 0
0 2 0
0 0 3
You can also do multiply vectors, invert them, etc.:
q
DiagonalMatrix
a, b, c
a, 0, 0 , 0, b, 0 , 0, 0, c
w
1, 2, 3
1, 2, 3
q.w
vector product
a, 2 b, 3 c
w.w
dot product
14
qq
a, b , c, d
a, b , c, d
Inverse qq
matrix inverse
d
bc ad
b
,
bc
ad
c
,
bc ad
,
a
bc
ad
Substitution rule:
Expression /. {Var1 -> value1, Var2 -> value2,…}
Replaces all the mentioned variables in Expression by the stated values, e.g. to turn certain
symbolic variables into numeric values.
v1 Table m ^i, i, 1, 3
2
3
m, m , m
v1 . m 3
3, 9, 27
Useful built-in functions:
Plot gives a plot of a function, with the following basic syntax:
Plot[f[x], {x, xmin, xmax}]
Plot Sin x^2 , x, Pi, Pi
1
0.5
-3
-2
-1
1
-0.5
-1
2
3
Graphics
D, Integrate, Simplify are all very useful functions for differentiation, integration and
simplification:
D ArcTan x , x
1
1 x2
Integrate 1
ArcTan
1 x ^4 , x
2 2x
2 2x
ArcTan
2
2
2 2
Simplify %
2 ArcTan 1
2
2 x
Log
2
2 ArcTan 1
2 x x2
1
4
2 x
4
Log
2
1
DSolve y'' x
x, y x , x
3
x
y x
C 1
xC 2
6
2 x
4
2 x x2
2
DSolve solves differential equations entered in the following format:
DSolve [equation, y, x]
Log 1
Log 1
x2
2
2 x
x2
Download