Evaluating a symbolic function

半世苍凉 提交于 2020-03-15 09:37:29

问题


I want to find cos(5). Why is this expression invalid:

syms x
f=sin(x)
disp(diff(f)(5))

The error is

Line: 3 Column: 12
Indexing with parentheses '()' must appear as the last operation of a valid indexing expression.

回答1:


Your error has nothing to do with symbolic variables.

It is caused by the statement diff(f)(5) - which is not something MATLAB syntax allows (as of R2019b). MATLAB interprets this as the user trying to access the 5th element of some intermediate result. If you want to know the actual value of the derivative of f at x=5, you would have to substitute the desired value of x (using subs) and convert this to some numeric format (such as double):

syms x
f = sin(x)
disp(double(subs(diff(f),x,5))) % substitute x and convert to double



回答2:


Y = cos(x) will simply provide the cosine value of x. Or all the x.

I think in your code: disp(diff(f(5))) should work.



来源:https://stackoverflow.com/questions/60182991/evaluating-a-symbolic-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!