필터 지우기
필터 지우기

Anonymous Function Differentiation Problem?

조회 수: 48 (최근 30일)
Steve
Steve 2011년 9월 29일
답변: Konstantin Ninidze 2021년 4월 24일
Hello Experts,
I have the following function:
>>f = @(x) x^2-1.
I want to find a function that will do differentiation: g = f' = 2*x so that g will be anonymous too and it will be possible to evaluate for example: g(5) = 10.
Please help me,
Steve

답변 (5개)

Richard Edmonds
Richard Edmonds 2015년 11월 5일
Yes you can do it. Here is the code to acheive exactly what you want. (too late for you perhaps but this was the first link in my search so I came beack to drop in the best answer I found).
fdash=@(fun,x,epsilon)imag(fun(x(:)+1i*epsilon))/epsilon;
g=@(x)fdash(f,x,10^-20);

Matt Tearle
Matt Tearle 2011년 9월 29일
If you know the independent variable is called 'x':
fprime = str2func(['@(x) ',char(diff(sym(regexprep(char(f),'^@\(x\)',''))))])
fprime(pi)
If not... well, you can do it, but it will be a bit messier.

Walter Roberson
Walter Roberson 2011년 9월 29일
You cannot do that, not without converting the anonymous function in to a character string and doing symbolic differentiation on that, and constructing the appropriate anonymous function from the result.
If you attempt to do this numerically, then because you can only sample at a finite number of numeric points, there will be an infinite number of functions that match the numeric results, leaving you unable to select the correct function to differentiate. You will, for example, be unable to find removable discontinuities numerically (except by luck). And good luck doing a numeric reconstruction of tan(Pi*exp(x)) .

Doug Hull
Doug Hull 2011년 9월 29일
If you know you will be dealing only with polynomials, can you make something like this work?

Konstantin Ninidze
Konstantin Ninidze 2021년 4월 24일
you can try my function: handle_to_sym:
function Fs=handle_to_sym(F)
% input F function_handle
% output Fs symbolic expression
S=char(F);
S=S(5:end);
Fs = evalin(symengine,S);

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by