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
. In other words, this problem is to minimize
subject to the constraint
. The functionf(x) is called theobjective functionand
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 variablex
as a continuous variable with three components.
x = optimvar('x',3);
Create the objective function and put it in theObjective
property 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*v
is perpendicular to the plane
. So the solution pointxopt
ist*v
for the value oft
that 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 vectorxopt
is equivalent to the pointsol.x
thatsolve
finds.