%
%   NLDEMO3_LESSON6.M  Solve nonlinear demo using Matlab's fsolve function
%
%   This file computes the solution for a 3rd order nonlinear system using 
%   Matlab's built-in fsolve function.  
%   
%   Note that NLDEMO1 and NLDEMO2... solve this same problem using other methods...
%
%   File prepared by J. R. White, UMass-Lowell (last update:  Nov. 2017)
%
      
      clear all;   close all;   nfig = 0;
%
%   set reasonable initial guess 
      x2 = input('input a guess for x2: ');  
      if x2 == 3, x2 = 3.001; end   % don't allow x2 = 3 as guess
      x1 = (-7+2*x2*x2)/3;          % satisfies eqn. 1
      x3 = -5/(3-x2)  ;             % satisfies eqn. 3
      xo = [x1 x2 x3]';   
      n = length(xo);
%
%   call fsolve and edit solution
      fx = @(x) [3*x(1)-2*x(2)*x(2)+7;
                 -x(1)*x(2)+3*x(2)-2*x(3)-9;
                 -x(2)*x(3)+3*x(3)+5];
      x = fsolve(fx,xo);
      fprintf(1,'\n Solution edit for NLDemo3  \n');
      for j = 1:n
        fprintf(1,'    x(%1i) = %10.5f    \n',j,x(j));
      end
%
%   end of file