How could I program a "for loop" in Matlab to calculate the function's minimum value?

1 回表示 (過去 30 日間)
chunyi liu
chunyi liu 2022 年 6 月 22 日
コメント済み: Star Strider 2022 年 6 月 22 日
How could I program a "for loop" in Matlab to calculate the function's minimum value?
The goal is to locate the function G's minimum value point that corresponds to the particular parameter b=[1 2 3 4 5]. Like:
syms x
for b=1:1:5;
G=x.^2-b.*x+1;
f=inline(G);
x=fminbnd(f,0,10)
end

回答 (2 件)

Stephen23
Stephen23 2022 年 6 月 22 日
X = [1,2,3,4,5];
Y = nan(size(X));
for k = 1:numel(X)
b = X(k);
G = @(x) x.^2 - b.*x + 1;
Y(k) = fminbnd(G,0,10);
end
plot(X,Y,'*')

Star Strider
Star Strider 2022 年 6 月 22 日
G = @(x,b) x.^2-b.*x+1;
b=1:1:5;
for k = 1:numel(b)
xv(k) = fminsearch(@(x)G(x,b(k)), rand);
end
xv
xv = 1×5
0.5000 1.0000 1.5000 2.0000 2.5000
The first derivative of ‘G’ is simply ‘2*x-b’ so an analytic solution is:
xq = b/2
xq = 1×5
0.5000 1.0000 1.5000 2.0000 2.5000
Giving the same result.
.
  2 件のコメント
chunyi liu
chunyi liu 2022 年 6 月 22 日
Thank you very much, it is exactly what I need!
Star Strider
Star Strider 2022 年 6 月 22 日
My pleasure!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by