shortest distance to a plane -凯发k8网页登录
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 function f(x) is called the objective function and is an equality 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 called pointtoplane
.
pointtoplane = optimproblem;
create a problem variable x
as a continuous variable with three components.
x = optimvar('x',3);
create the objective function and put it in the objective
property of pointtoplane
.
obj = sum(x.^2); pointtoplane.objective = obj;
create the linear constraint and put it in the problem.
v = [1,2,4]; pointtoplane.constraints = 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 calling solve
.
[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 nonzero t
, the vector t*[1,2,4] = t*v
is perpendicular to the plane . so the solution point xopt
is t*v
for the value of t
that satisfies the equation dot(t*v,v) = 7
.
t = 7/dot(v,v)
t = 0.3333
xopt = t*v
xopt = 1×3
0.3333 0.6667 1.3333
indeed, the vector xopt
is equivalent to the point sol.x
that solve
finds.