フィルターのクリア

Error msg in fminbnd

2 ビュー (過去 30 日間)
nirwana
nirwana 2023 年 3 月 16 日
コメント済み: nirwana 2023 年 3 月 16 日
Hi all
I an trying to find minimum value of function using fminbnd sing this script
x=linspace (0,6);
y=6*cos(x)+1.5*sin(x);
plot (y);
[xmin,fval]=fminbnd(y,2,4)
Error using fcnchk
FUN must be a function, a valid character vector expression, or an inline function object.

Error in fminbnd (line 198)
funfcn = fcnchk(funfcn,length(varargin));
But it give me an error mgs as shown, i don't understand what part in my code that have mistake.

採用された回答

AL
AL 2023 年 3 月 16 日
fun = @(x) 6*cos(x) + 1.5*sin(x); % Define the function
x_min = fminbnd(fun, 0, 6); % Find the minimum within the interval [0,6]
y_min = fun(x_min); % Evaluate the function at the minimum x value
plot(linspace(0,6), fun(linspace(0,6))); % Plot the function
hold on
plot(x_min, y_min, 'ro'); % Mark the minimum point on the plot
hold off
disp(['The minimum value of y is ', num2str(y_min), ' at x = ', num2str(x_min)]);
Try this Once.
  3 件のコメント
John D'Errico
John D'Errico 2023 年 3 月 16 日
fminbnd solves for the minimum of a FUNCTION. When you define x as a vector, then create y, as a VECTOR, y is just a list of numbers. It is not a function anymore, so fminbnd must fail.
What @AL did was create fun as a function. And now it works.
nirwana
nirwana 2023 年 3 月 16 日
Thanks a lot @John D'Errico. Now I understand

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

その他の回答 (0 件)

カテゴリ

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