What does the following error mean? Undefined function 'diffjac' for input arguments of type 'function_handle'. Error in Heq_Nwt (line 23) df = diffjac(x1,f,f0)

5 views (last 30 days)
My code is below. Not sure why I cannot use diffjac function. I also get an error for input arguments of type 'double' if I change my diffjac inputs to (x1,f,f0).
function [ x, ex] = Heq_Nwt( f, x1, tol, nmax )
c=.8;
N=10;
x2 = ones(N,1);
x1 = transpose(x2);
meu=1:N;
meu=(meu-.5)/N;
meu=meu';
cs=.5*c/N;
ChH= meu';
in = @(x1) sum((ChH'*x1)./(bsxfun(@plus, ChH',ChH)));
n2=ones(N,1)';
f = @(x1)x1 - (n2-(cs*(in(x1)))).^-1
f0 = f(x1)
df = diffjac(x1,f,f0)

Answers (4)

Matthew
Matthew on 23 Apr 2015
Hi Branden,
What are the results of
which diffjac -all
?
If its empty, then you need to add the folder diffjac is in to your path. If its not, then diffjac is written for a different type of object, and to use it you will need to overload it and define your desired behavior from it.

Branden Chamness
Branden Chamness on 23 Apr 2015
Results are:
'diffjac' not found.
I added diffjac.m file to directory. It is as follows:
function [l, u] =diffjac(x, f, f0)
% compute a forward difference Jacobian f'(x), return lu factors
%
% uses dirder.m to compute the columns
%
% C. T. Kelley, November 25, 1993
%
% This code comes with no guarantee or warranty of any kind.
%
%
% inputs:
% x, f = point and function
% f0 = f(x), preevaluated
%
n=length(x);
for j=1:n
zz=zeros(n,1);
zz(j)=1;
jac(:,j)=dirder(x,zz,f,f0);
end
[l, u] = lu(jac);
I received the following errors when running original code once diffjac.m added:
Undefined function 'dirder' for input arguments of type 'function_handle'.
Error in diffjac (line 19)
jac(:,j)=dirder(x,zz,f,f0);
Seems I need to add this file to directory as well. So I did. It is as follows:
function z = dirder(x,w,f,f0)
% Finite difference directional derivative
% Approximate f'(x) w
%
% C. T. Kelley, November 25, 1993
%
% This code comes with no guarantee or warranty of any kind.
%
% function z = dirder(x,w,f,f0)
%
% inputs:
% x, w = point and direction
% f = function
% f0 = f(x), in nonlinear iterations
% f(x) has usually been computed
% before the call to dirder
%
% Hardwired difference increment.
epsnew=1.d-7;
%
n=length(x);
%
% scale the step
%
if norm(w) == 0
z=zeros(n,1);
return
end
epsnew = epsnew/norm(w);
if norm(x) > 0
epsnew=epsnew*norm(x);
end
%
% del and f1 could share the same space if storage
% is more important than clarity
%
del=x+epsnew*w;
f1=feval(f,del);
z = (f1 - f0)/epsnew;
Now I am receiving errors from this file as well:
Error using +
Matrix dimensions must agree.
Error in dirder (line 38)
del=x+epsnew*w;
Am I going to have to debug diffjac.m and dirder.m in order to use diffjac in my code?

Ahmet Cecen
Ahmet Cecen on 23 Apr 2015
diffjac.m and dirder.m works perfectly fine for me. Your inputs must be different than intended.

Branden Chamness
Branden Chamness on 23 Apr 2015
Should I match my inputs to match diffjac and dirder? For example my input is 'x1' while diffjac and dirder is 'x'.
See any issue with my function? I used anonymous function. Is diffjac/dirder looking for different function notation?
f = @(x1)x1 - (n2-(cs*(in(x1)))).^-1

Tags

Community Treasure Hunt

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

Start Hunting!