Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有一个我前几天使用的方法,我忘记了细节,我们使用了这样的语法:
f=//command//(x,'sin(x)');
像这样的东西。我不确定语法是否完全正确,或者正确的命令是什么。但在此之后,我们可以像这样简单地询问 f(x) 值:
x= 0; y= f(x);
然后结果是y=0;
你所要求的通常是不推荐的。请检查一个简单的匿名函数是否也符合您的要求:
f=@(x)(sin(x))
如果您确实需要从字符串进行评估:
f=str2func('@(x)sin(x)')
除非绝对需要,否则我建议不要使用第二种选择,它可能导致难以调试错误。
好吧,我自己找到了答案,它是“内联”命令;:)
f=inline('sin(x+y+z)','x','y','z');
您也可以根据需要添加尽可能多的变量。