Undefined function 'diff' for input arguments of type 'function_handle'.

조회 수: 5 (최근 30일)
Dear All,
would you like please to help me about this error "Undefined function 'diff' for input arguments of type 'function_handle'."? Thanks in advance
This is the following code:
xguess = 2400; % initial guess
emax = 1e-7; % Maximum error
imax = 1000; % maximum number of iterations
maxstep = 3002; % maximum x-step size
minslope = .01; % minimum value for the slope (prevents divide by zero)
F1=@(x) 2400 + (1- (sqrt(pi)*erf(x))./exp(-x.^2)/2)./diff((sqrt(pi)*erf(x)./exp(-x.^2)/2));
lim = @(x,xlim) max(-xlim,min(xlim,x)); % limiter function
i=1;
x=xguess;
while (i<imax) && (abs(600))>=emax % needs abs(deltax) (can be + or -)
yguess = F1(x); % Get the function value for the x guess
slope = diff(F1,x); % Get the function slope for x-guess
if(abs(slope)<minslope) % Don't allow slope to go to zero (trap divide by zero condition)
slope = minslope*sign(slope);
end
yerr = yguess; % Since the desired value is zero, the function value represents the error value
deltax = -yerr/slope; % calculate the x-step
xstep = lim(deltax,maxstep); % limit the x-step (deltax) to +/- maxstep
x = x + xstep; % apply the (limited) x-step to the x-guess value and repeat the iteration
i=i+1; % update the increment counter
end

채택된 답변

Walter Roberson
Walter Roberson 2019년 1월 3일
There are two functions named diff. One is strictly numeric differences, and is used when the first argument is numeric such as a vector of numbers .
The second is calculus differentiation and is used if the first argument is either a symbolic expression or symbolic function .
You are attempting to call diff with a function handle .Function handles are not numeric and are not symbolic either , so you are trying to use diff in aa way that is not defined.
You appear to be attempting to take the derivative of the function and evaluate the derivative at a particular x value . There is no provided operation for that.
If you have the symbolic toolbox then you can define symbolic variables and pass them to the function handle hoping to get out aa symbolic representation of the function . You could then diff() that and then use matlabFunction . The result would be aa function handle to evaluate the derivative at the given point .
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 1월 3일
%before loop
syms X
dF1 = matlabFunction(diff(F1(X),X));
.....
%in loop
slope = dF1(x);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by