假设我在matlab中写了以下
c='x^2-6';
f=inline(c);
那么 f 将是一个内联函数。我可以通过输入以不同的值评估 f
f(2)
f(5)
等等
但是,当我尝试时diff(f)
它不会返回2*x
。我怎么能从中2*x
得到f
?
假设我在matlab中写了以下
c='x^2-6';
f=inline(c);
那么 f 将是一个内联函数。我可以通过输入以不同的值评估 f
f(2)
f(5)
等等
但是,当我尝试时diff(f)
它不会返回2*x
。我怎么能从中2*x
得到f
?
您不能将inline
对象用于符号数学计算。改为使用sym
对象:
c= sym('x^2-6'); % creates the 'sym' object
subs(c,2) % calculates c(2)
diff(c);
另请注意,inline
将在未来的版本中删除
您需要MATLAB 符号工具箱。您所描述的称为符号分化。(还有符号整合等)。MATLAB 的“普通”(非符号)版本旨在进行数值计算,而不是微积分或代数运算。
这是一种将函数和参数作为用户输入并加以区分的符号方法。
clear;
clc;
v=input('Parameter :');%input for example 'x' and remember the quotes
syms(v);%symbolic variable : x in this case
y=input('function :');%example exp (x) ,not exp(y) or ay other variable
f=matlabFunction(y);%converts y to a command type function f
df = matlabFunction(diff(y)); %calculates the differentiation.
现在,如果您执行 f(1) 之类的操作,它将显示 2.71828,而 df(1) 将显示 2.71828