MATLAB Answers

To create a code that differentiate any function

7 views (last 30 days)
Ali Baran Özpolat
Ali Baran Özpolat on 27 Mar 2020
Edited: James Tursa on 27 Mar 2020
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)

  0 Comments

Sign in to comment.

Answers (2)

John D'Errico
John D'Errico on 27 Mar 2020
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.

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 27 Mar 2020
Edited: James Tursa on 27 Mar 2020
fun = str2sym(input('fun= ', 's'));
var = str2sym(input('which variable= ', 's'));
y = diff(fun, var)

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags