Diff, derivative does not exist

My code is:
fun=@(x) abs(x-1);
pfun=@(x) diff(fun(x),x);
feval(pfun,1)
It didn't return any answer after running the code. My general point, if we want to use Matlab to take the derivative of a function, but the derivative of this funtion does not exist everywhere (like the point 1 in fun(x)). How Matlab return the value? If there are infinite numbers of such points, how Matlab deal with this issue? Any help is very appreciated!

댓글 수: 4

Try the below (yet it is not clear what point you are trying to make):
syms x
fun=abs(x-1);
pfun(x)=diff(fun,x);
pfun(1) % evaluated at point 1
James_111
James_111 2019년 1월 27일
Thank you so much, Madhan! My point is, suppose you have a function, and you want to use Matlab to take the derivative of this function. However, the derivative may not exist everywhere. For example, like abs(x-1), the derivative at the point of 1 does not exist, as the left derivative is not equal to the right derivative. In this case, how could we still use diff? Interesting, like your code, it returns 0.
Walter Roberson
Walter Roberson 2019년 1월 27일
편집: Walter Roberson 2019년 1월 27일
You are not doing a symbolic derivative. You are evaluating pfun(1) which is pfun with numeric input. You pass that numeric input to abs(x-1), getting a numeric output. Your diff() of a numeric input is finite differences, not derivative.
James_111
James_111 2019년 1월 27일
Thank you so much for pointing out the "finite difference", Walter!

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Just for fun에 대해 자세히 알아보기

질문:

2019년 1월 27일

편집:

2019년 1월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by