Search code examples
matlabnumerical-methods

error in two dimensional secand method


i want to understand what is a error in the following code?code is given below

function x0=secand2d(f,g,x,dx,tol)
% find solution of f(x,y)=0 and g(x,y)=0
for i=1:20
    x0=x;
    f0=[feval(f,x0),feval(g,x0)];
   x0=x+[dx(1),0];
   fx=[feval(f,x0),feval(g,x0)];
   fx=(fx-f0)/dx(1);
   x0=x+[0 dx(2)];
   fy=[feval(f,x0),feval(g,x0)];
   fy=(fy-f0)/dx(2);
   A=[fx;fy]+eps;
   x=x+dx;
   dx=(-A\f0'-dx')';
   if(norm(dx)<tol) ;return; end;
end;

 disp(norm(dx));
 pause;

end

which represents of two dimensional secant method,where function f and g is defined by following form

f=@(x,y) (x-sin(x+y)); and   g=@(x,y) y-cos(x-y);

i have also checked and these to function works fine on it's parameters

f(3,3)

ans =

    3.2794

and

g(1,1)

ans =

     0

also

x=[0.9 0.9];
dx=[0.1 0.1];
tol=1e-5;

but following code generated such kind of errors

secand2d(f,g,x,dx,tol)
Error using @(x,y)(x-sin(x+y))
Not enough input arguments.

Error in secand2d (line 5)
    f0=[feval(f,x0),feval(g,x0)];

please help me to clarify reasons of this errors


Solution

  • You are getting that error because you are passing only x0 to f here:

    f0=[feval(f,x0),feval(g,x0)];
    

    f and g are expecting two inputs. You can fix the error like this:

    f0=[feval(f,x0,y0),feval(g,x0,y0)];
    

    If there is not a particular reason you are using feval you can do it like this:

    f0 = [f(x0, y0) g(x0, y0)];