Fzero function with inputted ecuation problem

13 ビュー (過去 30 日間)
Orlando Barrera
Orlando Barrera 2020 年 5 月 4 日
コメント済み: Orlando Barrera 2020 年 5 月 4 日
I am having troubles with fzero function: Maybe I am dumb and this can't be done, but I want to use fzero with any function inputted by an user and I don't know how, here's my code and the problem it shows.
y=input('Function? : ');
f= @(x) y ;
x0=[0 1];
z=fzero(f,x0);
disp(z);
%Command Window:
Function? : exp(-x) - x
Error using fzero (line 257)
Function values at interval endpoints must be finite and real.
Error in prueba (line 4)
z=fzero(f,x0);
But if I use the function instead of 'y' it works perfectly:
f= @(x) exp(-x)-x ;
x0=[0 1];
z=fzero(f,x0);
disp(z);
%Command Window:
0.567143290409784

採用された回答

Walter Roberson
Walter Roberson 2020 年 5 月 4 日
y = input('Function? ', 's')
f = str2func(['@(x)' y])
  1 件のコメント
Orlando Barrera
Orlando Barrera 2020 年 5 月 4 日
Thank you man, it worked.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurrogate Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by