Docsity

Algorithm and Matlab Implementation for Finding Distance to an Ellipse - Prof. Dianne P. O, Assignments of Mathematics

Toggle sidebar
bg1
CMSC/AMSC 660-0101 Fall 2004
Homework 1: Comments on Solution
1. (10) Derive an algorithm for finding the distance d(z) between a given
point z=[z
1
,z
2
]
Tand a ellipse. In other words, solve the problem
min
x,y (x−z1)2+(y−z
2
)
2
subject to
x
α2
+ y
β!2
=1
for given parameters αand β. Note that the distance is the square root of
the optimal value of the objective function (x−z1)2+(y−z
2)2. The problem
can be solved using Lagrange multipliers, as a calculus student would. You
need only consider points zon or inside the ellipse, but handle all of the
special cases: α=β,zhas a zero coordinate, etc.
Answer: We set up the Lagrangian function
L(x, y, λ)=(x−z
1
)
2+(y−z
2
)
2−λ
x
α
2
+ y
β!2
−1
,
where the scalar λis the Lagrange multiplier for the constraint. Setting the
three partial derivatives to zero yields
2(x−z1)−2λx
α2=0
2(y−z2)−2λy
β2=0
x
α
2
+
y
β
!
2
−1=0
We conclude that
x=α2z1
α2−λ(1)
y=β2z2
β2−λ(2)
1
pf3
pf4
pf5

Partial preview of the text

Download Algorithm and Matlab Implementation for Finding Distance to an Ellipse - Prof. Dianne P. O and more Assignments Mathematics in PDF only on Docsity!

CMSC/AMSC 660-0101 Fall 2004 Homework 1: Comments on Solution

  1. (10) Derive an algorithm for finding the distance d(z) between a given point z = [z 1 , z 2 ]T^ and a ellipse. In other words, solve the problem

minx,y (x − z 1 )^2 + (y − z 2 )^2

subject to ( x α

) 2

( y β

) 2 = 1

for given parameters α and β. Note that the distance is the square root of the optimal value of the objective function (x − z 1 )^2 + (y − z 2 )^2. The problem can be solved using Lagrange multipliers, as a calculus student would. You need only consider points z on or inside the ellipse, but handle all of the special cases: α = β, z has a zero coordinate, etc.

Answer: We set up the Lagrangian function

L(x, y, λ) = (x − z 1 )^2 + (y − z 2 )^2 − λ

 

( x α

) 2

( y β

) 2 − 1

  (^) ,

where the scalar λ is the Lagrange multiplier for the constraint. Setting the three partial derivatives to zero yields

2(x − z 1 ) − 2 λ

x α^2

2(y − z 2 ) − 2 λ

y β^2

( x α

) 2

( y β

) 2 − 1 = 0

We conclude that

x =

α^2 z 1 α^2 − λ

y =

β^2 z 2 β^2 − λ

as long as the denominators are nonzero. Since |x| ≤ α and |y| ≤ β, we conclude that the solution we seek has λ satisfying 0 ≤ λ ≤ min(α^2 , β^2 ). So we can solve our problem by solving the nonlinear equation

f (λ) =

( x α

) 2

( y β

) 2 − 1 = 0

using (1) and (2) to define x(λ) and y(λ).

These formulas are overly complicated when z 1 = 0 or z 2 = 0 and fail when both are zero. There are two points to check, depending on whether it is shorter to move horizontally or vertically to the boundary. When z = 0, for example, then the solution is either (x, y) = (0, β) or (α, 0), depending on whether β or α is smaller. Full details are given in the sample code for Problem 3 and also in a description by David Eberly [1].

  1. (10) Program your distance algorithm in Matlab, document it, and pro- duce a convincing validation of the code by designing a suitable set of tests and discussing the results. Make sure that your program handles the numer- ically difficult cases correctly, for example, when a component of z is near zero.

Answer: Sample code appears on the webpage as dist to ellipse.m. The testing code makes a contour plot of distances on a grid of points in the ellipse. Note that it is important to test points that are near zero.

To validate the code, we might repeat the runs with various values of α and β, and also test the code for a point z outside the ellipse.

Comments on Alternatives:

One idea: use symbolic computation (Maple, within Matlab). The idea is to avoid round-off error through exact computation of results. There are two pitfalls to avoid:

  • Symbolic computation is expensive. Running solve takes noticeably longer than running dist to ellipse.m.
  • Symbolic computation merely postpones the inevitable conversion of the answer to floating point. And the form of the expression that it produces for evaluation is beyond your control, so it might contain
  • “A suitable set of tests” needs to exercise each line of the code and also test easy as well as “difficult” values of the parameters. For example, - What happens when α is close to the largest machine number, or the smallest positive number? - What happens if α or β is negative or zero? - What happens when z is very close to the ellipse? - What happens when a component of z is nonzero but close to zero? - Does the algorithm handle error conditions well? For example, the algorithm should return gracefully if given a point on or outside the ellipse. (This was not required by the assignment, but in the real world it would be important!) - The program should be tested on systematic sets of input values as well as randomly generated ones. - Don’t just use positive values of z; test in every quadrant.
  • The results need to be compared with “true” results. These can be obtained - from an existing trusted algorithm, - from hand computation, - or from special cases for which the solution can be computed: a circle, points were zi = 0, points on the ellipse, etc.
  • The output should be designed so that validity is clear. Your boss (or teacher) should not need to read a cryptic printout of results. See the posted code for an example of output that makes part of the validation clear.

Comments on Algorithm Organization: For your remaining work in this course:

  • Get in the habit of implementing algorithms in a general way. An al- gorithm should work for various parameters, so organize it as a Matlab function, in its own .m file. Pass the parameters to it and have it return

appropriate values. In other words, don’t make it part of your main program, and don’t require it to be changed every time the parameters change. Also, do not use input or output statements (read, print, disp, etc.) in such functions, except for error messages.

  • Include comments in your programs at least as detailed as those in my program dist to ellipse.m. In particular, every .m file should have - your name, - date, - a few words about the purpose of the file and its algorithm, - a description of inputs and outputs.

In other words, give enough information that you or someone else can take the code two years from now, use it correctly, and modify it to accomplish something similar. You will thank yourself later if you get into this habit now.

Know why these ideas are dubious:

  • If I expect a real root from solve or roots, then just set the imaginary parts to zero.
  • If I expect a real root from solve or roots, then ignore any root that has a nonzero imaginary part.
  • If I expect a real root from solve or roots, then ignore any root that has an imaginary part with magnitude greater than 0.00001.
  • If zi = 0, set it to

mach.

[1] David Eberly, Distance from a Point to an Ellipse in 2D, Magic Software, Inc. www.magic-software.com/Documentation/DistanceEllipse2Ellipse2.pdf