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

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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 6일
One solution is to use symbolic math toolbox
df = matlabFunction(diff(sym(f)));

댓글 수: 4

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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

태그

질문:

2018년 5월 6일

댓글:

2018년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by