%
%   SERIES_1.M   Evaluation of a discrete series from different views...
%
%   This file illustrate how to evaluate a discrete series and it emphasizes the
%   difference between the for ... end loop and a while ... end structure.
%
%   Evaluate the series:  f = sum(k-2)^2 for k = 1, 2, ... M
%     Case 1:  Find f for M = 30
%     Case 2:  Find minimun value of M such that f >= 5000
%
%   Prepared by J.R White, UMass-Lowell (last update: Sept. 2017)
%

     clear all, close all
%
%   Case 1:  Find f for M = 30
      f = 0;  M = 30;
      for k = 1:M
        f = f + (k-2)^2;
      end
      fprintf(' Case 1: Value of f for M = %4i is %6i \n\n',M,f)
%
%   Case 2:  Find minimun value of M such that f >= 5000
      ff = 5000;  f = 0;   k = 0;
      while ff > f
        k = k+1;
        f = f + (k-2)^2;
      end
      M = k;
      fprintf(' Case 2: Value of f = %6i for M = %4i  \n\n',f,M)
%
%   end of program