To create a code that differentiate any function

8 次查看(过去 30 天)
Hello everyone!
I want to create a code that differentiate any function. The procedure should be like this: An input function should be entered with respect to "x", "z" or whatever users want and that function should be differentiated with respect to this input.
How I can do this?
Example:
fun= input('fun= '); % let be fun= exp(10*x)*sin(x*y) or fun= ((8*(x^3))*(10*(t^2)))/((3*x*z)+6*x*y)
y= diff(fun,x)

回答(2 个)

John D'Errico
John D'Errico 2020-3-27
Well, you write a complete version of a set of symbolic tools, that can parse any function provided, and know the rules for differentiation. You will need to put all possible functions in, and know how to differentiate them, for example sin(x), etc. The chain rule will be necessary. Of course, you will need to implement computer algebra too, so adds, subtracts, multiplies, divides, powers.
Are you sure you are up to it? For example, I did much of what you want to do, but only for a very simple class of functions - symbolic polynomials. It is doable. You can look at what I did in the sympoly toolbox, since it is available for free download from the file exchange.
The answer will be to be careful, in that you need to limit what you implement. Don't try to write a complete set of tools that can handle ANYTHING. It will fail if you try to be too exhaustive. So start small, and accept that you could add capability with time.

Walter Roberson
Walter Roberson 2020-3-27
编辑:James Tursa 2020-3-27
fun = str2sym(input('fun= ', 's'));
var = str2sym(input('which variable= ', 's'));
y = diff(fun, var)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by