%
%   ANALYTICAL_IVP1.M   Plot Implicit Solution to a given IVP
%
%   This problem illustrates how to evaluate and plot a function in implicit
%   form.  The function of interest here is the analytical solution to the IVP 
%   given by
%               -(2xy^3 + y^4)
%        y' = --------------------    with y(0) = 1
%                  xy^3 -2)
%
%   The goal is to plot y(x) vs x for the implicit solution to the IVP given by:
%        u(x,y) = x^2 + xy + y^(-2) - 1 = 0
%
%   This requires use of Matlab's fzero root finding routine with the implicit
%   function evaluated within an anonymous function.  To plot y versus x, we find 
%   the 'root' of this implicit equation for each value of x, and then plot the 
%   results.  
%
%   File prepared by J. R. White, UMass-Lowell (last update:  Nov. 2017)
%

      clear all, close all,  nfig = 0;
%
%   initial setup 
      xo = 0;   xf = 2;   yo = 1;  Nx = 2001;   
      xe = linspace(xo,xf,Nx);    ye = zeros(size(xe));   
%
%   now evaluate the implicit eqn at each point (uses previous y-value as guess)
      xe(1)= xo;    ye(1) = yo;
      for i = 2:Nx
        x = xe(i);  
        fy = @(y) x^2 + x*y + y^(-2) - 1;  % anonymous function for used in fzero
        ye(i) = fzero(fy,ye(i-1));  
      end
%
%   now plot results 
      nfig = nfig+1;   figure(nfig)
      plot(xe,ye,'b-','LineWidth',2)
      title('Analytical\_IVP1:  Plot of Implicit Solution to given IVP')
      xlabel('x values'),ylabel('y(x)'), grid, hold on
%
%   end of simulation