Error using fmincon line 619 (2024)

1 view (last 30 days)

Show older comments

Laila Alzahrawi on 26 May 2023

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619

Commented: Walter Roberson on 26 May 2023

Open in MATLAB Online

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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (1)

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#answer_1245384

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#answer_1245384

Edited: Torsten on 26 May 2023

Open in MATLAB Online

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);

Your initial point x0 is not between bounds lb and ub; FMINCONshifted x0 to satisfy the bounds. Iter Func-count Fval Feasibility Step Length Norm of First-order step optimality 0 11 -1.526094e+04 2.125e+01 1.000e+00 0.000e+00 4.623e+02 1 22 -4.937565e+04 1.105e+01 1.000e+00 4.441e+01 2.500e+01 2 34 -4.730441e+04 7.437e+00 7.000e-01 1.455e+01 2.500e+01 3 47 -4.811526e+04 8.388e+00 4.900e-01 6.863e+00 2.500e+01 4 62 -4.772028e+04 7.104e+00 2.401e-01 3.096e+00 2.500e+01 5 79 -4.786548e+04 7.541e+00 1.176e-01 1.203e+00 2.500e+01 6 97 -4.775070e+04 7.122e+00 8.235e-02 9.057e-01 2.500e+01 7 121 -4.773831e+04 7.091e+00 9.689e-03 9.777e-02 2.500e+01 8 146 -4.774656e+04 7.109e+00 6.782e-03 6.800e-02 2.500e+01 9 173 -4.774232e+04 7.093e+00 3.323e-03 3.344e-02 2.500e+01 10 203 -4.774087e+04 7.089e+00 1.140e-03 1.143e-02 2.500e+01 11 234 -4.774188e+04 7.091e+00 7.979e-04 7.993e-03 2.500e+01 12 266 -4.774117e+04 7.089e+00 5.585e-04 5.599e-03 2.500e+01 13 299 -4.774167e+04 7.091e+00 3.910e-04 3.919e-03 2.500e+01 14 333 -4.774132e+04 7.089e+00 2.737e-04 2.743e-03 2.500e+01 15 369 -4.774115e+04 7.089e+00 1.341e-04 1.344e-03 2.500e+01 16 405 -4.774132e+04 7.089e+00 1.341e-04 1.345e-03 2.500e+01 17 442 -4.774120e+04 7.089e+00 9.387e-05 9.406e-04 2.500e+01 18 480 -4.774129e+04 7.089e+00 6.571e-05 6.591e-04 2.500e+01 19 519 -4.774123e+04 7.089e+00 4.600e-05 4.609e-04 2.500e+01 20 560 -4.774120e+04 7.089e+00 2.254e-05 2.258e-04 2.500e+01 21 602 -4.774122e+04 7.089e+00 1.578e-05 1.582e-04 2.500e+01 22 646 -4.774121e+04 7.089e+00 7.731e-06 7.746e-05 2.500e+01 23 682 -4.774121e+04 7.089e+00 2.652e-06 2.657e-05 2.500e+01 Converged to an infeasible point.fmincon stopped because the size of the current step is less thanthe value of the step size tolerance but constraints are notsatisfied to within the value of the constraint tolerance.

disp('Optimized Solution:');

Optimized Solution:

disp(x_opt);

30.0000 15.0000 10.0000 15.0000 10.0000 5.5520 30.0000 30.0000 15.0000 15.0000

obj_val = -sum(x_opt .*R);

crop_obj_vals = x_opt .* R;

disp('Optimal Objective Value:');

Optimal Objective Value:

disp(obj_val);

-4.7741e+04

disp('Individual Objective Values:');

Individual Objective Values:

disp(crop_obj_vals);

1.0e+04 * 0.3629 0.6934 0.2316 0.2114 0.2675 0.0530 1.2418 1.0835 0.4673 0.1616

disp(-fval);

4.7741e+04

7 Comments

Show 5 older commentsHide 5 older comments

Laila Alzahrawi on 26 May 2023

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2760949

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2760949

Edited: Laila Alzahrawi on 26 May 2023

I got this when I copied what you wrote.

"Your initial point x0 is not between bounds lb and ub; FMINCON

shifted x0 to satisfy the bounds."

What does this mean?

Torsten on 26 May 2023

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2760959

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2760959

Edited: Torsten on 26 May 2023

It means that your x0 does not satisfy lb <= x0 <= ub, the lower and upper bounds you defined for your solution vector. That's the reason MATLAB had to shift your guess in order to satisfy your bound constraints.

Laila Alzahrawi on 26 May 2023

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761014

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761014

Edited: Laila Alzahrawi on 26 May 2023

I just noticed that you asked this "Why do you solve a linear optimization problem with "fmincon" ?"

As I mentioned in the beginning of the question, I am trying to solve a nonlinear optimization problem.

Does this change your answer?

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?

I appreciate your help and your fast responses.

Torsten on 26 May 2023

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761389

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761389

Edited: Torsten 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

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761404

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761404

Open in MATLAB Online

(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

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761519

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761519

Open in MATLAB Online

@Walter Roberson

(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

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761534

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1974079-error-using-fmincon-line-619#comment_2761534

Open in MATLAB Online

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 ??

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Mathematics and OptimizationOptimization ToolboxOptimization ResultsSolver Outputs and Iterative Display

Find more on Solver Outputs and Iterative Display in Help Center and File Exchange

Tags

  • fmincon
  • scalar

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Error using fmincon line 619 (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Error using fmincon line 619 (2024)
Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5856

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.