## Optimization App with the lsqlin Solver

### The Problem

This example shows how to use the Optimization app to solve a constrained least-squares problem.

### Note

The Optimization app warns that it will be removed in a future release.

The problem in this example is to find the point on the plane x1 + 2x2 + 4x3 = 7 that is closest to the origin. The easiest way to solve this problem is to minimize the square of the distance from a point x = (x1,x2,x3) on the plane to the origin, which returns the same optimal point as minimizing the actual distance. Since the square of the distance from an arbitrary point (x1,x2,x3) to the origin is ${x}_{1}^{2}+{x}_{2}^{2}+{x}_{3}^{2}$, you can describe the problem as follows:

`$\underset{x}{\mathrm{min}}f\left(x\right)={x}_{1}^{2}+{x}_{2}^{2}+{x}_{3}^{2},$`

subject to the constraint

x1 + 2x2 + 4x3 = 7.

The function f(x) is called the objective function and x1 + 2x2 + 4x3 = 7 is an equality constraint. More complicated problems might contain other equality constraints, inequality constraints, and upper or lower bound constraints.

### Setting Up the Problem

This section shows how to set up the problem with the `lsqlin` solver in the Optimization app.

1. Enter `optimtool` in the Command Window to open the Optimization app.

2. Select `lsqlin` from the selection of solvers. Use the `Interior point` algorithm. 3. Enter the following to create variables for the objective function:

• In the C field, enter `eye(3)`.

• In the d field, enter `zeros(3,1)`.

The C and d fields should appear as shown in the following figure. 4. Enter the following to create variables for the equality constraints:

• In the Aeq field, enter ```[1 2 4]```.

• In the beq field, enter `7`.

The Aeq and beq fields should appear as shown in the following figure. 5. Click the button as shown in the following figure. 6. When the algorithm terminates, under Run solver and view results the following information is displayed: • The Current iteration value when the algorithm terminated, which for this example is `1`.

• The final value of the objective function when the algorithm terminated:

`Objective function value: 2.333333333333334`
• The exit message:

```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.```
• The final point, which for this example is

``` 0.333 0.667 1.333```