0
$\begingroup$

I'm new to Matlab and am working on implementing Newton's method on a nonlinear system. I'm using a very simple example but when I call the function I create, I get the error: "Not enough input arguments. Error in function1 (line 2) f=x.^3-x.^2-1;"

Here are my function definitions:

Newton's method part:

function [new, iter] = Netn(function1,dfunction1,x) tol=0.0001; old=x; u=function1(x); v=dfunction1(x); new=old-u/v; iter=1; while (abs(new-old)>=tol) old=new; u=function1(old); v=dfunction1(old); new=old-u/v; iter=iter+1; end end

Simple function definition

function f=function1(x) f=x.^3-x.^2-1; end

Simple function derivative definition (for Newton's)

function f=dfunction1(x) f=3*x.^2-2*x; end

Can someone please help me with where I'm going wrong? Typing things like "Netn(function1, dfunction1, 3)" in the command window gets the error. I'd appreciate anything, thanks!

$\endgroup$

1 Answer 1

0
$\begingroup$

You need to use function handles.

Change your call to

Netn(@function1, @dfunction1, 3)

Change you main function to (you need to use feval when evaluating the functions passed by function handles)

function [new, iter] = Netn(func1,dfunc1,x)
tol=0.0001;
old=x; 
u=feval(func1,x);
v=feval(dfunc1,x);
new=old-u/v;
iter=1;
while (abs(new-old)>=tol)
  old=new;
  u=feval(func1,old);
  v=feval(dfunc1,old);
  new=old-u/v; 
  iter=iter+1;
end 
end

Now you can also create other objective and derivative functions, with other names. And you pass those names as function handles in your call to Netn, without changing the code to Netn.

$\endgroup$
1
  • $\begingroup$ perfect, thanks so much! $\endgroup$
    – user564998
    May 26, 2018 at 0:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .