function [f,A] = fvec(x,flag)
%function example to specify the nonlinear system. f contains the function
%values and J contains the Jacobian matrix, which is evaluated only when
%flag=1.

n = length(x);
f = zeros(n,1); A = zeros(n,n);

f(1) = 3*x(1) - cos(x(2)*x(3)) - 0.5;
f(2) = 4*x(1)^2 - 625*x(2)^2 + 2*x(2) - 1;
f(3) = exp(-x(1)*x(2)) + 20*x(3) + 10*pi()/3 -1;

%for Broyden's method the following is skipped except in the initial
%iteration.

if flag==1
    A(1,1) = 3;
    A(1,2) = sin(x(2)*x(3))*x(3);
    A(1,3) = sin(x(2)*x(3))*x(2);
    A(2,1) = 8*x(1);
    A(2,2) = -1250*x(2) + 2;
    A(2,3) = 0;
    A(3,1) = -x(2)*exp(-x(1)*x(2));
    A(3,2) = -x(1)*exp(-x(1)*x(2));
    A(3,3) = 20;
end

end