Error using fmincon line 619
Show older comments
Hi,
I am trying to do a non-linear optimization using the "fmincon" function. I have got an error and couldn't find the problem. When I test my objective function on the initial conditions using
objective(x0) % Scalar?
I got the following answer
ans =
0 0 0 0 0 0 0 0 0 0
Your initial point x0 is not between bounds lb and ub; FMINCON
shifted x0 to satisfy the bounds.
The initial error obtained is this:
Error using fmincon (line 619)
Supplied objective function must return a scalar value.
Error in (line 41)
[x_opt, fval] = fmincon(objective, x0, [], [], [], [], lb, ub, nonlcon, options);
The code is the following:
R = [120.97, 462.26, 231.65, 140.94, 267.54, 95.49, 413.92, 361.17, 311.53, 107.76];
V = [5315.13, 11786.19, 8840.62, 15132.03, 23078.9, 7982.05, 11175.72, 7994.76, 10068.08, 11194.89];
IR = [60.67, 224.67, 177.67, 193.67, 239.33, 196.33, 220.58, 151.33, 169, 185.67];
Ky_initial =[0.2, 0.62, 0.2, 0.9, 0.4, 0.4, 0.2, 0.2];
Ky_flowering = [0.6, 0.9, 0, 1.1, 0, 1.5, 0.55, 0.8];
Ky_fruit = [0, 0.2, 0.2, 0.35, 0.25, 0.2, 0.2, 0];
Ky_total = [1, 1.1, 1.15, 0.9, 1.35, 1.05, 0.85, 1.25, 0.9, 0.85];
n = 10;
objective = @(A) -sum(A.*R);
nonlcon = @(A) [
sum(A.*V) - 34000000;
sum((10*A.*IR)/(86400*30)) - 3.78;
sum(A) - 3400;
min(A./sum(A))*100 - [20, 5, 3, 5, 3, 3, 10, 5, 5, 5];
max(A./sum(A))*100 - [40, 15, 10, 15, 10, 10, 30, 30, 15, 15];
];
lb = [20, 5, 3, 5, 3, 3, 10, 5, 5, 5];
ub = [40, 15, 10, 15, 10, 10, 30, 30, 15, 15];
x0 = zeros(n, 1);
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
[x_opt, fval] = fmincon(objective, x0, [], [], [], [], lb, ub, nonlcon, options);
disp('Optimized Solution:');
disp(x_opt);
obj_val = -sum(x_opt .*R);
crop_obj_vals = x_opt .* R;
disp('Optimal Objective Value:');
disp(obj_val);
disp('Individual Objective Values:');
disp(crop_obj_vals);
disp(-fval);
I would appreciate if somebody can help me find the error and fix it.
Thank you.
Laila
Answers (1)
Why do you solve a linear optimization problem with "fmincon" ?
Use "linprog" instead.
R = [120.97, 462.26, 231.65, 140.94, 267.54, 95.49, 413.92, 361.17, 311.53, 107.76];
V = [5315.13, 11786.19, 8840.62, 15132.03, 23078.9, 7982.05, 11175.72, 7994.76, 10068.08, 11194.89];
IR = [60.67, 224.67, 177.67, 193.67, 239.33, 196.33, 220.58, 151.33, 169, 185.67];
Ky_initial =[0.2, 0.62, 0.2, 0.9, 0.4, 0.4, 0.2, 0.2];
Ky_flowering = [0.6, 0.9, 0, 1.1, 0, 1.5, 0.55, 0.8];
Ky_fruit = [0, 0.2, 0.2, 0.35, 0.25, 0.2, 0.2, 0];
Ky_total = [1, 1.1, 1.15, 0.9, 1.35, 1.05, 0.85, 1.25, 0.9, 0.85];
n = 10;
objective = @(A) -sum(A.*R);
nonlcon = @(A) deal([
sum(A.*V) - 34000000;
sum((10*A.*IR)/(86400*30)) - 3.78;
sum(A) - 3400;
(min(A./sum(A))*100 - [20, 5, 3, 5, 3, 3, 10, 5, 5, 5]).';
(max(A./sum(A))*100 - [40, 15, 10, 15, 10, 10, 30, 30, 15, 15]).'
],[ ]);
lb = [20, 5, 3, 5, 3, 3, 10, 5, 5, 5];
ub = [40, 15, 10, 15, 10, 10, 30, 30, 15, 15];
x0 = zeros(1,n);
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
[x_opt, fval] = fmincon(objective, x0, [], [], [], [], lb, ub, nonlcon, options);
disp('Optimized Solution:');
disp(x_opt);
obj_val = -sum(x_opt .*R);
crop_obj_vals = x_opt .* R;
disp('Optimal Objective Value:');
disp(obj_val);
disp('Individual Objective Values:');
disp(crop_obj_vals);
disp(-fval);
7 Comments
Laila Alzahrawi
on 26 May 2023
Edited: Laila Alzahrawi
on 26 May 2023
Laila Alzahrawi
on 26 May 2023
Edited: Laila Alzahrawi
on 26 May 2023
As I mentioned in the beginning of the question, I am trying to solve a nonlinear optimization problem. Does this change your answer?
The problem you posted is linear and can be solved using a linear optimizer, namely "linprog". If this is not your real problem and the real problem is non-linear, it's a different thing.
Also, I have provided numbers for the upper and lower bounds, but lets say they are not given, would I still be able to solve the problem by any way?
If your solution variables are not bounded from the application behind your optimization, you don't need to set the bounds. But usually, the problem needs to be constrained in order to get a finite value for the objective.
Walter Roberson
on 26 May 2023
(min(A./sum(A))*100 - [20, 5, 3, 5, 3, 3, 10, 5, 5, 5]).';
Your A is a vector, so sum(A) is a vector, and A./sum(A) is a vector. min() of a vector is a scalar, scalar times constant is scalar. So min(A./sum(A))*100 is a scalar.
What is the point of subtracting a vector from a scalar at that point? You are going for a <= 0 which is going to be most difficult to satisfy when the subtracted value is smallest -- so why not just subtract 3 there instead of the vector?
Torsten
on 26 May 2023
(min(A./sum(A))*100 - [20, 5, 3, 5, 3, 3, 10, 5, 5, 5]).';
(max(A./sum(A))*100 - [40, 15, 10, 15, 10, 10, 30, 30, 15, 15]).'
Yes, these are strange conditions.
I would have guessed that the first condition has to be reversed:
- (min(A./sum(A))*100 + [20, 5, 3, 5, 3, 3, 10, 5, 5, 5]).';
so that they would read in a more comprehensible form
A(i) >= 3/100*sum_j(A(j)) for all i
A(i) <= 10/100*sum_j(A(j)) for all i
Walter Roberson
on 26 May 2023
lb = [20, 5, 3, 5, 3, 3, 10, 5, 5, 5];
ub = [40, 15, 10, 15, 10, 10, 30, 30, 15, 15];
Those match the vectors in the odd condition. Are they a clumsy re-statement of the bounds ??
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!