Finding miminum and maximum of function f(x)

50 vues (au cours des 30 derniers jours)
Faris Hajdarpasic
Faris Hajdarpasic le 26 Fév 2019
So I have function y=f(x) and I need to find global minimum and global maximum of function and display it on graph.
My question is HOW to find global max and min, and HOW to plot them on same graph.
Here is my code so far:
function nacrtaj_funkciju() %nema parametara i tipa je void
x = -2:0.05:2;
y = 1./(((x-0.3).*(x-0.3))+0.01) + 1./(((x-0.9).*(x-0.9))+0.04) - 6;
plot(x,y)
xlabel('x osa')
ylabel('f(x)')
grid on
end
  2 commentaires
John D'Errico
John D'Errico le 26 Fév 2019
Modifié(e) : John D'Errico le 26 Fév 2019
help min
help max
help hold
Note that the global minimum appears to lie outside of the range you have chosen to plot
Faris Hajdarpasic
Faris Hajdarpasic le 26 Fév 2019
i solve this using functions max(y) and min(y)( I found index and then plot those dots on graph). Can I solve same problem but for z=f(x,y) using max and min functions??

Connectez-vous pour commenter.

Réponses (1)

David Goodmanson
David Goodmanson le 3 Mar 2019
Hi Faris,
In 1d I assume you had y = f(x) with an x vector of length n, so its index is 1:n. Then
[ymax ind] = max(y)
xmax = x(ind)
gave you the point xmax,ymax for the function y, with the limitations that [1] the precision of the maximum value is limited by the fineness of the x array, [2] the global max is only going to be found if it's within the domain of the specified x, and [3] if the max is not unique, you will find one of the maxes (within the precision limitations) but all bets are off for finding the rest.
In 2d with an x index of 1:n and a y index of 1:m, and a matrix z = f(x,y) then (assuming x across, y down)
[zcolmax rowind] = max(z); % max for each column
[zmax colind] = max(zcolmax)
xmax = x(colind)
ymax = y(rowind(colind))
with basically the same limitations as in the 1d case. Similarly for min.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by