Shortest distance from a point to a plane
This example demonstrates how to formulate and solve a least squares optimization problem using a problem-oriented approach.
We will use the functions of the [JuMP.jl] library(https://github.com/jump-dev/JuMP .jl) for the formulation of the optimization problem and the library of nonlinear optimization Ipopt.jl.
Installing Libraries
If the latest version of the package is not installed in your environment JuMP, uncomment and run the cell below:
Pkg.add(["Ipopt", "JuMP"])
#Pkg.add("JuMP");
To launch a new version of the library after the installation is complete, click on the "Personal Account" button:
Then click on the "Stop" button:
Restart the session by clicking the "Start Engee" button:
Task description
The goal of the task is to find the shortest distance from the origin (point ) to the plane .
Thus, the task is to minimize the function:
Subject to restrictions:
Function is an objective function, and it is an equality constraint.
More complex tasks may contain other equality constraints, inequality constraints, and upper or lower bound constraints.
Connecting libraries
Connect the library JuMP:
using JuMP;
Connect the library of the nonlinear solver Ipopt:
using Ipopt;
Creating an optimization task
Create an optimization task using the function Model() and specify the name of the solver in parentheses.:
plane_prob = Model(Ipopt.Optimizer)
Create a variable x containing three values, – ,  and :
@variable(plane_prob, x[1:3]);
Create an objective function to solve the minimization problem:
@objective(plane_prob, Min, sum(x[i]^2 for i in 1:3))
Set the condition for solving the optimization problem . You can use the function dot() for the scalar product of two vectors, or write the equation in the traditional way:
v = [1, 2, 4]
@constraint(plane_prob, dot(x, v) == 7)
The task statement is completed. You can view and check the issue.:
println(plane_prob)
Problem solving
Solve the optimization problem:
optimize!(plane_prob)
Save the values x in the variable:
sol = value.(x);
Print the optimization results. You can use the function round to round up overly precise values:
println("Оптимизированные значения x = ", round.(sol, digits=4))
Thus, the shortest distance from the point is up to the plane located at the point . Now we can calculate the distance between two points.:
origin = [0, 0, 0]
distance = sqrt(sum((sol .- origin).^2))
println("Расстояние ", round(distance, digits=4))
You have found the shortest distance between the origin and the specified plane.
Conclusion
In this example, we solved the problem of finding the shortest distance from the origin (point ) to the plane using the least squares method (OLS), using a problem-oriented approach.