Finding miminum and maximum of function f(x)
44 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
John D'Errico
el 26 de Feb. de 2019
Editada: John D'Errico
el 26 de Feb. de 2019
help min
help max
help hold
Note that the global minimum appears to lie outside of the range you have chosen to plot
Respuestas (1)
David Goodmanson
el 3 de Mzo. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!