%% Goal Attainment with Linear Constraint
% The objective function is
% F(x) = \left [ {
% {2 + \|x - p_1\|^2}
% \atop
% 5 +\|x - p_2\|^2/4 }\right ].
% Here, p_1 = [2,3] and p_2 = [4,1]. The goal is [3,6], the weight is [1,1], and the linear constraint is x_1 + x_2 \le 4.
% Create the objective function, goal, and weight.

p_1 = [2,3];
p_2 = [4,1];
fun = @(x)[2 + norm(x-p_1)^2;5 + norm(x-p_2)^2/4];
goal = [3,6];
weight = [1,1];

% Create the linear constraint matrices A and b representing A*x <= b.

A = [1,1];
b = 4;

% Set an initial point [1,1] and solve the goal attainment problem.

x0 = [1,1];
x = fgoalattain(fun,x0,goal,weight,A,b)

% Find the value of F(x) at the solution.

fun(x)

% fgoalattain does not meet the goals. Because the weights are equal, the solver underachieves each goal by the same amount.
% Copyright 2018 The MathWorks, Inc.