MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   查找用户输入的函数的f(任何值),例如f(1)或f(2) (https://www.labfans.com/bbs/showthread.php?t=26490)

poster 2019-12-14 20:13

查找用户输入的函数的f(任何值),例如f(1)或f(2)
 
这里的主要问题是在某个时刻评估用户函数,因为我们不知道用户是否会在x域或另一个域中输入函数

我试过了,但是没有用:

function y = f(~) y = input('Enter you function: '); end 这就是我想要的:

>>f Enter you function: a^2+3*a-3 >>f(1) ans = 1

[B]回答:[/B]

该解决方案几乎可以满足您提出问题的确切要求。我虽然在EVAL中使用它,但仍感到有些好奇。您还希望对此进行一些错误检查。

classdef f < handle properties (Access = private) functionString = ''; end methods function obj = f if nargin == 0 obj.functionString = input('Enter your function: ', 's'); end end function value = subsref(obj, a) a = a.subs{:}; value = eval(obj.functionString); end function display(obj) end end end 然后可以使用此类:

>> a = f输入您的函数:a ^ 2 + 3 * a-3>>一(1)回答= 1个>>一个(2)回答= 7

[url=https://stackoverflow.com/questions/5200266]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 03:06

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.