Main Content

Shortest Distance to a Plane

The Problem

This example shows how to formulate a linear least squares problem using the problem-based approach.

The problem is to find the shortest distance from the origin (the point[0,0,0]) to the plane x 1 + 2 x 2 + 4 x 3 = 7 . In other words, this problem is to minimize f ( x ) = x 1 2 + x 2 2 + x 3 2 subject to the constraint x 1 + 2 x 2 + 4 x 3 = 7 . The functionf(x) is called theobjective functionand x 1 + 2 x 2 + 4 x 3 = 7 is anequality constraint. More complicated problems might contain other equality constraints, inequality constraints, and upper or lower bound constraints.

Set Up the Problem

To formulate this problem using the problem-based approach, create an optimization problem object calledpointtoplane.

pointtoplane = optimproblem;

Create a problem variablexas a continuous variable with three components.

x = optimvar('x',3);

Create the objective function and put it in theObjectiveproperty ofpointtoplane.

obj = sum(x.^2); pointtoplane.Objective = obj;

Create the linear constraint and put it in the problem.

v =(1、2、4);pointtoplane。约束= dot(x,v) == 7;

The problem formulation is complete. To check for errors, review the problem.

show(pointtoplane)
OptimizationProblem : Solve for: x minimize : sum(x.^2) subject to : x(1) + 2*x(2) + 4*x(3) == 7

The formulation is correct.

Solve the Problem

Solve the problem by callingsolve.

[sol,fval,exitflag,output] = solve(pointtoplane);
Solving problem using lsqlin. Minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
disp(sol.x)
0.3333 0.6667 1.3333

Verify the Solution

To verify the solution, solve the problem analytically. Recall that for any nonzerot, the vectort*[1,2,4] = t*vis perpendicular to the plane x 1 + 2 x 2 + 4 x 3 = 7 . So the solution pointxoptist*vfor the value oftthat satisfies the equationdot(t*v,v) = 7.

t = 7/dot(v,v)
t = 0.3333
xopt = t*v
xopt =1×30.3333 0.6667 1.3333

Indeed, the vectorxoptis equivalent to the pointsol.xthatsolvefinds.

Related Topics