



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CMSC/AMSC 660-0101 Fall 2004 Homework 1: Comments on Solution
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].
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:
Comments on Algorithm Organization: For your remaining work in this course:
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.
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:
mach.
[1] David Eberly, Distance from a Point to an Ellipse in 2D, Magic Software, Inc. www.magic-software.com/Documentation/DistanceEllipse2Ellipse2.pdf