Matlab Fmincon Optimization Example: Constrained Box Volume

Поделиться
HTML-код
  • Опубликовано: 25 дек 2024

Комментарии •

  • @EngrHafizQasimAli
    @EngrHafizQasimAli 3 года назад

    Thank you very much for making a video on this simple optimization example, for beginner learners like me.

  • @chinthakawk
    @chinthakawk Год назад +1

    Works fine in R2021b.
    % set initial guess values for box dimensions
    lengthGuess = 1;
    widthGuess = 1;
    heightGuess = 1;
    % load guess values into array
    x0 = [lengthGuess widthGuess heightGuess];
    % call solver to minimize the objective function given the constraint
    xopt = fmincon(@objective,x0,[],[],[],[],[],[],@constraint,[])
    % retrieve optimized box sizing and volume
    volumeOpt = calcVolume(xopt)
    % calculate surface area with optimized values just to double check
    surfaceAreaOpt = calcSurface(xopt)
    % define function to calculate volume of box
    function volume = calcVolume(x)
    length = x(1);
    width = x(2);
    height = x(3);
    volume = length * width * height;
    end
    % define function to calculate surface area of box
    function surfaceArea = calcSurface(x)
    length = x(1);
    width = x(2);
    height = x(3);
    surfaceArea = 2*length*width + 2*length*height + 2*height*width;
    end
    % define objective function for optimization
    function obj = objective(x)
    obj = -calcVolume(x);
    end
    % define constraint for optimization
    function [c, ceq] = constraint(x)
    c = calcSurface(x) - 10;
    ceq = [];
    end

  • @johnlloydcalisi3257
    @johnlloydcalisi3257 4 года назад +1

    what is the symbol used before the repeated terms objective and constraints?

    • @anujagrawal1036
      @anujagrawal1036 4 года назад

      @ ,it is used to denote a pointer(or address) to the function in file

    • @alielectricalelectronicsan2092
      @alielectricalelectronicsan2092 4 года назад

      @@anujagrawal1036 A.o.A sir how can we solve non linear problems in matlab .
      I only now how to solve the linear
      Problems in matlab
      Can you provide me some help in this .

  • @gamzebalkan6432
    @gamzebalkan6432 6 лет назад +1

    Thank you! Do you know how to optimize dynamic systems with optimization toolbox?

    • @abemartin6945
      @abemartin6945 6 лет назад +1

      Gamze Balkan for dynamic optimization I would recommend the APMonitor optimization package: apmonitor.com

    • @gamzebalkan6432
      @gamzebalkan6432 6 лет назад

      I will look over it, thanks a lot!

  • @alielectricalelectronicsan2092
    @alielectricalelectronicsan2092 4 года назад +2

    A.o.A sir how can we solve non linear problems in matlab .
    I only now how to solve the linear
    Problems in matlab
    Can you provide me some help in this .

    • @beoptimistic5853
      @beoptimistic5853 4 года назад

      ruclips.net/video/XPCgGT9BlrQ/видео.html 💐💐💐💐💐💐

  • @jauharalikhan6658
    @jauharalikhan6658 4 года назад +1

    i have wrote the different function in different files but this error occurs
    Not enough input arguments.
    Error in calVolume (line 3)
    len = x(1);

    • @beoptimistic5853
      @beoptimistic5853 4 года назад

      ruclips.net/video/XPCgGT9BlrQ/видео.html 💐💐💐💐💐💐

  • @kewjx44
    @kewjx44 4 года назад

    Is this a quadratic type objective function with nonlinear constraint

    • @beoptimistic5853
      @beoptimistic5853 4 года назад

      ruclips.net/video/XPCgGT9BlrQ/видео.html 💐💐💐💐💐

  • @ahmedsultan36
    @ahmedsultan36 6 лет назад

    Hi,I have MATLAB R2016a 'function definitions are not permitted in this context'I think this the version's problem. However, I have tried to find a solution but i couldn't find a proper one.Do you have an idea?
    Thanks in advance

    • @alphaopt2024
      @alphaopt2024  6 лет назад

      Ahmed, multiple function definitions in a single file are permitted from Matlab 2016b and onwards. For earlier versions of Matlab you will need to define each function in a separate file.

    • @ahmedsultan36
      @ahmedsultan36 6 лет назад

      Thanks,
      One more question. Do you have any recommendation for finding a global minimum
      Many thanks

    • @alphaopt2024
      @alphaopt2024  6 лет назад +1

      Finding a global minimum can be difficult depending on your problem. Starting from many different initial guess values can help to verify that you've found a global optimum. Gradient free methods such as particle swarm or genetic algorithms sometimes work. I've found the Matlab global optimization toolbox to be pretty good if you have access to it: www.mathworks.com/products/global-optimization.html

    • @ahmedsultan36
      @ahmedsultan36 6 лет назад

      One more question. Is there is a way to plot the iterations for this video or similar to it with 3 variables
      Thanks

  • @omartalal2554
    @omartalal2554 3 года назад

    thanks a lot for this great explination .... can I have the code because i faced an error when excute it .

  • @anujagrawal1036
    @anujagrawal1036 4 года назад

    why did you used so many square bracket inside fmincon . how many square bracket are needed??

    • @beoptimistic5853
      @beoptimistic5853 4 года назад

      ruclips.net/video/XPCgGT9BlrQ/видео.html 💐💐💐💐💐💐

  • @MrZvensk
    @MrZvensk 6 лет назад +1

    Tried the code but having a "function error" .. wont run .. saying that nested functions cannot run ..

    • @alphaopt2024
      @alphaopt2024  6 лет назад +1

      Peter, for the code to work as shown here box.m needs to be a script, not a function. This is possible in Matlab 2016b or later. If you want it as a nested function, you'll need to adapt the code: www.mathworks.com/help/matlab/matlab_prog/nested-functions.html

    • @MrZvensk
      @MrZvensk 6 лет назад

      AlphaOpt Thanks for d feedback

  • @basirbarmaki1564
    @basirbarmaki1564 5 лет назад

    That is greatful video
    Can I help me the a method constrained optimization or a level comparison?
    How can fined best worst average

  • @bipinkarki9664
    @bipinkarki9664 3 года назад

    HOW TO FIND THE OBJECTIVE FUNCTION USING TWO VARIABLES?

  • @ramiz2999
    @ramiz2999 5 лет назад +2

    Great video.
    However, I write everything like you and I use MATLAB 2016b:
    lengthGuess=1;
    widthGuess=1;
    heightGuess=1;
    x0=[lengthGuess widthGuess heightGuess];
    xopt=fmincon(@objective,x0,[],[],[],[],[],[],[],@constraint,[])
    volumeOpt=calcVolume(xopt)
    surfaceAreaOpt=calcSurface(xopt)
    function volume = calcVolume( x )
    length=x(1);
    width=x(2);
    height=x(3);
    volume=length*width*height;
    end
    function surfaceArea=calcSurface(x)
    length=x(1);
    width=x(2);
    height=x(3);
    surfaceArea=2*length*width+2*length*height+2*width*height;
    end
    function obj=objective(x)
    obj=-calcVolume(x);
    end
    function [c, ceq]=constraint (x)
    c=calcSurface-10;
    ceq=[];
    end
    I got this:
    box
    Field assignment to a non-structure array object.
    Error in createOptionFeedback (line 33)
    options.(stopTestOptions{k}) = [];
    Error in prepareOptionsForSolver (line 40)
    optionFeedback = createOptionFeedback(options);
    Error in fmincon (line 215)
    [options, optionFeedback] = prepareOptionsForSolver(options, 'fmincon');
    Error in box (line 7)
    xopt=fmincon(@objective,x0,[],[],[],[],[],[],[],@constraint,[])

    >>
    Where is the problem?

  • @ns3lover779
    @ns3lover779 6 лет назад +1

    I HAVE WRITE THE SAME CODE BUT I GOT THIS ERROR :
    xopt = fmincon( @objective,x0,[],[],[],[],[],[],@constraint,[] )
    Caused by:
    Failure in initial objective function evaluation. FMINCON cannot continue.

  • @johnlloydcalisi3257
    @johnlloydcalisi3257 4 года назад

    thanks

    • @beoptimistic5853
      @beoptimistic5853 4 года назад

      ruclips.net/video/XPCgGT9BlrQ/видео.html 💐💐💐💐💐💐

  • @kevindsilva1344
    @kevindsilva1344 4 года назад

    I didn't understand the constraints part of this, could you explain that? I think your videos are awesome BTW, given a thumbs up for all I've viewed till now.

    • @beoptimistic5853
      @beoptimistic5853 4 года назад

      ruclips.net/video/XPCgGT9BlrQ/видео.html 💐💐💐💐💐💐