如何解决增量搜索方法脚本错误
我写了我的第一个八度音阶脚本,该脚本是用于根查找的增量搜索方法的代码,但是遇到了很多难以理解的错误。 以下是脚本:
clear
syms x;
fct=input('enter your function in standard form: ');
f=str2func(fct); % This built in octave function creates functions from strings
Xmax=input('X maximum= ');
Xinit=input('X initial= ');
dx=input('dx= ');
epsi=input('epsi= ');
N=10; % the amount by which dx is decreased in case a root was found.
while (x<=Xmax)
f1=f(Xinit);
x=x+dx
f2=f(x);
if (abs(f2)>(1/epsi))
disp('The function approches infinity at ',num2str(x));
x=x+epsi;
else
if ((f2*f1)>0)
x=x+dx;
elseif ((f2*f1)==0)
disp('a root at ',num2str );
x=x+epsi;
else
if (dx < epsi)
disp('a root at ',num2str);
x=x+epsi;
else
x=x-dx;
dx=dx/N;
x=x+dx;
end
end
end
end
运行它时出现以下错误:
>> Incremental enter your function in standard form: 1+(5.25*x)-(sec(sqrt(0.68*x))) warning: passing floating-point values to sym is dangerous,see "help sym" warning: called from double_to_sym_heuristic at line 50 column 7 sym at line 379 column 13 mtimes at line 63 column 5 Incremental at line 3 column 4 warning: passing floating-point values to sym is dangerous,see "help sym" warning: called from double_to_sym_heuristic at line 50 column 7 sym at line 379 column 13 mtimes at line 63 column 5 Incremental at line 3 column 4 error: wrong type argument 'class' error: str2func: FCN_NAME must be a string error: called from Incremental at line 4 column 2
解决方法
此行中发生问题:
fct=input('enter your function in standard form: ');
此处input
接受用户输入并对其进行评估。它尝试将其转换为数字。在下一行,
f=str2func(fct)
您假设fct
是一个字符串。
要解决此问题,请告诉input
仅以字符串形式返回用户的输入(请参见docs):
fct=input('enter your function in standard form: ','s');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。