How to Differentiate a Function Handler Based on User Input

I would like to know how to differentiate or derive a function handler based on user input. I tried using diff but that doesn't work.
f = @(x) input
end
that's how i expect to get an input polynomial function from the user who run the program. I'm really new to matlab and now struggling to find the solution. Thanks for your answer.

3 commentaires

Can you tell what type of input can the user give?
i'm expecting the user to input a polynomial function, something like (5*x^3) or etc.
Refer to my answer below.

Connectez-vous pour commenter.

 Réponse acceptée

One solution is to use symbolic math toolbox
df = matlabFunction(diff(sym(f)));

4 commentaires

Thanks, that works, but what if I'd also like to find the second derivative, or the derivative of df that you provided?
And, can i also find the integration within certain limits of a function handler based on user input?
For the second derivative, there are two ways.
1) Since you already have function handle for df, you can get its derivative similarly.
df2 = matlabFunction(diff(sym(df)));
2) Or you can directly find the second derivative in the first step if you don't need the first derivative
df2 = matlabFunction(diff(sym(f), 2));
As far as integration is concerned, you can use integral() to directly integrate function handle.
thanks for your answer, it really helps.
You are welcome.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by