Finding miminum and maximum of function f(x)
44 ビュー (過去 30 日間)
古いコメントを表示
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
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
回答 (1 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!