From: Tom Lee on
Dear all,

I am reading a programm in a book showed as following:

function [x,fx,xx]=newton(f,df,x0,TolX,MaxIter)
h=1e-4;h2=2*h;TolFun=eps;
if nargin==4 & isnumeric(df)
MaxItr=TolX;
TolX=x0;
x0=df;
end
xx(1)=0;
fx=feval(f,x0);
for k=1:MaxIter
if ~isnumeric(df)
dfdx=feval(df,xx(k)); %derivative function
else dfdx=(feval(f,xx(k)+h)-feval(f,xx(k)-h))/h2;
end
dx=-fx/dfdx;
xx(k+1)=xx(k)+dx;
fx=feval(f,xx(k+1));
if abs(fx)<TolFun|abx(dx)<TolX
break;
end
end
x=xx(k+1)
end

I would like to know what it means:
if ~isnumeric(df)
dfdx=feval(df,xx(k)); %derivative function
else dfdx=(feval(f,xx(k)+h)-feval(f,xx(k)-h))/h2;

Because I checked the meaning of isnumeric(), which will return 1 or
0. but if df is an array , how the derivative function is used in an
array and what the df function is ?

Is there anyone can help to explain that?

Thank you all in advance
Tom
From: Steven Lord on

"Tom Lee" <lee.tom30(a)gmail.com> wrote in message
news:6bbfdd69-2969-474c-85cb-dde69d65b85d(a)h9g2000yqm.googlegroups.com...
> Dear all,
>
> I am reading a programm in a book showed as following:

*snip*

> I would like to know what it means:
> if ~isnumeric(df)
> dfdx=feval(df,xx(k)); %derivative function
> else dfdx=(feval(f,xx(k)+h)-feval(f,xx(k)-h))/h2;
>
> Because I checked the meaning of isnumeric(), which will return 1 or
> 0. but if df is an array , how the derivative function is used in an
> array and what the df function is ?
>
> Is there anyone can help to explain that?

The book probably explains that it expects df to be either a function handle
or an empty (placeholder) array. [The help text for this function should do
the same ... except it doesn't appear to have any. Bad author!] If df is a
function handle, it's not numeric and so should be evaluated to obtain the
derivative of the function being processed. If it's empty, then the
function needs to approximate the derivative.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ