How to use fminunc for a 2D function composed of two functions? (2024)

31 Ansichten (letzte 30 Tage)

Ältere Kommentare anzeigen

Sergio vor etwa 12 Stunden

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions

Bearbeitet: Sergio vor etwa 9 Stunden

Akzeptierte Antwort: Aquatris

In MATLAB Online öffnen

I have:

How to use fminunc for a 2D function composed of two functions? (2)

and I want to minimize it on the unit square How to use fminunc for a 2D function composed of two functions? (3) without using differentials.

I use the following code,

close all; clear; clc;

options = optimoptions(@fminunc,'Display','iter','Algorithm','quasi-newton');

xy_guess = [0,0];

[xy_opt,fval] = fminunc(@quadratic,xy_guess,options)

function f = quadratic(in)

x = in(1);

y = in(2);

f = -5.*x - 5.*y + 10.*x.^2 + 2.*x.*y

f = 1/200.*(-1000.*x - 1000.*y + 400.*x.*y + 1200.*y.^2 + 5.*cos(30.*x) + 4.*cos(80.*x.^2) + 5.*cos(30.*y) + 4.*cos(80.*y^2))

But declaring f twice does not work. How should I declare this double-function as an input for fminunc ?

Thanks!

2 Kommentare

Keine anzeigenKeine ausblenden

Ashutosh Thakur vor etwa 12 Stunden

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions#comment_3197791

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions#comment_3197791

Hi Sergio,

With the last line of code the value of f is overwritten, and only this value is passed to the fminunc. Also I have observed that you have a constraint on the unit square. You can take advantage of the fmincon function, https://www.mathworks.com/help/optim/ug/fmincon.html, as fminunc does not have any constraint.

Can you try to use fmincon function?

Sergio vor etwa 10 Stunden

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions#comment_3197881

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions#comment_3197881

Bearbeitet: Sergio vor etwa 10 Stunden

In MATLAB Online öffnen

Thanks for the tip Ashutosh. While I will try fmincon, how do I represent f from that double map to a single input function?

With the new input, accordingly to your tip I get convergence, but I have no idea what f is considered as...

So I tried this (with the comma after + 2.*x.*y):

f = -5.*x - 5.*y + 10.*x.^2 + 2.*x.*y, 1/200.*(-1000.*x - 1000.*y + 400.*x.*y + 1200.*y.^2 + 5.*cos(30.*x) + 4.*cos(80.*x.^2) + 5.*cos(30.*y) + 4.*cos(80.*y^2))

options = optimoptions(@fmincon,'Display','iter','Algorithm','interior-point');

xy_guess = [0,0];

A = [0,1];

[xy_opt,fval] = fmincon(@quadratic,xy_guess,A,1)

function f = quadratic(in)

% Unpack inputs

x = in(1);

y = in(2);

% The Quadratic function in 2D

f = -5.*x - 5.*y + 10.*x.^2 + 2.*x.*y

f = 1/200.*(-1000.*x - 1000.*y + 400.*x.*y + 1200.*y.^2 + 5.*cos(30.*x) + 4.*cos(80.*x.^2) + 5.*cos(30.*y) + 4.*cos(80.*y^2))

end

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Akzeptierte Antwort

Aquatris vor etwa 10 Stunden

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions#answer_1478386

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions#answer_1478386

Bearbeitet: Aquatris vor etwa 10 Stunden

In MATLAB Online öffnen

You dont seem to have a double function.

So here is a code to solve your problem. You have local minimas so fmincon and brute force approach gives you different results, since brute force approach is a global optimization.

f = @(x) [x(1) x(2)]*[10 2;2 6]*[x(1);x(2)]-[5 5]*[x(1);x(2)]+(cos(30*x(1))+cos(30*x(2)))/40+(cos(80*x(1)^2)+cos(80*x(2)^2))/50;

options = optimoptions(@fmincon);

%options = optimoptions(@fmincon,'Display','iter','OptimalityTolerance',1e-12);

lb = [0 0];% lower bounds

ub = [1 1];% upper bounds

x0 = [.5 .5]; % initial guess

[xSol,fval,exitflag,output] = fmincon(f,x0,[],[],[],[],lb,ub,[],options); % solve optimization

Feasible point with lower objective function value found, but optimality criteria not satisfied. See output.bestfeasible..Local 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.

% brute force searching the entire space for min function value

x1 = 0:0.001:1;

x2 = 0:0.001:1;

[X1,X2] = meshgrid(x1,x2);

fValBrute = arrayfun(@(x1,x2)f([x1 x2]),X1,X2);

idxMin = find(fValBrute == min(fValBrute,[],'all')); % find minimum function value

% plot

contourf(X1,X2,fValBrute,150)

hold on

plot(xSol(1),xSol(2),'k*') % black star is the result of fmincon

plot(X1(idxMin),X2(idxMin),'rx') % red x is the brute force result

title({sprintf('Min function value found via fmincon: %.4f at [%.4f %.4f]',fval,xSol(1),xSol(2));

sprintf('Min function value found via brute force: %.4f at [%.4f %.4f]',fValBrute(idxMin),X1(idxMin),X2(idxMin))})

hold off

How to use fminunc for a 2D function composed of two functions? (7)

1 Kommentar

-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Sergio vor etwa 10 Stunden

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions#comment_3197901

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/2132836-how-to-use-fminunc-for-a-2d-function-composed-of-two-functions#comment_3197901

Bearbeitet: Sergio vor etwa 9 Stunden

Superb!! Thanks for this. I see the correct use with the semi-colonuse for the function representation, which is in fact a vector field rather than a function

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Melden Sie sich an, um diese Frage zu beantworten.

Siehe auch

Tags

  • fminunc
  • declare
  • function

Produkte

  • MATLAB

Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Es ist ein Fehler aufgetreten

Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.


Translated by How to use fminunc for a 2D function composed of two functions? (9)

How to use fminunc for a 2D function composed of two functions? (10)

Website auswählen

Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .

Sie können auch eine Website aus der folgenden Liste auswählen:

Amerika

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

Europa

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

Asien-Pazifik

Kontakt zu Ihrer lokalen Niederlassung

How to use fminunc for a 2D function composed of two functions? (2024)
Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 5852

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.