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 个评论

请先登录,再进行评论。

更多回答(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