Finding miminum and maximum of function f(x)

조회 수: 50 (최근 30일)
Faris Hajdarpasic
Faris Hajdarpasic 2019년 2월 26일
답변: David Goodmanson 2019년 3월 3일
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
John D'Errico
John D'Errico 2019년 2월 26일
편집: John D'Errico 2019년 2월 26일
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 2019년 2월 26일
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??

댓글을 달려면 로그인하십시오.

답변 (1개)

David Goodmanson
David Goodmanson 2019년 3월 3일
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.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by