如何让用户输入函数

How to let a user input a function

我知道 scilab 中的输入语法是这样的:

result = input ("Enter your number: ")

我的问题是,如何输入 cos(x) + sin(x) 这样的函数?

让用户输入一个字符串(input命令的参数"s"),并在eval命令中使用这个字符串进行评估。示例:

fstr = input("Enter a function of x: ", "s")
x = input("What is the value of x: ")
y = eval(fstr) 
disp("The value of function is " + string(y))

字符串计算也可以包装为 Scilab 函数:

fstr = input("Enter a function of x: ", "s")
function y = f(x)
    y=eval(fstr)
endfunction

t = linspace(0, 2)
plot(t, f(t))