How to find derivatives using the diff command
20 次查看(过去 30 天)
显示 更早的评论
i need to find the derivative of a function fx and fy using the diff commmand wiht my current code i input the function and then just wrote diff(f) yet still getting error is this the correct way to do it where matlab would output the deravative
Find fx and fy using the diff command
Setting these to zero, solve the resulting system of
equations using the solve command
f=@(x,y) 2*x^3*y - y^2 - 3*x*y;
diff(f)
0 个评论
回答(3 个)
Cris LaPierre
2023-3-26
I think this is the page you want: https://www.mathworks.com/help/symbolic/diff.html
0 个评论
John D'Errico
2023-3-26
编辑:John D'Errico
2023-3-26
There are two versions of diff in MATLAB. One operates on numeric vectors and arrays. It simply subtracts each number from the one that follows it. But the numerical diff can be used to approximate a derivative, if you then divide by the stride between numbers. Essentially, you can form an APPROXIMATION to a derivative by dividing a difference in y with a difference in x.
As well, there is a symbolic version of diff. It does compute a derivative, but only of symboic expressions.
However, f is a function handle. Is it either of those two options? Clearly not. It may look vaguely like something you might want to differentiate, but it is not either of those alternatives. Yes, I'll admit it is a subtle difference, but an important one.
The error message generated tells you that a function handle is not an acceptable argument for diff.
As well, even if it was something in symbolic form, would you expect MATLAB to know you want to differentiate with respect to x? Or to y? Which one?
I would suggest you need to form the expression in symbolic form. Then you would compute the pair of derivatives, WRT first x and y, and then do as directed.
0 个评论
Walter Roberson
2023-3-26
编辑:Walter Roberson
2023-3-26
It is possible to apply symbolic diff() to a function handle of an anonymous function -- but you have to specifically mention a symbolic variable in the parameters so that MATLAB knows to use symbolic diff() instead of numeric diff()
f=@(x,y) 2*x^3*y - y^2 - 3*x*y;
syms x y
G = [diff(f,x); diff(f, y)]
Note that this only works for anonymous functions:
diff(@sin, x)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!