function E = r8mat_expm3 ( A )

%*****************************************************************************80
%
%% R8MAT_EXPM3 approximates the matrix exponential using an eigenvalue approach.
%
%  Discussion:
%
%    exp(A) = V * D * V
%
%    where V is the matrix of eigenvectors of A, and D is the diagonal matrix
%    whose i-th diagonal entry is exp(lambda(i)), for lambda(i) an eigenvalue
%    of A.
%
%    This function is accurate for matrices which are symmetric, orthogonal,
%    or normal.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    18 March 2011
%
%  Author:
%
%    Cleve Moler, Charles Van Loan
%
%  Reference:
%
%    Cleve Moler, Charles VanLoan,
%    Nineteen Dubious Ways to Compute the Exponential of a Matrix,
%    Twenty-Five Years Later,
%    SIAM Review,
%    Volume 45, Number 1, March 2003, pages 3-49.
%
%  Parameters:
%
%    Input, real A(N,N), the matrix.
%
%    Output, real E(N,N), the estimate for exp(A).
%
  [ V, D ] = eig ( A );
  E = V * diag ( exp ( diag ( D ) ) ) / V;

  return
end


function p = predict(Theta1, Theta2, X)
%PREDICT Predict the label of an input given a trained neural network
%   p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
%   trained weights of a neural network (Theta1, Theta2)

% Useful values
m = size(X, 1);
num_labels = size(Theta2, 1);

% You need to return the following variables correctly
p = zeros(size(X, 1), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned neural network. You should set p to a
%               vector containing labels between 1 to num_labels.
%
% Hint: The max function might come in useful. In particular, the max
%       function can also return the index of the max element, for more
%       information see 'help max'. If your examples are in rows, then, you
%       can use max(A, [], 2) to obtain the max for each row.
%

X = [ones(m, 1) X];
temp = sigmoid(X * Theta1');
temp = [ones(m, 1) temp];
r = sigmoid(temp * Theta2');
for i = 1:m
    [~, temp] = max(r(i, :));
    p(i) = temp;
end

% =========================================================================

end


function n = hex_grid_01_n ( nodes_per_layer )

%*****************************************************************************80
%
%% HEX_GRID_01_N computes the number of unit square hex grid points.
%
%  Discussion:
%
%    This routine determines the value of N, the number of
%    hex grid points, from the fundamental hexagonal grid
%    parameter NODES_PER_LAYER.
%
%    A hexagonal grid is defined in the unit square [0,1] x [0,1].
%
%    All nodes of the grid lie on one of LAYERS horizontal lines.
%    The first of these lines is the X axis, and each successive
%    line is HY units higher.
%
%    On all the odd numbered lines, there are NODES_PER_LAYER points,
%    equally spaced from 0 to 1, with a spacing of HX.
%
%    On the even numbered lines, there are NODES_PER_LAYER-1 points,
%    whose values are the midpoints of successive intervals on
%    an odd numbered line.  (The grid is staggered).
%
%    HY = HX * sqrt ( 3 ) / 2.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    08 March 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer NODES_PER_LAYER, the number of grid points on the first
%    horizontal layer of points.
%
%    Output, integer N, the number of hex grid points.
%
  if ( nodes_per_layer < 1 )

    n = 0;

  elseif ( nodes_per_layer == 1 )

    n = 1;

  else

    layers = hex_grid_01_layers ( nodes_per_layer );

    n = nodes_per_layer       * floor ( ( layers + 1 ) / 2 ) + ...
      ( nodes_per_layer - 1 ) * floor ( ( layers     ) / 2 );

  end

  return
end


function fx = p25_fun ( n, x )

%*****************************************************************************80
%
%% P25_FUN evaluates the integrand for problem 25.
%
%  Interval:
%
%    0 <= x <= 1.
%
%  Integrand:
%
%    ln ( abs ( x - 0.7 ) )
%
%  Exact Integral:
%
%    0.3 * ln ( 0.3 ) + 0.7 * ln ( 0.7 ) - 1
%
%  Approximate Integral (20 digits):
%
%    -1.6108643020548934630
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 November 2009
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Kendall Atkinson,
%    An Introduction to Numerical Analysis,
%    Prentice Hall, 1984, page 303.
%
%  Parameters:
%
%    Input, integer N, the number of evaluation points.
%
%    Input, real X(N), the evaluation points.
%
%    Output, real FX(N), the integrand values.
%
  fx = log ( abs ( x - 0.7 ) );

  return
end


function f = local_xy ( x , y )

%*****************************************************************************80
%
%% LOCAL_XY computes the local minimum function.
%
%  Discussion:
%
%    This function has a local minimum:
%
%      X* = (  0.28581..., 0.27936...), F(X*) = 5.9225...
%
%    and a global minimum:
%
%      X* = (  -21.026653..., -36.760090...), F(X*) = 0.
%
%    Suggested starting point:
%
%      X = ( 1, 1 ), F(X) = 3.33 * 10^6.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    12 February 2008
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    David Himmelblau,
%    Applied Nonlinear Programming,
%    McGraw Hill, 1972,
%    ISBN13: 978-0070289215,
%    LC: T57.8.H55.
%
%  Parameters:
%
%    Input, real X, Y, the argument of the function.
%
%    Output, real F, the value of the function at (X,Y).
%
  f = ( x^2 + 12 * y - 1 )^2 ...
    + ( 49 * x^2 + 49 * y^2 + 84 * x + 2324 * y - 681 )^2;

  return
end


function loc=RLSloc(X)
%Syntax: loc=RLSloc(X)
%_____________________
%
% Calculates the Reweighterd Least Squares (RLS) location parameter
% of the columns of a matrix X. If X is a vector, it returns the RLS
% location parameter of its components. If X is a scalar, it returns
% X.
%
% loc is the RLS estimated vector of locations.
% X is the matrix with the data sets.
%
% Reference:
% Rousseeuw PJ, Leroy AM (1987): Robust regression and outlier detection. Wiley.
%
%
% Alexandros Leontitsis
% Institute of Mathematics and Statistics
% University of Kent at Canterbury
% Canterbury
% Kent, CT2 7NF
% U.K.
%
% University e-mail: al10@ukc.ac.uk (until December 2002)
% Lifetime e-mail: leoaleq@yahoo.com
% Homepage: http://www.geocities.com/CapeCanaveral/Lab/1421
%
% Sep 3, 2001.

if nargin<1 | isempty(X)==1
   error('Not enough input arguments.');
else
   % If X is a row vector make it a column vector
   if size(X,1)==1
      X=X';
   end
   % n is the length of the data set
   n=size(X,1);
end

% For a single data point there is no need to proceed
if n==1
   loc=X;
else
   % Find the LMS location parameter
   l=LMSloc(X);
   % Find the LMS scale parameter
   s=LMSsca(X);

   % Calculate the location parameter for every column of X
   for i=1:size(X,2);
      r=X(:,i)-l(i);
      if s(i)==0
         w=1:n;
      else
         w=find(abs(r)/s(i)<=2.5);
      end
      loc(i)=mean(X(w,i));
   end
end


function [neighborIds neighborDistances] = kNearestNeighbors(dataMatrix, queryMatrix, k)
%--------------------------------------------------------------------------
% Program to find the k - nearest neighbors (kNN) within a set of points.
% Distance metric used: Euclidean distance
%
% Usage:
% [neighbors distances] = kNearestNeighbors(dataMatrix, queryMatrix, k);
% dataMatrix  (N x D) - N vectors with dimensionality D (within which we search for the nearest neighbors)
% queryMatrix (M x D) - M query vectors with dimensionality D
% k           (1 x 1) - Number of nearest neighbors desired
%
% Example:
% a = [1 1; 2 2; 3 2; 4 4; 5 6];
% b = [1 1; 2 1; 6 2];
% [neighbors distances] = kNearestNeighbors(a,b,2);
%
% Output:
% neighbors =
%      1     2
%      1     2
%      4     3
%
% distances =
%          0    1.4142
%     1.0000    1.0000
%     2.8284    3.0000
%--------------------------------------------------------------------------

neighborIds = zeros(size(queryMatrix,1),k);
neighborDistances = neighborIds;

numDataVectors = size(dataMatrix,1);
numQueryVectors = size(queryMatrix,1);
for i=1:numQueryVectors,
    dist = sum((repmat(queryMatrix(i,:),numDataVectors,1)-dataMatrix).^2,2);
    [sortval sortpos] = sort(dist,'ascend');
    neighborIds(i,:) = sortpos(1:k);
    neighborDistances(i,:) = sqrt(sortval(1:k));
end


function f = local ( x )

%*****************************************************************************80
%
%% LOCAL computes the local function.
%
%  Discussion:
%
%    This function has a local minimizer:
%
%      X* = ( 0.2858054412..., 0.2793263206...), F(X*) = 5.9225...
%
%    and a global minimizer:
%
%      X* = ( -21.02667179..., -36.75997872...), F(X*) = 0.
%
%    Suggested starting point for local minimizer:
%
%      X = ( 1, 1 ), F(X) = 3.33 * 10^6.
%
%    Suggested starting point for global minimizer:
%
%      X = ( -15, -35), F(X) = 1.49 * 10^8.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    01 January 2012
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    David Himmelblau,
%    Applied Nonlinear Programming,
%    McGraw Hill, 1972,
%    ISBN13: 978-0070289215,
%    LC: T57.8.H55.
%
%  Parameters:
%
%    Input, real X(2), the argument of the function.
%
%    Output, real F, the value of the function at X.
%
  if ( length ( x ) ~= 2 )
    error ( 'Error: function expects a two dimensional input\n' );
  end

  f = ( x(1)^2 + 12 * x(2) - 1 )^2 ...
    + ( 49 * x(1)^2 + 49 * x(2)^2 + 84 * x(1) + 2324 * x(2) - 681 )^2;

  return
end


function [ vX, paramLambda ] = SolveLsNormConst( mA, vB, normConst )
% ----------------------------------------------------------------------------------------------- %
%[ vX ] = SolveLsNormConst( mA, vB, normConst )
% Solves norm constrained Least Squares problem by finding the optimal Dual
% Variable (paramLambda) of the KKT Conditions by succesively solving
% Tikhonov Regularized Least Squares problems.
% The objective Funciton is given by:
% \arg \min_{x} \frac{1}{2} {\left\| A x - b \right\|}_{2}^{2}
% subject to {\left\| x \right\|}_{2} \leq normSquaredConst
% Input:
%   - mA                -   Input Matrix.
%                           The given matrix of the problem 0.5 || A x - b|| ^ 2.
%                           Structure: Vector (Column Vector).
%                           Type: 'Single' / 'Double'.
%                           Range: (-inf, inf).
%   - vB                -   Input Vector.
%                           The given vector of the problem 0.5 || A x - b|| ^ 2.
%                           Structure: Scalar.
%                           Type: 'Single' / 'Double'.
%                           Range: {1, 2, ...}.
%   - normConst         -   Norm Constraint.
%                           The solution must obey || x || <= normConst.
%                           Structure: Scalar.
%                           Type: 'Single' / 'Double'.
%                           Range: (0, inf).
% Output:
%   - vX                -   Solution Vector.
%                           The optimal solution of the problem 0.5 || A x
%                           - b || ^ 2 subject to || x || <= normConst.
%                           Structure: Vector (Column Vector).
%                           Type: 'Single' / 'Double'.
%                           Range: (-inf, inf).
% References
%   1.  See - https://stats.stackexchange.com/questions/401212.
% Remarks:
%   1.  T
% TODO:
%   1.  U
% Release Notes:
%   -   1.0.000     14/04/2019
%       *   First realease version.
% ----------------------------------------------------------------------------------------------- %

paramLambda = 0; %<! Initialization

vX = pinv(mA) * vB;
mI = eye(size(vX, 1));

mAA = mA.' * mA;
mAb = mA.' * vB;

if(norm(vX, 2) <= normConst)
    return;
end

hObjFun = @(paramLambda) norm((mAA + (2 * paramLambda * mI)) \ mAb, 2) - normConst;

paramLambda = fzero(hObjFun, paramLambda);

vX = (mAA + (2 * paramLambda * mI)) \ mAb;

end


function length = polyloop_length_nd ( dim_num, nk, pk )

%*****************************************************************************80
%
%% POLYLOOP_LENGTH_ND computes the length of a polyloop in ND.
%
%  Discussion:
%
%    A polyloop of order NK is the geometric structure consisting of
%    the NK line segments that lie between successive elements of a list
%    of NK points, with the last point joined to the first.
%
%    Warning: I just made up the word "polyloop".
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 March 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer DIM_NUM, the spatial dimension.
%
%    Input, integer NK, the number of points defining the polyloop.
%
%    Input, real PK(DIM_NUM,NK), the points defining the polyloop.
%
%    Output, real LENGTH, the length of the polyloop.
%
  length = 0.0;

  for i = 2 : nk + 1

    if ( i <= nk )
      j = i;
    else
      j = 1;
    end if

    length = length + sqrt ( sum ( ( pk(1:dim_num,j) - pk(1:dim_num,i-1) ).^2 ) );

  end

  return
end


function error_frobenius = inverse_error ( n, a, b )

%*****************************************************************************80
%
%% INVERSE_ERROR determines the error in an inverse matrix.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    29 October 2013
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the matrix.
%
%    Input, real A(N,N), the matrix.
%
%    Input, real B(N,N), the inverse.
%
%    Output, real ERROR_FROBENIUS, the Frobenius norm
%    of (A*B-I) + (B*A-I).
%
  error_frobenius = norm ( a * b - eye ( n ), 'fro' ) ...
                  + norm ( b * a - eye ( n ), 'fro' );

  return
end


function value = r8mat_norm_fro ( m, n, a )

%*****************************************************************************80
%
%% R8MAT_NORM_FRO returns the Frobenius norm of an R8MAT.
%
%  Discussion:
%
%    The Frobenius norm is defined as
%
%      value = sqrt ( sum ( 1 <= I <= M ) sum ( 1 <= j <= N ) A(I,J)**2 )
%
%    The matrix Frobenius norm is not derived from a vector norm, but
%    is compatible with the vector L2 norm, so that:
%
%      vec_norm_l2 ( A * x ) <= mat_norm_fro ( A ) * vec_norm_l2 ( x ).
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    24 April 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer M, the number of rows in A.
%
%    Input, integer N, the number of columns in A.
%
%    Input, real A(M,N), the matrix whose Frobenius
%    norm is desired.
%
%    Output, real VALUE, the Frobenius norm of A.
%
  value = sqrt ( sum ( sum ( a(1:m,1:n).^2 ) ) );

  return
end


function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
%   theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by
%   taking num_iters gradient steps with learning rate alpha

% Initialize some useful values
m = length(y); % number of training examples
n = size(X , 2);
J_history = zeros(num_iters, 1);

for iter = 1:num_iters

    % ====================== YOUR CODE HERE ======================
    % Instructions: Perform a single gradient step on the parameter vector
    %               theta.
    %
    % Hint: While debugging, it can be useful to print out the values
    %       of the cost function (computeCostMulti) and gradient here.
    %

	H = X * theta;
	T = zeros(n , 1);
	for i = 1 : m,
		T = T + (H(i) - y(i)) * X(i,:)';
	end

	theta = theta - (alpha * T) / m;

    % ============================================================

    % Save the cost J in every iteration
    J_history(iter) = computeCostMulti(X, y, theta);

end

end


function y = nanstd(x, flag, dim)
% nanstd - Standard deviation ignoring NaNs
%
% Synopsis:
%   y = nanstd(x)
%   y = nanstd(x,flag)
%   y = nanstd(x,flag,dim)
%
% Arguments:
%   x: Matrix or vector
%   flag: Logical. Default: false (normalize by N-1). If true: normalize by N
%       (produce the second moment about the mean)
%   dim: Dimension along which to operate
%
% Returns:
%   y: Standard deviation along the chosen dimension
%
% Description:
%   nanstd(X) returns the standard deviation treating NaNs
%   as missing values.
%   For vectors, nanstd(X) is the standard deviation of the
%   non-NaN elements in X.  For matrices, nanstd(X) is a row
%   vector containing the standard deviation of each column,
%   ignoring NaNs.
%
% Examples:
%   nanstd([1 2 NaN]) returns 0.7071 (the same result as std([1 2])
%   nanstd([1 2 NaN; 3 4 5], 1, 1) returns [1 1 0]
%
% See also: nanmean,nansum
%

% Author(s): Anton Schwaighofer, Feb 2005

if nargin<3,
  % Operate along the first non-singleton dimension
  dim = min(find(size(x)~=1));
  if isempty(dim),
    dim = 1;
  end
end
if nargin<2,
  flag = 0;
end
if isempty(x),
  y = 0/0;
  return;
end
tile = ones(1, max(ndims(x), dim));
tile(dim) = size(x,dim);
% Effective number of non-NaNs
effSize = size(x,dim)-sum(isnan(x),dim);
% Remove mean, also ignoring NaNs
xc = x - repmat(nanmean(x, dim), tile);
if flag,
  y = sqrt(nansum(conj(xc).*xc, dim)./effSize);
else
  y = sqrt(nansum(conj(xc).*xc, dim)./(effSize-1));
  % Simulate the behaviour of std: if only one sample, output 0
  y(effSize==1) = 0;
end


function a = mass_matrix_t3 ( node_num, element_num, element_node, x, y )

%*****************************************************************************80
%
%% MASS_MATRIX_T3 computes the mass matrix, using 3-node triangles.
%
%  Discussion:
%
%    The mass matrix to be estimated has the form:
%
%      A(I,J) = integral ( PHI(I)(X,Y) * PHI(J)(X,Y) ) d Region
%
%    where PHI(I) and PHI(J) are the shape functions associated with
%    the I-th and J-th variables.
%
%  Referene Element T6:
%
%    |
%    1  3
%    |  |\
%    |  | \
%    S  |  \
%    |  |   \
%    |  |    \
%    0  1-----2
%    |
%    +--0--R--1-->
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    11 January 2013
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer NODE_NUM, the number of nodes.
%
%    Input, integer ELEMENT_NUM, the number of elements.
%
%    Input, integer ELEMENT_NODE(3,ELEMENT_NUM), the nodes that make
%    up each element.
%
%    Input, real X(NODE_NUM), Y(NODE_NUM), the coordinates
%    of the nodes.
%
%    Output, real A(NODE_NUM,NODE_NUM), the mass matrix.
%
  element_order = 3;
%
%  Zero out the matrix.
%
  a(1:node_num,1:node_num) = 0.0;
%
%  Get the weights and abscissas for a unit triangle.
%
  rule = 4;
  quad_num = triangle_unit_size ( rule );
  [ rtab, stab, weight ] = triangle_unit_set ( rule );
%
%  For each element.
%
  for element = 1 : element_num

    p1 = element_node(1,element);
    p2 = element_node(2,element);
    p3 = element_node(3,element);

    area = 0.5 * abs ( ...
        x(p1) * ( y(p2) - y(p3) ) ...
      + x(p2) * ( y(p3) - y(p1) ) ...
      + x(p3) * ( y(p1) - y(p2) ) );

    if ( area == 0.0 )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'MASS_MATRIX_T3 - Fatal error!\n' );
      fprintf ( 1, '  Zero area for element %d\n', element );
      error ( 'MASS_MATRIX_T3 - Fatal error!' );
    end
%
%  For each quadrature point in the element...
%
    for quad = 1 : quad_num

      r = rtab(quad);
      s = stab(quad);

      [ w, dwdr, dwds ] = shape_t3 ( r, s );
%
%  For each basis function PHI(I) associated with a node in the element,
%
      for iq = 1 : element_order

        ip = element_node(iq,element);
%
%  For each "neighbor" basis function PHI(J) associated with a node in
%  the element.
%
        for jq = 1 : element_order

          jp = element_node(jq,element);

          a(ip,jp) = a(ip,jp) + area * weight(quad) * w(iq) * w(jq);

        end
      end
    end
  end

  return
end


function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
%   theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by
%   taking num_iters gradient steps with learning rate alpha

% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);

for iter = 1:num_iters

    % ====================== YOUR CODE HERE ======================
    % Instructions: Perform a single gradient step on the parameter vector
    %               theta.
    %
    % Hint: While debugging, it can be useful to print out the values
    %       of the cost function (computeCost) and gradient here.
    %

    h = X * theta;
    errors = h - y;
    delta = X' * errors;
    theta = theta - (alpha / m) * delta;

    % ============================================================

    % Save the cost J in every iteration
    J_history(iter) = computeCost(X, y, theta);

end

end


function p = predict(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic
%regression parameters theta
%   p = PREDICT(theta, X) computes the predictions for X using a
%   threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)

m = size(X, 1); % Number of training examples

% You need to return the following variables correctly
p = zeros(m, 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned logistic regression parameters.
%               You should set p to a vector of 0's and 1's
%

p = sigmoid(X * theta)>=0.5 ;

% =========================================================================

end


function exactness_test08 ( )

%*****************************************************************************80
%
%% EXACTNESS_TEST08 tests Gauss-Chebyshev1 rules for the Chebyshev1 integral.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    27 May 2014
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'EXACTNESS_TEST08\n' );
  fprintf ( 1, '  Test Gauss-Chebyshev1 rules for the Chebyshev1 integral.\n' );
  fprintf ( 1, '  Density function rho(x) = 1/sqrt(1-x^2).\n' );
  fprintf ( 1, '  Region: -1 <= x <= +1.\n' );
  fprintf ( 1, '  Exactness: 2*N-1.\n' );

  for n = 1 : 5

    [ x, w ] = chebyshev1_set ( n );
    p_max = 2 * n;
    chebyshev1_exactness ( n, x, w, p_max );

  end

  return
end


function l = l1dd_inverse ( n, h )

%*****************************************************************************80
%
%% L1DD_INVERSE stores the inverse of the 1D DD Laplacian.
%
%  Discussion:
%
%    The N grid points are assumed to be evenly spaced by H.
%
%    For N = 5, the discrete Laplacian with Dirichlet boundary conditions
%    at both ends of [0,6] has the matrix form L:
%
%       2 -1  0  0  0
%      -1  2 -1  0  0
%       0 -1  2 -1  0
%       0  0 -1  2 -1
%       0  0  0 -1  2
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    29 October 2013
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the number of points.
%    N must be at least 3.
%
%    Input, real H, the spacing between points.
%
%    Output, real L(N,N), the inverse of the Laplacian matrix.
%
  if ( n < 3 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'L1DD_INVERSE - Fatal error!\n' );
    fprintf ( 1, '  N < 3.\n' );
    error ( 'L1DD_INVERSE - Fatal error!' );
  end

  l = zeros ( n, n );

  for j = 1 : n
    for i = 1 : n
      l(i,j) = min ( i, j ) * ( n + 1 - max ( i, j ) ) * h * h / ( n + 1 );
    end
  end

  return
end


%% Machine Learning Online Class - Exercise 2: Logistic Regression
%
%  Instructions
%  ------------
%
%  This file contains code that helps you get started on the logistic
%  regression exercise. You will need to complete the following functions
%  in this exericse:
%
%     sigmoid.m
%     costFunction.m
%     predict.m
%     costFunctionReg.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%

%% Initialization
clear ; close all; clc

%% Load Data
%  The first two columns contains the exam scores and the third column
%  contains the label.

data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3);

%% ==================== Part 1: Plotting ====================
%  We start the exercise by first plotting the data to understand the
%  the problem we are working with.

fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...
         'indicating (y = 0) examples.\n']);

plotData(X, y);

% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')

% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

%% ============ Part 2: Compute Cost and Gradient ============
%  In this part of the exercise, you will implement the cost and gradient
%  for logistic regression. You neeed to complete the code in
%  costFunction.m

%  Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X);

% Add intercept term to x and X_test
X = [ones(m, 1) X];

% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);

% Compute and display initial cost and gradient
[cost, grad] = costFunction(initial_theta, X, y);

fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

%% ============= Part 3: Optimizing using fminunc  =============
%  In this exercise, you will use a built-in function (fminunc) to find the
%  optimal parameters theta.

%  Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);

%  Run fminunc to obtain the optimal theta
%  This function will return theta and the cost
[theta, cost] = ...
	fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

% Print theta to screen
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('theta: \n');
fprintf(' %f \n', theta);

% Plot Boundary
plotDecisionBoundary(theta, X, y);

% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')

% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

%% ============== Part 4: Predict and Accuracies ==============
%  After learning the parameters, you'll like to use it to predict the outcomes
%  on unseen data. In this part, you will use the logistic regression model
%  to predict the probability that a student with score 45 on exam 1 and
%  score 85 on exam 2 will be admitted.
%
%  Furthermore, you will compute the training and test set accuracies of
%  our model.
%
%  Your task is to complete the code in predict.m

%  Predict probability for a student with score 45 on exam 1
%  and score 85 on exam 2

prob = sigmoid([1 45 85] * theta);
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...
         'probability of %f\n\n'], prob);

% Compute accuracy on our training set
p = predict(theta, X);

fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

github-repos/MATLAB/Borye-machine-learning-coursera-1/machine-learning-coursera-1-033fdc2e6da393eeb1179a09aafe92362021effb/Week 3 Assignments/Logistic Regression and Regularization/mlclass-ex2/ex2.m


function a = cauchy ( n, x, y )

%*****************************************************************************80
%
%% CAUCHY returns the CAUCHY matrix.
%
%  Formula:
%
%    A(I,J) = 1.0 / ( X(I) + Y(J) )
%
%  Example:
%
%    N = 5, X = ( 1, 3, 5, 8, 7 ), Y = ( 2, 4, 6, 10, 9 )
%
%    1/3  1/5  1/7  1/11 1/10
%    1/5  1/7  1/9  1/13 1/12
%    1/7  1/9  1/11 1/15 1/14
%    1/10 1/12 1/14 1/18 1/17
%    1/9  1/11 1/13 1/17 1/16
%
%    or, in decimal form,
%
%    0.333333      0.200000      0.142857      0.0909091     0.100000
%    0.200000      0.142857      0.111111      0.0769231     0.0833333
%    0.142857      0.111111      0.0909091     0.0666667     0.0714286
%    0.100000      0.0833333     0.0714286     0.0555556     0.0588235
%    0.111111      0.0909091     0.0769231     0.0588235     0.0625000
%
%  Properties:
%
%    A is generally not symmetric: A' /= A.
%
%    A is totally positive if 0 < X(1) < ... < X(N) and 0 < Y1 < ... < Y(N).
%
%    A will be singular if any X(I) equals X(J), or
%    any Y(I) equals Y(J), or if any X(I)+Y(J) equals zero.
%
%    A is generally not normal: A' * A /= A * A'.
%
%    The Hilbert matrix is a special case of the Cauchy matrix.
%
%    The Parter matrix is a special case of the Cauchy matrix.
%
%    The Ris or "ding-dong" matrix is a special case of the Cauchy matrix.
%
%    det ( A ) = product ( 1 <= I < J <= N ) ( X(J) - X(I) )* ( Y(J) - Y(I) )
%           / product ( 1 <= I <= N, 1 <= J <= N ) ( X(I) + Y(J) )
%
%    The inverse of A is
%
%      INVERSE(A)(I,J) = product ( 1 <= K <= N ) [ (X(J)+Y(K)) * (X(K)+Y(I)) ] /
%            [ (X(J)+Y(I)) * product ( 1 <= K <= N, K /= J ) (X(J)-X(K))
%                          * product ( 1 <= K <= N, K /= I ) (Y(I)-Y(K)) ]
%
%    The family of matrices is nested as a function of N.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 January 2015
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Robert Gregory, David Karney,
%    Example 3.26,
%    A Collection of Matrices for Testing Computational Algorithms,
%    Wiley, New York, 1969, page 54,
%    LC: QA263.G68.
%
%    Nicholas Higham,
%    Accuracy and Stability of Numerical Algorithms,
%    SIAM, 1996.
%
%    Donald Knuth,
%    The Art of Computer Programming,
%    Volume 1, Fundamental Algorithms, Second Edition
%    Addison-Wesley, Reading, Massachusetts, 1973, page 36.
%
%    Olga Taussky, Marvin Marcus,
%    Eigenvalues of finite matrices,
%    in Survey of Numerical Analysis,
%    Edited by John Todd,
%    McGraw-Hill, New York, pages 279-313, 1962.
%
%    Evgeny Tyrtyshnikov,
%    Cauchy-Toeplitz matrices and some applications,
%    Linear Algebra and Applications,
%    Volume 149, 1991, pages 1-18.
%
%  Parameters:
%
%    Input, integer N, the order of A.
%
%    Input, real X(N), Y(N), vectors that determine A.
%
%    Output, real A(N,N), the matrix.
%
  for i = 1 : n

    for j = 1 : n

      if ( x(i) + y(j) == 0.0 )
        fprintf ( 1, '\n' );
        fprintf ( 1, 'CAUCHY - Fatal error!\n' );
        fprintf ( 1, '  The denominator X(I)+Y(J) was zero\n' );
        fprintf ( 1, '  for I = %d\n', i );
        fprintf ( 1, '  X(I)  = %g\n', x(i) );
        fprintf ( 1, '  and J = %d\n', j );
        fprintf ( 1, '  Y(J)  = %g\n', y(j) );
        error ( 'CAUCHY - Fatal error!' );
      end

      a(i,j) = 1.0 / ( x(i) + y(j) );

    end
  end

  return
end


function p=unrankTComposition(theRank,t,n,firstElMostSig)
%%UNRANKTCOMPOSITION Return the composition of the given theRank, starting
%                   from zero, of n unlabeled items in t parts. That is, a
%                   method of putting n unlabeled balls into t labeled
%                   slots. Compositions are tuples of t integers >=1 that
%                   sum to n. Compositions are given in lexicographic order
%                   where the first element is the least significant unless
%                   otherwise specified.
%
%INPUTS: theRank The theRank (position in an ordered sequence) of the
%                desired composition starting from zero. There is a total
%                of binomial(n-1,t-1) compositions.
%              t The number of slots that can hold items, >=1.
%              n The number of items that are composed into slots, >=1;
%                n>=t
% firstElMostSig An optional parameter specifying whether the first
%                element is the most or least significant. The default if
%                omitted or an empty matrix is passed is false (the first
%                element is the least significant).
%
%OUTPUTS: p A tX1 vector holding the current composition, whose elements
%           sum to n. Each element is the number of "balls" in that slot.
%           If theRank is larger than the maximum number of compositions,
%           an empty matrix is returned. The values of q can range from 0
%           to n.
%
%This is an implementation of the relation described in Chapter 7.2.1.3 of
%[1] for mapping combinations to compositions. theRank is used to obtain a
%combination in lexicographic order (p(1) is the least significant
%element) and the elements of the combination are mapped to the equivalent
%composition. However, a special case is if the input is one-dimensional,
%in which case the only composition is for theRank=0 and the value is n.
%
%REFERENCES:
%[1] D. E. Knuth, The Art of Computer Programming. Vol. 4, Fascicle 3:
%    Generating all Combinations and Partitions, Upper Saddle River, NJ:
%    Addison-Wesley, 2009.
%
%October 2013 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(nargin<4||isempty(firstElMostSig))
    firstElMostSig=false;
end

n=n-1;
nCombo=n;
tCombo=t-1;

if(tCombo>0)
    c=unrankCombination(theRank,nCombo,tCombo);

    if(isempty(c))
        p=[];
        return;
    end

    %Transform the combination into a valid composition.
    p=zeros(t,1);
    p(1)=c(1)+1;
    for curIdx=2:tCombo
        p(curIdx)=c(curIdx)-c(curIdx-1);
    end
    p(t)=n-c(tCombo);
else%The t=1 case has to be handled separately.
    if(theRank==0)
        p=n+1;
    else
        p=[];
    end
end

if(firstElMostSig)
   p=flipud(p(:));
end

end

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.


function [H] = lpfilter(type,M,N,D0,n)
%LPFILTER Computes freq. domain lowpass filters
%		THIS IS NOT A STANDARD MATLAB FUNCTION
%		H = lpfilter (type,M,N,D0,n) creates the
%		transfer function of a lowpass filter, H, of
%		the specified type and size MxN. Acceptable
%		values for type, D0, and n are:
%
%				'ideal'				Ideal lowpass filter with
%									cutoff frequency D0. If
%									supplied, n is ignored.
%				'btw'				Butterworth lowpass filter
%									of order n, and cutoff D0.
%				'gauss'			    Gaussian lowpass filter with
%									cutoff (standard deviation)D0.
%									If supplied, n is ignored.
%		M and N should be even numbers for DFT
%		filtering.
%
%		Class support: double, uint8, uint16
%		The output is of class double

%		Verify the number of inputs

error(nargchk(4,5,nargin));

%		Issue warning if M or N not even

L1 = M/2;
L2 = N/2;
if((L1-round(L1)) | (L2-round(L2)))
		disp('Warning: M & N must be even for DFT filtering')
end

%		Set up meshgrid to compute filter functions
%		running from 0 to M-1 and 0 to N-1. But,
%		recall that U and V run from 1 to M and 1 to N,
%		repectively.  See Section 2.10.4 for an
%		explanation of meshgrid. Recall also that
%		meshgrid reverses the order of rows and
%		columns, so the final result must be transposed
%		in order to preserve the original order of
%		of the coordinates.

[U,V] = meshgrid(0:M-1,0:N-1);

%   Compute the filter center

U0=L1;
V0=L2;

%		Compute distances D(U,V)

D=((U-U0).^2 + (V-V0).^2).^0.5;

%		Begin filter computations

switch type
	case 'ideal'
		H = D<=D0;
        H = H';
	case 'btw'
		H = 1./(1 + (D./D0).^(2*n));
        H = H';
	case 'gauss'
		H = exp(-(D.^2)./(2*(D0^2)));
        H = H';
	otherwise
		error('Unknown filter type')
end

%		End of function


function [h,tf]=Jakes_Flat(fd,Ts,Ns,t0,E0,phi_N)
% Inputs:
%   fd      : Doppler frequency
%   Ts      : sampling period
%   Ns      : number of samples
%   t0      : initial time
%   E0      : channel power
%   phi_N  : inital phase of the maximum doppler frequency sinusoid
% Outputs:
%   h       : complex fading vector
%   t_state: current time
if nargin<6
    phi_N=0;
end
if nargin<5
    E0=1;
end
if nargin<4
    t0=0;
end
if nargin<3
    error('More arguments are needed for Jakes_Flat()');
end
N0=8;                 % As suggested by Jakes
N=4*N0+2;             % an accurate approximation
wd=2*pi*fd;           % Maximum doppler frequency[rad]
%t_state = t0;
%for i=1:Ns
%   ich=sqrt(2)*cos(phi_N)*cos(wd*t_state);
%   qch=sqrt(2)*sin(phi_N)*cos(wd*t_state);
%   for k=1:N0
%      phi_n=pi*k/(N0+1);
%      wn=wd*cos(2*pi*k/N);
%      ich=ich+2*cos(phi_n)*cos(wn*t_state);
%      qch=qch+2*sin(phi_n)*cos(wn*t_state);
%   end
%   h1(i) = E0/sqrt(2*N0+1)*complex(ich,qch);
%   t_state=t_state+Ts;             % save last time
%end
t = t0+[0:Ns-1]*Ts;
tf = t(end)+Ts;
coswt = [sqrt(2)*cos(wd*t); 2*cos(wd*cos(2*pi/N*[1:N0]')*t)]; % (2.32)
h = E0/sqrt(2*N0+1)*exp(j*[phi_N pi/(N0+1)*[1:N0]])*coswt;
% discrepancy = norm(h-h1)


function [average, weight] = CrossWeightAverage(DATA,WEIGHT)
% =========================================================================
% Compute the weighted average of DATA, computing the weights form the
% matrix WEIGHT. Note that if there are NaNs in DATA the weights are
% rescaled. WEIGHTS CANNOT contain NaN
% =========================================================================
% [average, weight] = CrossWeightAverage(DATA,WEIGHT)
% -------------------------------------------------------------------------
% INPUT
%	- DATA: panel of time series DATA(T,N) of T observation x N series
%       (NaN are accepted and weights rescaled)
%   - WEIGHT: time series of FLOWS or WEIGHTS (TxN). You can also
%       provide a (1xN) matrix in this case the code would automatically
%       compute fixed weights
% -------------------------------------------------------------------------
% OUTPUT
%	- average : weighted average vector (T x 1)
%	- weight  : matrix of weights (T x N)
% =========================================================================
% Example 1: foreign variables as in GVAR (example with fixed weights)
% x = [1 NaN  3 ;
%      2   3  4 ;
%      3   4  5 ;
%      4   5  6];
% w = [0  .4 .6];
% [average, weight] = CrossWeightAverage(x,w)
% -------------------------------------------------------------------------
% Example 2: weighted average (with time-varying weights)
% x = [ 1 NaN   3 ;
%       2   3   4 ;
%       3   4   5 ;
%       4   5   6];
% w = [.4  .2  .4 ;
%      .3  .4  .3 ;
%      .4  .3  .3 ;
%      .5  .3  .2];
% [average, weight] = CrossWeightAverage(x,w)
% =========================================================================
% Ambrogio Cesa Bianchi, March 2015

%% Preliminaries
% Check inputs
[r1, c1] = size(DATA);
[r2, c2] = size(WEIGHT);
if r2==1 && c2==c1
    % Create a matrix of fixed weights
    aux = [];
    for ii=1:r1
        aux = [aux; WEIGHT];
    end
    WEIGHT = aux;
end

% Re-Check inputs
[r1, c1] = size(DATA);
[r2, c2] = size(WEIGHT);
if r1~=r2 || c1~=c2
    disp(' ')
    disp('The matrices of data and weights are not conformable.')
    return
end

%% Cross section weihgted average
[nobs, nvars] = size(DATA);
nans = isnan(DATA);
WEIGHT(nans) = NaN;

average(1:nobs,1) = NaN;
weight(1:nobs,1:nvars) = NaN;

for ii=1:nobs
    aux_weight = WEIGHT(ii,:)./nansum(WEIGHT(ii,:));
    weight(ii,:) = aux_weight;
    aux_weight(nans(ii,:)) = 0;
    DATA(ii,nans(ii,:)) = 0;
    average(ii,1) = DATA(ii,:) * aux_weight';
end


function intVal=monomialIntPrism(alpha)
%%MONOMIALINTPRISM Evaluate the integral of prod_{i=1}^3 x(i)^alpha(i)
%  over a standard prism in 3D. This is a triangle that has been extruded
%  upward. The vertices are (1,-1,-1), (-1,-1,-1), (-1,1,-1), (1,-1,1),
%  (-1,-1,1), and (-1,1,1).
%
%INPUTS: alpha A 3X1 or 1X3 vector of the integer exponents of the
%              monomial term. All elements must be >=0.
%
%OUTPUTS: intVal The value of the specified integral.
%
%The solution was obtained via analytic integration. These types of
%explicit moment formulae are useful for testing cubature integration
%points.
%
%EXAMPLE:
%An analytic solution for alpha=[10;18;6] is -4/3861. Here, we
%show that this function produces the same result within resonable finite
%precision limits.
% alpha=[19;12;8];
% intSol=monomialIntPrism(alpha);
% trueSol=-4/3861;
% RelErr=(intSol-trueSol)/trueSol
%
%October 2022 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(mod(alpha(3),2)==0)
    a=alpha(1);
    b=alpha(2);
    c=alpha(3);

    intVal=(2*(-1)^a*(1+a)+2*(-1)^b*(1+b+(-1)^a*(2+a+b)))/((1+a)*(1+b)*(2+a+b)*(1+c));
else
    intVal=0;
end
end

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.


%% Example of dop2Dbox Applied to Illumination Correction
%
% (C) 2013 Matthew harker and Paul O'Leary
% Institute for Automation
% University of Leoben
% A-8700 Leoben
% Austria
%
% email: office@harkeroleary.org
%
% This program demonstrates the application of discrete orthogonal
% polynomials to illumination correction in an image.
%
% This tool requires the discrete orthogonal toolbox: dopBox:
%
%   http://www.mathworks.com/matlabcentral/fileexchange/41250
%
%%
close all;
clear;
%
%% Load the Test Data
% \cellName{loadData}
%
% This is an image of a metallurgical sample taken with a microscope
%
load metallurgy;
%
fig1 = figure;
imagesc( D );
colormap(gray);
axis image;
%
%\caption{Example metallurgical image, note the inhomogeneous illumination}
%
%% Generate the Basis Functions for the X and Y Directions
% \cellName{genBases}
%
% Determine the size of the image
%
[ny, nx] = size( D );
%
% Define the degree of the basis functions for the x and y directions
%
degreeX = 2;
degreeY = 2;
%
% The function call dop uses the number of basis functions not degree
%
noBfsX = degreeX + 1;
noBfsY = degreeY + 1;
%
% Generate the discrete orthogonal basis functions
%
Bx = dop( nx, noBfsX );
By = dop( ny, noBfsY );
%
%% Compute the 2D Polynomial Spectrum
% \cellName{computeSpec}
%
% Only a few basis functions have been used. Consequently the 2D polynomial
% is of low degree. The fit will model the global intensity of the image.
%
Sp = By' * D * Bx;
%
% generate scale vectors for the x and y directions
%
xScale = 0:degreeX;
yScale = 0:degreeY;
%
fig2 = figure;
imagesc( xScale, yScale, Sp );
xlabel('Degree in $$x$$');
ylabel('Degree in $$y$$');
colorbar;
%
% \caption{2D polynomial spectrum}
%
%% Compute the Illumination Approximation
% \cellName{approx}
%
% Reconstructing the 2D data set corresponds to the global illumination
% approximation.
%
Z = By * Sp * Bx';
%
fig3 = figure;
imagesc( Z );
colorbar;
colormap(gray);
%
% \caption{2D Polynomial approximation to the image. The low degree of
% approximation ensures that the details in the image are not modelled.
% In this manner the approximation corresponds to the global illumination}
%
%% Correct the Image Intensity
% \cellName{correct}
%
C = D - Z;
%
fig4 = figure;
imagesc( C );
colorbar;
colormap(gray);
%
% \caption{Metallurgical image corrected for the non-uniform illumination.}


function [ x, seed ] = c_normal_01_sample ( seed )

%*****************************************************************************80
%
%% C_NORMAL_01_SAMPLE samples the complex Normal 01 PDF.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    05 September 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer SEED, a seed for the random number generator.
%
%    Output, complex X, a sample of the PDF.
%
  [ v1, seed ] = r8_uniform_01 ( seed );
  [ v2, seed ] = r8_uniform_01 ( seed );

  x_r = sqrt ( - 2.0 * log ( v1 ) ) * cos ( 2.0 * pi * v2 );
  x_c = sqrt ( - 2.0 * log ( v1 ) ) * sin ( 2.0 * pi * v2 );

  x = x_r + i * x_c;

  return
end


function trans = createTranslation(varargin)
%CREATETRANSLATION Create the 3*3 matrix of a translation.
%
%   TRANS = createTranslation(DX, DY);
%   Returns the translation corresponding to DX and DY.
%   The returned matrix has the form :
%   [1 0 TX]
%   [0 1 TY]
%   [0 0  1]
%
%   TRANS = createTranslation(VECTOR);
%   Returns the matrix corresponding to a translation by the vector [x y].
%
%
%   See also:
%   transforms2d, transformPoint, createRotation, createScaling
%
%   ---------
%   author : David Legland
%   INRA - TPV URPOI - BIA IMASTE
%   created the 06/04/2004.
%

%   HISTORY
%   22/04/2009: rename as createTranslation

% process input arguments
if isempty(varargin)
    tx = 0;
    ty = 0;
elseif length(varargin)==1
    var = varargin{1};
    tx = var(1);
    ty = var(2);
else
    tx = varargin{1};
    ty = varargin{2};
end

% create the matrix representing the translation
trans = [1 0 tx ; 0 1 ty ; 0 0 1];


function [ x, w ] = ccoh_rule ( n )

%*****************************************************************************80
%
%% CCOH_RULE computes a Clenshaw Curtis Open Half rule.
%
%  Discussion:
%
%    Our convention is that the abscissas are numbered from left to right.
%
%    The rule is defined on [-1,1].
%
%    The integral to approximate:
%
%      Integral ( -1 <= X <= 1 ) F(X) dX
%
%    The quadrature rule:
%
%      Sum ( 1 <= I <= N ) W(I) * F ( X(I) )
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 February 2011
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the rule.
%
%    Output, real X(N), the abscissas.
%
%    Output, real W(N), the weights.
%
  x = zeros ( n, 1 );
  x =  ( 1 : 2 : 2 * n - 1 )' / ( 2 * n );
%
%  Chebyshev transformation from [0,1] to [-1,1].
%
  x = cos ( x * pi );
%
%  Compute the weights.
%
  x_min = -1.0;
  x_max = +1.0;

  w = nc_compute ( n, x_min, x_max, x );

  return
end


function f = p03_f ( m, c, w, n, x )

%*****************************************************************************80
%
%% P03_F evaluates the function for problem p03.
%
%  Discussion:
%
%    f(x) = 1 / ( 1 + sum ( c(1:m) * x(1:m) ) ) ^ ( m + 1 )
%
%    Default values are:
%
%    c(1:m) = 1/m
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    26 August 2012
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Alan Genz,
%    A Package for Testing Multiple Integration Subroutines,
%    in Numerical Integration: Recent Developments, Software
%    and Applications,
%    edited by Patrick Keast and Graeme Fairweather,
%    Reidel, 1987, pages 337-340,
%    ISBN: 9027725144,
%    LC: QA299.3.N38.
%
%  Parameters:
%
%    Input, integer M, the spatial dimension.
%
%    Input, real C(M,1), W(M,1), the problem parameters.
%
%    Input, integer N, the number of points.
%
%    Input, real X(M,N), the evaluation points.
%
%    Output, real F(N,1), the function values.
%
  f = ones ( n, 1 );

  for i = 1 : m
    f = f + c(i) .* x(i,:)';
  end
  f = 1.0 ./ f .^ ( m + 1 );

  return
end


function result = fit_rayleigh_pdf( x,y,W,hAx )
% fit_rayleigh_pdf - Non Linear Least Squares fit of the Rayleigh distribution.
%                    given the samples of the histogram of the samples, finds the
%                    distribution parameter that fits the histogram samples.
%
%    fits data to the probability of the form:
%        p(r)=r*exp(-r^2/(2*s))/s
%    with parameter: s
%
% format:   result = fit_rayleigh_pdf( x,y,W,hAx )
%
% input:    y   - vector, samples of the histogram to be fitted
%           x   - vector, position of the samples of the histogram (i.e. y = f(x,a))
%           W   - matrix or scalar, a square weighting matrix of the size NxN where
%                 N = length(y), or 0 to indicate no weighting is needed.
%           hAx - handle of an axis, on which the fitted distribution is plotted
%                 if h is given empty, a figure is created.
%
% output:   result  - structure with the fields
%                      s   - fitted parameter
%                      VAR - variance of the estimation
%                      type- weighted LS or not weighted LS
%                      iter- number of iteration for the solution
%

%
% Algorithm
% ===========
%
% We use the WLS algorithm to estimate the PDF from the samples.%
% The rayleigh distribution is given by:
%
%    p(x,s) = x*exp(-x^2/(2*s))/s
%
%    note that X is known and therefore, considered a constant vector
%
% The non liner WLS estimator is given by:
%
%    s(n+1) = s(n) + inv(H'*W*H)*(H') * (y-h) = s(n) + G * err
%
%    where:   h = p(x,s)
%             H = diff( p(x,a) ) with respect to "s"
%             W = weighting matrix of size NxN  (N = length(y))
%             s = a single parameter to be estimated
%
% The error estimation is given by:
%
%    VAR( s ) = G * VAR( err ) * (G')
%
%       or when W=I and the noise is a gaussian noise
%
%    VAR( s ) = inv( H' * H )
%

if (nargin<3)
    error( 'fit_rayleigh_pdf - insufficient input arguments' );
end

s       = x(find(y==max(y))).^2;        % initial guess
y       = y(:);                         % both should be column vectors !
x       = x(:);
x2      = x.^2;                         % save computation time
thresh  = 0.995;                        % convergence threshold for the loop
last_cnt= inf;
iter    = 0;

% check weight matrix input
if (size(W,1)==length(y)) & (size(W,2)==length(y))
    weights_flag    = 1;
    type            = 'WLS';
else
    weights_flag    = 0;
    type            = 'LS';
end

% Estimation
% =============
if (weights_flag)
    % loop for convergence (with weighting matrix)
    % =============================================
    while (1)
        iter    = iter + 1;
        h       = x.*exp( -x2/(2*s) )/s;
        H       = h.*(x2/(2*s^2) - 1/s);
        HTW     = H'*W;
        e       = inv( HTW * H ) * HTW * (y-h);
        s       = s + e;
        control = e*e;
        if ( control > (last_cnt * thresh) )
            break;
        else
            last_cnt = control;
        end
    end

    % summarize results
    h       = x.*exp( -x2/(2*s) )/s;
    H       = h.*(x2/(2*s^2) - 1/s);
    HTW     = H'*W;
    G       = inv( HTW * H ) * HTW;
    err     = (y-h);
    VAR     = G * var(err) * (G');
else

    % loop for convergence (without a weighting matrix)
    % ==================================================
    while (1)
        iter    = iter + 1;
        h       = x.*exp( -x2/(2*s) )/s;
        H       = h.*(x2/(2*s^2) - 1/s);
        HT      = H';
        control = inv( HT * H );
        s       = s + control * HT * (y-h);
        if ( control>(last_cnt * thresh) )
            break;
        else
            last_cnt = control;
        end
    end

	% summarize results
    h       = x.*exp( -x2/(2*s) )/s;
    H       = h.*(x2/(2*s^2) - 1/s);
    err     = (y-h);
    VAR     = inv( (H') * H );
end

% finish summarizing results
% ============================
result.s    = s;
result.VAR  = VAR;
result.RMS  = sqrt( (err')*err/ (x(2)-x(1))^2 / (length(err)-1) );
result.iter = iter;
result.type = type;

% plot distribution if asked for
% ===============================
if (nargin>3)
    if ishandle( hAx )
        plot_rayleigh( x,result,hAx,4 );
    else
        figure;
        plot_rayleigh( x,result1,gca,4 );
    end
end


% RM_histogram Computes the frequency histogram of the row vector x.
%   [RESULT,DESCRIPTOR] = HISTOGRAM(X) or
%   [RESULT,DESCRIPTOR] = HISTOGRAM(X,DESCRIPTOR) or
% where
%   DESCRIPTOR = [LOWER,UPPER,NCELL]
%
%   RESULT    : A row vector containing the histogram
%   DESCRIPTOR: The used descriptor
%
%   X         : The row vector be analyzed
%   DESCRIPTOR: The descriptor of the histogram
%     LOWER   : The lowerbound of the histogram
%     UPPER   : The upperbound of the histogram
%     NCELL   : The number of cells of the histogram
%
%   See also: http://www.cs.rug.nl/~rudy/matlab/
%
%   R. Moddemeijer
%   Copyright (c) by R. Moddemeijer
%   $Revision: 1.2 $  $Date: 2001/02/05 09:54:29 $

function [result,descriptor] = RM_histogram(x,descriptor)

if nargin < 1
   disp('Usage: RESULT = HISTOGRAM(X)')
   disp('       RESULT = HISTOGRAM(X,DESCRIPTOR)')
   disp('Where: DESCRIPTOR = [LOWER,UPPER,NCELL]')
   return
end

% Some initial tests on the input arguments

[NRowX, NColX] = size(x);

if NRowX ~= 1
  error('Invalid dimension of X');
end;

if nargin > 2
  error('Too many arguments');
end;

if nargin==1
  minx=min(x);
  maxx=max(x);
  delta=(maxx-minx)/(length(x)-1);
  ncell=ceil(sqrt(length(x)));
  descriptor=[minx-delta/2,maxx+delta/2,ncell];
end;

lower=descriptor(1);
upper=descriptor(2);
ncell=descriptor(3);

if ncell<1
  error('Invalid number of cells')
end;

if upper<=lower
  error('Invalid bounds')
end;

result(1:ncell)=0;

y=round( (x-lower)/(upper-lower)*ncell + 1/2 );
for n=1:NColX
  index=y(n);
  if index >= 1 & index<=ncell
    result(index)=result(index)+1;
  end;
end;


function ninv = norm_inv(x,mu,sigma)
%NORM_INV Inverse of the normal cumulative distribution function (cdf).
%   Y = NORM_INV(X,MU,SIGMA) Returns inverse of the normal cdf with
%   mean, MU, and standard deviation, SIGMA, at the values in X.
%
%   The size of X is the common size of the input arguments. A scalar input
%   functions as a constant matrix of the same size as the other inputs.
%
%   Default values for MU and SIGMA are 0 and 1 respectively.
%
% Copyright (c) 1995-1997, 2005-2007 Kurt Hornik

% This software is distributed under the GNU General Public
% License (version 3 or later); please refer to the file
% License.txt, included with the software, for details.

if nargin < 3
  sigma = 1;
end
if nargin < 2
  mu = 0;
end

if (~isscalar(mu) || ~isscalar(sigma))
  if ~((size(x,1) == size(mu,1)) && (size(mu,1) == size(sigma,1)))
    error ('norm_inv: x, mu and sigma must be of common size or scalars');
  end
end

[n,nin] = size (x);
ninv = zeros (n,nin);

if (isscalar (mu) && isscalar (sigma))
  if (find (isinf (mu) || isnan (mu) || (sigma < 0) || isinf(sigma)))
    ninv = NaN*ones(n,nin);
  else
    ninv =  mu+sigma.*sqrt(2)*erfinv(2*x-1);
  end
else
  k = find(isinf(mu) || isnan(mu) || (sigma < 0) || isinf(sigma));
  if (any(k))
    ninv(k) = NaN;
  end

  k = find(~isinf(mu) && ~isnan(mu) && (sigma > 0) && (sigma < Inf));
  if (any (k))
    ninv(k) = mu(k)+sigma(k).*sqrt(2)*erfinv(2*x(k)-1);
  end
end

k = find ((sigma == 0) && (x > 0) && (x < 1));
if (any(k))
  ninv(k) = mu(k);
end

ninv((sigma == 0) & (x == 0)) = -Inf;
ninv((sigma == 0) & (x == 1)) = Inf;

end


function [Hr,w,theta] = AmpResp(h,FreqPoint,FreqRange,ShiftFreq)

% AmpResp           Calculate Amplitude Response of Symmetric FIR Filter
%
% Arguments:
% h                 Symmetric impulse response:
%                     Type I linear-phase FIR filter, L is odd
%                     Type II lienar-phase FIR fitler, L is even
% FreqPoint         Range of amplitude response from 0 to 2pi or -pi to pi
% FreqRange         FreqRange = 1, frequency range is from 0 to 2pi
%                             = 2, frequency range -pi to pi
% ShiftFreq         Freq shift of amplitude response with specified amount
% Hr                Amplitude (zero-phase) response (column vectors)
%
% by Kong A. Lee, Feb 2005

% Check input argument values

if nargin <4; ShiftFreq = 0; end
if nargin <3; FreqRange = 1; end
if nargin <2; FreqPoint = 4096; end

h = h(:);                       % Make sure h is a column vector
L = length(h);                  % Length of filter

% Check selected frequency range

if FreqRange ==1
    w = (0:FreqPoint-1)*2*pi/FreqPoint;
elseif FreqRange ==2
    FreqPoint = FreqPoint/2;
    w = (-FreqPoint+1:1:FreqPoint-1)*pi/FreqPoint;
end

if mod(L,2)==1                  % L is an odd number

    alpha = (L-1)/2;
	a = [h(alpha + 1); 2*h(alpha:-1:1)];
	n = 0:alpha;
	Hr = cos((w + ShiftFreq)'*n)*a;

end

if mod(L,2)==0                  % L is an even number

    alpha = L/2;
    b = 2*h(alpha:-1:1);
    n = (1:alpha) - 0.5;
    Hr = cos((w + ShiftFreq)'*n)*b;

end

theta = -((L-1)/2)*w;


function isBipartite=graphIsBipartite(A)
%%GRAPHISBIPARTITE Given a nondirectional graph represented by a symmetric
%                  adjacency matrix A, determine whether the graph is
%                  bipartite.
%
%INPUTS: A An nXn symmetric adjacency matrix. If element i is adjacent to
%          element j, then A(i,j) is nonzero. Otherwise, it is zero.
%          Diagonal elements are typically zero (if not, then the graph is
%          not bipartite).
%
%OUTPUTS: isBipartite This is true if the graph is bipartite and false
%                     otherwise.
%
%This function implements Algorithm B in Chapter 7 of [1], modified for an
%adjacency matrix. When very large, space graphs are used, then the
%adjacency matrix representation is not ideal.
%
%EXAMPLE:
%We consider four graphs. Two are bipartite and two are not.
%A1 and A2 are:
%A1:     A2:
%1--2    1--2
% \/      \/
% /\      /\
%3--4    3--6
%         \/
%         5
%and graphs A3 and A4 are:
%A3:        A4:
%1-----2    1-----2
%|\   /|    |\   /|
%| 5-6 |    | 5-6 |
%| | | |    | |\| |
%| 7-8 |    | 7-8 |
%|/   \|    |/   \|
%3-----4    3-----4
% A1=[0,1,0,1;
%     1,0,1,0;
%     0,1,0,1;
%     1,0,1,0];
% A2=[0,1,0,1,0;
%     1,0,1,0,0;
%     0,1,0,1,1;
%     1,0,1,0,1;
%     0,0,1,1,0];
% A3=[0,1,1,0,1,0,0,0;
%     1,0,0,1,0,0,0,0;
%     1,0,0,1,0,0,0,0;
%     0,1,1,0,0,0,0,0;
%     1,0,0,0,0,1,1,0;
%     0,0,0,0,1,0,0,1;
%     0,0,0,0,1,0,0,1;
%     0,0,0,0,0,1,1,0];
% A4=[0,1,1,0,1,0,0,0;
%     1,0,0,1,0,0,0,0;
%     1,0,0,1,0,0,0,0;
%     0,1,1,0,0,0,0,0;
%     1,0,0,0,0,1,1,1;
%     0,0,0,0,1,0,0,1;
%     0,0,0,0,1,0,0,1;
%     0,0,0,0,1,1,1,0];
% isBi1=graphIsBipartite(A1)
% isBi2=graphIsBipartite(A2)
% isBi3=graphIsBipartite(A3)
% isBi4=graphIsBipartite(A4)
%One will find that A1 and A3 are bipartite and A2 and A4 are not.
%
%REFERENCES:
%[1] D. E. Knuth, The Art of Computer Programming. Vol. 4A: Combinatorial
%    Algorithms, Part I, Boston: Addison-Wesley, 2011.
%
%October 2018 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

n=size(A,1);

%Allocate space.
link=zeros(n,1);

%Step B1
color=-1*ones(n,1);%In [1], it is indexed from 0, not 1; we index from 1.
w=n+1;%Only need to consider the ones originating an edge.

isBipartite=true;
while(w~=1)
    w=w-1;

    %Step B3
    if(color(w)>=0)
        continue;
    end
    color(w)=0;
    link(w)=0;
    s=w;

    while(1)
        %Step B4
        u=s;
        s=link(s);

        aList=find(A(u,:));

        for a=1:length(aList)
            v=aList(a);

            if(color(v)<0)
                color(v)=1-color(u);
                link(v)=s;
                s=v;
            elseif(color(v)==color(u))
                isBipartite=false;
                return;
            end
        end
        if(s==0)
            break;
        end
    end
end

end

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.


function [R] = Euler2RotMat(eul)
% [R] = Euler2RotMat(theta_x, theta_y, theta_z)
%
% Stand-in for eul2rotm (built-in function MATLAB Robotics System Toolbox)
% Converts Euler angles [rad] to 3x3 Rotation Matrix
%
% This implementation based on:
% S.M.LaValle, Planning Algorithms, Cambridge University Press, 2006, p.98
% (PDF available here: http://planning.cs.uiuc.edu/ch3.pdf)
% as cited in: http://nghiaho.com/?page_id=846
%
% Inputs:
%   eul = [n x 3] - Euler Rotation Angles
%
% Outputs:
%   R = [3 x 3 x n] - rotation matrix.
%
% Written by Conrad McGreal @ 2019
R = zeros(3,3,size(eul,1)) ;

for i=1:size(eul,1)
    theta_x = eul(i,1) ;
    theta_y = eul(i,2) ;
    theta_z = eul(i,3) ;

    X = [1 0 0
        0 cos(theta_x) -sin(theta_x)
        0 sin(theta_x) cos(theta_x)];

    Y = [cos(theta_y) 0 sin(theta_y)
        0 1 0
        -sin(theta_y) 0 cos(theta_y)];

    Z = [cos(theta_z) -sin(theta_z) 0
        sin(theta_z) cos(theta_z) 0
        0 0 1];

    R(:,:,i) = Z*Y*X;
end


function y = msmoothboxlogpdf(x,a,b,sigma)
%MSMOOTHBOXLOGPDF Multivariate smooth-box log probability density function.
%   Y = MSMOOTHBOXLOGPDF(X,A,B,SIGMA) returns the logarithm of the pdf of
%   the multivariate smooth-box distribution with pivots A and B and scale
%   SIGMA, evaluated at the values in X. The multivariate smooth-box pdf is
%   the product of univariate smooth-box pdfs in each dimension.
%
%   For each dimension i, the univariate smooth-box pdf is defined as a
%   uniform distribution between pivots A(i), B(i) and Gaussian tails that
%   fall starting from p(A(i)) to the left (resp., p(B(i)) to the right)
%   with standard deviation SIGMA(i).
%
%   X can be a matrix, where each row is a separate point and each column
%   is a different dimension. Similarly, A, B, and SIGMA can also be
%   matrices of the same size as X.
%
%   The log pdf is typically preferred in numerical computations involving
%   probabilities, as it is more stable.
%
%   See also MSMOOTHBOXPDF, MSMOOTHBOXRND.

% Luigi Acerbi 2022

[N,D] = size(x);

if any(sigma(:) <= 0)
    error('msmoothboxpdf:NonPositiveSigma', ...
        'All elements of SIGMA should be positive.');
end

if D > 1
    if isscalar(a); a = a*ones(1,D); end
    if isscalar(b); b = b*ones(1,D); end
    if isscalar(sigma); sigma = sigma*ones(1,D); end
end

if size(a,2) ~= D || size(b,2) ~= D || size(sigma,2) ~= D
    error('msmoothboxpdf:SizeError', ...
        'A, B, SIGMA should be scalars or have the same number of columns as X.');
end

if size(a,1) == 1; a = repmat(a,[N,1]); end
if size(b,1) == 1; b = repmat(b,[N,1]); end
if size(sigma,1) == 1; sigma = repmat(sigma,[N,1]); end

if any(a(:) >= b(:))
    error('msmoothboxpdf:OrderError', ...
        'For all elements of A and B, the order A < B should hold.');
end

y = -inf(size(x));
lnf = log(1/sqrt(2*pi)./sigma) - log1p(1/sqrt(2*pi)./sigma.*(b - a));

for ii = 1:D
    idx = x(:,ii) < a(:,ii);
    y(idx,ii) = lnf(idx,ii) - 0.5*((x(idx,ii) - a(idx,ii))./sigma(idx,ii)).^2;

    idx = x(:,ii) >= a(:,ii) & x(:,ii) <= b(:,ii);
    y(idx,ii) = lnf(idx,ii);

    idx = x(:,ii) > b(:,ii);
    y(idx,ii) = lnf(idx,ii) - 0.5*((x(idx,ii) - b(idx,ii))./sigma(idx,ii)).^2;
end

y = sum(y,2);


function [J, grad] = lrCostFunction(theta, X, y, lambda)
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with
%regularization
%   J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
%   theta as the parameter for regularized logistic regression and the
%   gradient of the cost w.r.t. to the parameters.

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta
%
% Hint: The computation of the cost function and gradients can be
%       efficiently vectorized. For example, consider the computation
%
%           sigmoid(X * theta)
%
%       Each row of the resulting matrix will contain the value of the
%       prediction for that example. You can make use of this to vectorize
%       the cost function and gradient computations.
%
% Hint: When computing the gradient of the regularized cost function,
%       there're many possible vectorized solutions, but one solution
%       looks like:
%           grad = (unregularized gradient for logistic regression)
%           temp = theta;
%           temp(1) = 0;   % because we don't add anything for j = 0
%           grad = grad + YOUR_CODE_HERE (using the temp variable)
%

tempTheta = theta;
tempTheta(1) = 0;

J = (-1 / m) * sum(y.*log(sigmoid(X * theta)) + (1 - y).*log(1 - sigmoid(X * theta))) + (lambda / (2 * m))*sum(tempTheta.^2);
temp = sigmoid (X * theta);
error = temp - y;
grad = (1 / m) * (X' * error) + (lambda/m)*tempTheta;

% =============================================================

grad = grad(:);

end


function dist = distancePoints3d(p1, p2, varargin)
%DISTANCEPOINTS3D Compute euclidean distance between pairs of 3D Points
%
%   D = distancePoints3d(P1, P2) return distance between points P1 and
%   P2, given as [X Y Z].
%
%   If P1 and P2 are two arrays of points, result is a N1*N2 array
%   containing distance between each point of P1 and each point of P2.
%
%
%   D = distancePoints3d(P1, P2, NOR)
%   with NOR being 1, 2, or Inf, corresponfing to the norm used. Default is
%   2 (euclidean norm). 1 correspond to manhattan (or taxi driver) distance
%   and Inf to maximum difference in each coordinate.
%
%
%   See also:
%   points3d, minDistancePoints, distancePoints
%
%   ---------
%
%   author : David Legland
%   INRA - TPV URPOI - BIA IMASTE
%   created the 18/02/2005.
%

%   HISTORY
%   21/02/2005: add different norms
%   28/08/2007: deprecate

norm = 2;
if length(varargin)==1
    norm = varargin{1};
end

% compute difference of coordinate for each pair of points
ptsDiff = bsxfun(@minus, p2, p1);

% Return dist based on the type of measurement requested
switch(norm)
    case 1
        dist = sum(abs(ptsDiff),2);
    case 2
        dist = vectorNorm3d(ptsDiff);
    case Inf
        dist = max(abs(ptsDiff), [], 2);
    otherwise
        dist = power(sum(power(ptsDiff, norm),2), 1/norm);
end


% Mathematics Q2199546
% https://math.stackexchange.com/questions/2199546
% Minimizing the Sum of Quadratic Form with Equality Constraint
% References:
%   1.  aa
% Remarks:
%   1.  sa
% TODO:
% 	1.  ds
% Release Notes
% - 1.0.000     29/07/2017
%   *   First release.

%% General Parameters

run('InitScript.m');

figureIdx           = 0; %<! Continue from Question 1
figureCounterSpec   = '%04d';

generateFigures = OFF;

%% Simulation Parameters

numRows = 4;
numCols = 3; %<! Number of Vectors - i (K in the question)

numIterations   = 1000;
stepSize        = 0.05;

%% Generate Data

vE          = ones([numRows, 1]);
tA          = randn([numRows, numRows, numCols]);
mC          = randn([numRows, numCols]);
paramLambda = 1;

for ii = 1:numCols
    tA(:, :, ii) = tA(:, :, ii).' * tA(:, :, ii);
end

hProjEquality = @(mX) mX - ((sum(mX, 2) - vE) / numCols);

%% Solution by CVX

cvx_begin('quiet')
    cvx_precision('best');
    variable mXCvx(numRows, numCols)
    objVal = quad_form( mXCvx(:, 1), tA(:, :, 1) ) + (paramLambda * (mC(:, 1).' * mXCvx(:, 1)));
    for ii = 2:numCols
        objVal = objVal + quad_form( mXCvx(:, ii), tA(:, :, ii) ) + (paramLambda * (mC(:, ii).' * mXCvx(:, ii)));
    end
    minimize( objVal )
    subject to
        mXCvx * ones([numCols, 1]) == vE; %<! vE to be Eigen Vector for Eigen Value 1
cvx_end

disp([' ']);
disp(['CVX Solution Summary']);
disp(['The CVX Solver Status - ', cvx_status]);
disp(['The Optimal Value Is Given By - ', num2str(cvx_optval)]);
disp(['The Optimal Argument Is Given By - [ ', num2str(mXCvx(:).'), ' ]']);
disp([' ']);

%% Solution by Projected Gradient Descent

mX = zeros([numRows, numCols]);

for ii = 1:numIterations
    for jj = 1:numCols
        mX(:, jj) = mX(:, jj) - (stepSize * ((2 * tA(:, :, jj) * mX(:, jj)) + (paramLambda * mC(:, jj))));
    end
    mX = hProjEquality(mX);
end

objVal = 0;
for ii = 1:numCols
    objVal = objVal + (mX(:, ii).' * tA(:, :, ii) * mX(:, ii)) + (paramLambda * mC(:, ii).' * mX(:, ii));
end

disp([' ']);
disp(['Projected Gradient Solution Summary']);
disp(['The Optimal Value Is Given By - ', num2str(objVal)]);
disp(['The Optimal Argument Is Given By - [ ', num2str(mX(:).'), ' ]']);
disp([' ']);

%% Restore Defaults

% set(0, 'DefaultFigureWindowStyle', 'normal');
% set(0, 'DefaultAxesLooseInset', defaultLoosInset);


function  x = SoftTh(y,thld)
%    x = SoftTh(y,thld);
%
%    SOFTTH soft thresholds the input signal y with the threshold value
%    thld.
%
%    Input:
%       y    : 1D or 2D signal to be thresholded
%       thld : Threshold value
%
%    Output:
%       x : Soft thresholded output (x = sign(y)(|y|-thld)_+)
%
%  HERE'S AN EASY WAY TO RUN THE EXAMPLES:
%  Cut-and-paste the example you want to run to a new file
%  called ex.m, for example. Delete out the % at the beginning
%  of each line in ex.m (Can use search-and-replace in your editor
%  to replace it with a space). Type 'ex' in matlab and hit return.
%
%
%    Example:
%       y = makesig('Doppler',8);
%       thld = 0.2;
%       x = SoftTh(y,thld)
%       x = 0 0 0 -0.0703 0 0.2001 0.0483 0
%
%    See also: HardTh
%
%    Reference:
%       "De-noising via Soft-Thresholding" Tech. Rept. Statistics,
%       Stanford, 1992. D.L. Donoho.
%

%File Name: SoftTh.m
%Last Modification Date: 8/15/95	17:49:48
%Current Version: SoftTh.m	2.4
%File Creation Date: Mon Mar  7 10:38:45 1994
%Author: Haitao Guo  <harry@jazz.rice.edu>
%
%Copyright (c) 2000 RICE UNIVERSITY. All rights reserved.
%Created by Haitao Guo, Department of ECE, Rice University.
%
%This software is distributed and licensed to you on a non-exclusive
%basis, free-of-charge. Redistribution and use in source and binary forms,
%with or without modification, are permitted provided that the following
%conditions are met:
%
%1. Redistribution of source code must retain the above copyright notice,
%   this list of conditions and the following disclaimer.
%2. Redistribution in binary form must reproduce the above copyright notice,
%   this list of conditions and the following disclaimer in the
%   documentation and/or other materials provided with the distribution.
%3. All advertising materials mentioning features or use of this software
%   must display the following acknowledgment: This product includes
%   software developed by Rice University, Houston, Texas and its contributors.
%4. Neither the name of the University nor the names of its contributors
%   may be used to endorse or promote products derived from this software
%   without specific prior written permission.
%
%THIS SOFTWARE IS PROVIDED BY WILLIAM MARSH RICE UNIVERSITY, HOUSTON, TEXAS,
%AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
%BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
%FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RICE UNIVERSITY
%OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
%EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
%PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
%OR BUSINESS INTERRUPTIONS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
%WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
%OTHERWISE), PRODUCT LIABILITY, OR OTHERWISE ARISING IN ANY WAY OUT OF THE
%USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%
%For information on commercial licenses, contact Rice University's Office of
%Technology Transfer at techtran@rice.edu or (713) 348-6173
%
%Change History:
%

x = abs(y);
x = sign(y).*(x >= thld).*(x - thld);


function centroid = polygon_centroid_2 ( n, v )

%*****************************************************************************80
%
%% POLYGON_CENTROID_2 computes the centroid of a polygon.
%
%  Discussion:
%
%    The centroid is the area-weighted sum of the centroids of
%    disjoint triangles that make up the polygon.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    17 October 2005
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Adrian Bowyer and John Woodwark,
%    A Programmer's Geometry,
%    Butterworths, 1983.
%
%  Parameters:
%
%    Input, integer N, the number of vertices of the polygon.
%
%    Input, real V(2,N), the coordinates of the vertices.
%
%    Output, real CENTROID(2,1), the coordinates of the centroid.
%
  area = 0.0;
  centroid(1:2,1) = 0.0;

  for i = 1 : n - 2

    area_triangle = triangle_area ( ...
      v(1,i),   v(2,i), ...
      v(1,i+1), v(2,i+1), ...
      v(1,n),   v(2,n) );

    area = area + area_triangle;

    centroid(1:2,1) = centroid(1:2,1) + area_triangle ...
      * ( v(1:2,i) + v(1:2,i+1) + v(1:2,n) ) / 3.0;

  end

  if ( area == 0.0 )
    centroid(1:2,1) = v(1:2,1);
  else
    centroid(1:2,1) = centroid(1:2,1) / area;
  end

  return
end


function [ p, seed ] = triangle_sample ( t, n, seed )

%*****************************************************************************80
%
%% TRIANGLE_SAMPLE returns random points in a triangle.
%
%  Discussion:
%
%    In order to compare the results of this program with the
%    C++ and FORTRAN90 versions, it is useful to use R8VEC_UNIFORM_01
%    to generate the random numbers.  However, it is much faster
%    to rely on MATLAB's RAND routine to do this.  So if speed
%    is a consideration, replace the calls to R8VEC_UNIFORM_01
%    as indicated.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    10 April 2007
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real T(2,3), the triangle vertices.
%
%    Input, integer N, the number of points to generate.
%
%    Input, integer SEED, a seed for the random number generator.
%    Only needed if R8VEC_UNIFORM_01 is used.
%
%    Output, real P(2,N), random points in the triangle.
%
%    Output, integer SEED, a seed for the random number generator.
%
  dim_num = 2;
%
%  For fast execution, call RAND:
%
% alpha = rand ( 1, n );
%
%  For comparison with  F90 and C++, call R8VEC_UNIFORM:
%
  [ alpha, seed ] = r8vec_uniform_01 ( n, seed );
%
%  Interpret R as a percentage of the triangle's area.
%
%  Imagine a line L, parallel to side 1, so that the area between
%  vertex 1 and line L is R percent of the full triangle's area.
%
%  The line L will intersect sides 2 and 3 at a fraction
%  ALPHA = SQRT ( R ) of the distance from vertex 1 to vertices 2 and 3.
%
  alpha(1:n) = sqrt ( alpha(1:n) );
%
%  Determine the coordinates of the points on sides 2 and 3 intersected
%  by line L.
%
  for dim = 1 : dim_num
    p12(dim,1:n) = ( 1.0 - alpha(1:n) ) * t(dim,1) ...
                         + alpha(1:n)   * t(dim,2);

    p13(dim,1:n) = ( 1.0 - alpha(1:n) ) * t(dim,1) ...
                         + alpha(1:n)   * t(dim,3);
  end
%
%  Now choose, uniformly at random, a point on the line L.
%
%  For fast execution, call RAND:
%
% alpha = rand ( 1, n );
%
%  For comparison with  F90 and C++, call R8VEC_UNIFORM:
%
  [ alpha, seed ] = r8vec_uniform_01 ( n, seed );

  for dim = 1 : dim_num
    p(dim,1:n) = ( 1.0 - alpha(1:n) ) .* p12(dim,1:n) ...
                       + alpha(1:n)   .* p13(dim,1:n);
  end

 return
end


function [ n_data, x, fx ] = shi_values ( n_data )

%*****************************************************************************80
%
%% SHI_VALUES returns some values of the hyperbolic sine integral function.
%
%  Discussion:
%
%    SHI(X) = integral ( 0 <= T <= X ) sinh ( T ) / T dt
%
%    In Mathematica, the function can be evaluated by:
%
%      SinhIntegral[x]
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    11 June 2007
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Milton Abramowitz, Irene Stegun,
%    Handbook of Mathematical Functions,
%    National Bureau of Standards, 1964,
%    ISBN: 0-486-61272-4,
%    LC: QA47.A34.
%
%    Stephen Wolfram,
%    The Mathematica Book,
%    Fourth Edition,
%    Cambridge University Press, 1999,
%    ISBN: 0-521-64314-7,
%    LC: QA76.95.W65.
%
%  Parameters:
%
%    Input/output, integer N_DATA.  The user sets N_DATA to 0 before the
%    first call.  On each call, the routine increments N_DATA by 1, and
%    returns the corresponding data; when there is no more data, the
%    output value of N_DATA will be 0 again.
%
%    Output, real X, the argument of the function.
%
%    Output, real FX, the value of the function.
%
  n_max = 16;

  fx_vec = [ ...
    0.5069967498196672, ...
    0.6121303965633808, ...
    0.7193380189288998, ...
    0.8289965633789345, ...
    0.9414978265114335, ...
    1.057250875375729, ...
    1.300250361022057, ...
    1.561713388361002, ...
    1.845814141358504, ...
    2.157290343425901, ...
    2.501567433354976, ...
    3.549340406224435, ...
    4.973440475859807, ...
    6.966162067504942, ...
    9.817326911233034, ...
    13.96788504934715  ];

  x_vec = [ ...
      0.5E+00, ...
      0.6E+00, ...
      0.7E+00, ...
      0.8E+00, ...
      0.9E+00, ...
      1.0E+00, ...
      1.2E+00, ...
      1.4E+00, ...
      1.6E+00, ...
      1.8E+00, ...
      2.0E+00, ...
      2.5E+00, ...
      3.0E+00, ...
      3.5E+00, ...
      4.0E+00, ...
      4.5E+00 ];

  if ( n_data < 0 )
    n_data = 0;
  end

  n_data = n_data + 1;

  if ( n_max < n_data )
    n_data = 0;
    x = 0.0;
    fx = 0.0;
  else
    x = x_vec(n_data);
    fx = fx_vec(n_data);
  end

  return
end


% plot real and imaginary parts of various finite difference derivative operators
% input: c = scalar multiplier
% input: M = number of points on interval (excluding one end point)
% input: dx = uniform grid point separation

function plotmultiplies(c, M, dx)

m=0:(M-1);

thetam = 2*pi*m/M;

deltaR = c*(exp(i*thetam)-1)/dx;
deltaL = c*(1-exp(-i*thetam))/dx;
deltaC = i*c*sin(thetam)/dx;
deltaC4 = i*c*(8*sin(thetam)-sin(2*thetam))/(6*dx);
deltaC6 = i*c*sin(thetam).*(1+(2/3)*sin(thetam/2).^2 +(8/15)*sin(thetam/2).^4)/dx;

% plot real part of eigenvalues
subplot(1,2,1);
plot(thetam, real(deltaR), 'r-d');
hold on;
plot(thetam, real(deltaL), 'b-*');
plot(thetam, real(deltaC), 'g-s');
plot(thetam, real(deltaC4), 'k-o');
hold off;
xlabel('\theta_m = 2\pim/M', 'FontSize', 18);
title('Real part of ODE multipliers', 'FontSize', 18);
legend('\delta_+', '\delta_-', '\delta_0', '\delta_0(1-(1/6)dx^2\delta^2)');

% plot imaginary part of eigenvalues
subplot(1,2,2);
plot(thetam, imag(deltaR), 'r-d');
hold on;
plot(thetam, imag(deltaL), 'b-*');
plot(thetam, imag(deltaC), 'g-s');
plot(thetam, imag(deltaC4), 'k-o');
plot(thetam, imag(deltaC6), 'm-h');
plot([0 pi], [0 pi], 'k-', 'LineWidth', 2);      % first part of correct phase relationship
plot([pi 2*pi], [-pi 0], 'k-', 'LineWidth', 2); % second part

hold off;
xlabel('\theta_m = 2\pim/M', 'FontSize', 18);
title('Imaginary part of ODE multipliers', 'FontSize', 18);
legend('\delta_+', '\delta_-', '\delta_0', '\delta_0(1-(1/6)dx^2\delta^2)', '6th order central difference', 'Exact');


function val=tanDerivVal(z,n)
%%TANDERIVVAL Return the value of the nth derivative of tan(x) with respect
%             to x evaluated at x=z.
%
%INPUTS: z A matrix of real or complex values at which the nth derivative
%          of the tangent function is desired.
%        n The number of derivatives to take. n>=0.
%
%OUTPUTS: val The value of the nth derivative of the tangent function taken
%             at all of the points in z. val has the same dimensions as z.
%
%This function implements the algorithm of [1].
%
%REFERENCES:
%[1] M. E. Hoffman, "Derivative polynomials for tangent and secant," The
%    American Mathematical Monthly, vol. 102, no. 1, pp. 23-30, Jan. 1995.
%
%October 2016 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

numEls=numel(z);

val=zeros(size(z));
for curEl=1:numEls
    x=z(curEl);
    u=tan(x);

    P=zeros(n+1,1);
    P(1)=u;

    %The loop implements Equation 5.
    for k=1:n
        sumVal=0;
        for i=0:(k-1)
            sumVal=sumVal+binomial(k-1,i)*P(i+1)*P(k-1-i+1);
        end
        if(k==1)
            sumVal=sumVal+1;
        end
        P(k+1)=sumVal;
    end
    val(curEl)=P(end);
end
end

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.


% Spherical Harmonics basis of order lmax.
% NB: the definition now matches the one in DIPY
function Ylm = AMICO_CreateYlm( lmax, colatitude, longitude )

Ylm = zeros( size(longitude,1), (lmax+2)*(lmax+1)/2 );
for l = 0:2:lmax
    Pm = legendre(l,cos(colatitude'))';
    lconstant = sqrt((2*l + 1)/(4*pi));

    center = (l+1)*(l+2)/2 - l;
    Ylm(:,center) = lconstant*Pm(:,1);
    for m=1:l
        precoeff = lconstant * sqrt(factorial(l - m)/factorial(l + m));

		Ylm(:, center - m) = sqrt(2)*precoeff*Pm(:,m+1).*cos(m*longitude);
		Ylm(:, center + m) = sqrt(2)*precoeff*Pm(:,m+1).*sin(m*longitude);
	end
end


function [p,d]=berk2prob(b)
%BERK2PROB convert Berksons to probability
%
%  Inputs:  B(M,N)       matrix containing Berkson values
%
% Outputs:  P(M,N)       Corresponding probability values
%           D(M,N)       Corresponding derivatives dP/dB
%
% Berksons, or log-odds, are a nonlinear scale for measuring
% probability defined by B = log2(P./(1-P)).
% When Berksons are used to measure probability, a logistic
% psychometric function becomes linear.
%
% The inverse function is berk2prob()

%      Copyright (C) Mike Brookes 2014
%      Version: $Id: berk2prob.m 4501 2014-04-24 06:28:21Z dmb $
%
%   VOICEBOX is a MATLAB toolbox for speech processing.
%   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   This program is free software; you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation; either version 2 of the License, or
%   (at your option) any later version.
%
%   This program is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You can obtain a copy of the GNU General Public License from
%   http://www.gnu.org/copyleft/gpl.html or by writing to
%   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
p=1-1./(1+pow2(b));
if nargout>1
    d=log(2)*p.*(1-p);
end


function dx = not2dx ( x1, y1, x2, y2, x3, y3, x4, y4 )

%*****************************************************************************80
%
%% NOT2DX evaluates a factor for serendipity basis functions.
%
%  Discussion:
%
%    not2(x1,y1,x2,y2,x3,y3,x4,y4) evaluates at the point (x1,y1), the basis
%    factor that is 0 at (x2,y2) and (x3,y3) and 1 at (x4,y4):
%
%          ( ( x1 - x2 ) * ( y3 - y2 ) - ( x3 - x2 ) * ( y1 - y2 ) )
%        / ( ( x4 - x2 ) * ( y3 - y2 ) - ( x3 - x2 ) * ( y4 - y2 ) )
%
%    not2dx returns the derivative of this function with respect to X1.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    28 June 2014
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real X1, Y2, the evaluation point.
%
%    Input, real X2, Y2, X3,Y3, values that define the factor.
%
%    Output, real DX, the derivative of the basis function factor
%    with respect to X1.
%
  dx = (   1.0       * ( y3 - y2 ) +                 0.0       ) ...
     / ( ( x4 - x2 ) * ( y3 - y2 ) - ( x3 - x2 ) * ( y4 - y2 ) );

  return
end


function E = ECGModelError(X,ECGmn,Phasemn,flag)
% E = ECGModelError(X,ECGmn,Phasemn,flag)
% Overview: Generate Synthetic ECG beat with parameters given in the variable X. Generate the artificial ECG
% over phase given in Phasemn. Compute the ECGModelError as the difference between the synthetic
% ECG beat and ECG beat stored in ECGmn.
%
% Inputs:
%           X - contains the amplitude (alphai), standard deviation (bi) and phase (tetai) for
%           each Gaussian function used for generating the synthetic ECG beat.
%
%           ECGmn - Average ECG beat estimated from the ECG analysis
%           window.
%
%           Phasemn - array contains the phase points over which the synthetic
%           ECG beat is generated.
%
%           flag - set to 1 to return generated sythetic ECG beat. Set to 0 to
%           return the difference between generated synthetic ECG beat and average ECG beat for analysis window.
%
% Outputs:
%           E - synthetic ECG beat generated using Gaussian parameter estimates if flag set to 1.
%               difference between synthetic ECG beat and average ECG beat for analysis window if flag set to 0.
%
% Open Source ECG Toolbox, version 1.0, November 2006
% Released under the GNU General Public License
% Copyright (C) 2006  Reza Sameni
% Sharif University of Technology, Tehran, Iran -- LIS-INPG, Grenoble, France
% reza.sameni@gmail.com
% last modified:
% on 11/30/2020 by Ismail Sadiq
%
% This program is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation; either version 2 of the License, or (at your
% option) any later version.
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
% Public License for more details. You should have received a copy of the
% GNU General Public License along with this program; if not, write to the
% Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
% MA  02110-1301, USA.

L = (length(X)/3);

alphai = X(1:L);
bi = X(L+1:2*L);
tetai = X(2*L+1:3*L);

Z = zeros(size(ECGmn));
for j = 1:length(alphai),
    dtetai = rem(Phasemn - tetai(j) + pi,2*pi)-pi;
    Z = Z + alphai(j) .* exp(-dtetai .^2 ./ (2*bi(j) .^ 2));
end

if(flag==0)
    E = (Z-ECGmn);
elseif(flag==1)
    E = Z;
end


%-----------------------------------------
% UNIVERSITY OF TENAGA NASIONAL
% PROGRAMMER: MOHD ANUAR ABD AZIZ
% STUDENT ID: ME071642
% SUBJECT   : COMPUTATIONAL FLUID DYNAMICS
% ASSIGNMENT 1
% MATLAB 6.5
%16 Feb 2005
% ------------------------------------------
clc
clear all
N=200;
L=2;
dx=L/N;
n2=25;
tb=400+273.15;
ta=34+273.15;
omega=1.78;
eps=1e-6; %minimum error being set

global N omega eps L dx n2 tb ta
warning off
%-----------------------------------------
% GET NUMBER OF ITERATION FOR EVERY METHOD
% ------------------------------------------
[iter,T]=JACOBBI;
fprintf('Jacobbi method\t\t: %d iteration \n',iter);
[iter,T]=GS;
fprintf('Gauss-Seidel method\t: %d iteration \n',iter);
[iter,T]=SOR;
fprintf('SOR method\t\t\t: %d iteration \n',iter);
%----------------------------------------------------
% GENERATE ACTUAL/THEORETICAL TEMPERATURE DISTRIBUTION
% ----------------------------------------------------
x=mesh;
for i=1:N+2
    A=cosh(sqrt(n2)*(L-x(i)))/cosh(sqrt(n2)*L);
    T(i)=(tb-ta)*A+ta;
end
Ta=T(101);
plot(x,T,'b')
hold
%-----------------------------------------
% GET RESULT AND ERROR FOR EVERY NUMBER OF
% MESH AND PLOT THE GRAPH
% ------------------------------------------
N=25;
x=mesh;
[iter,T]=SOR;
plot(x,T,'g')
[y,i]=min(abs(ones(size(x))-x));
err(1)=abs(T(i)-ta)*100/ta;
errm(1)=N;

N=50;
x=mesh;
[iter,T]=SOR;
plot(x,T,'r')
[y,i]=min(abs(ones(size(x))-x));
err(2)=abs(T(i)-ta)*100/ta;
errm(2)=N;

N=75;
x=mesh;
[iter,T]=SOR;
plot(x,T,'c')
[y,i]=min(abs(ones(size(x))-x));
err(3)=abs(T(i)-ta)*100/ta;
errm(3)=N;

N=100;
x=mesh;
[iter,T]=SOR;
plot(x,T,'m')
[y,i]=min(abs(ones(size(x))-x));
err(4)=abs(T(i)-ta)*100/ta;
errm(4)=N;

N=200;
x=mesh;
[iter,T]=SOR;
plot(x,T,'y')
[y,i]=min(abs(ones(size(x))-x));
err(5)=abs(T(i)-ta)*100/ta;
errm(5)=N;

legend('Actual', '25 mesh','50 mesh','75 mesh','100 mesh','200 mesh');
hold
xlabel('Distance, X, in meter');
ylabel('Temperature, teta, in Kelvin');
title('Temperature across fin from the base x=0');
grid
figure
plot(errm,err)
xlabel('Number of mesh, N');
ylabel('% difference/error');
title('error vs number of mesh at x= 1.0 m');
grid
fprintf('\n\nNote:-\n\tOptimum omega value is %g\n\tObtained from mfile optimum.m for N=200 mesh',omega);


function vn = normalizeVector(v)
%NORMALIZEVECTOR Normalize a vector to have norm equal to 1
%
%   V2 = normalizeVector(V);
%   Returns the normalization of vector V, such that ||V|| = 1. V can be
%   either a row or a column vector.
%
%   When V is a MxN array, normalization is performed for each row of the
%   array.
%
%   Example:
%   vn = normalizeVector([3 4])
%   vn =
%       0.6000   0.8000
%   vectorNorm(vn)
%   ans =
%       1
%
%   See Also:
%   vectors2d, vectorNorm
%
%
%   ---------
%
%   author : David Legland
%   INRA - TPV URPOI - BIA IMASTE
%   created the 29/11/2004.
%

%   HISTORY
%   2005-01-14 correct bug
%   2009-05-22 rename as normalizeVector
%   2011-01-20 use bsxfun

dim = size(v);

if dim(1)==1 || dim(2)==1
    % in case of one vector, the norm is a scalar
    vn = v / sqrt(sum(v.^2));
else
    % for several vectors, need to adapt size of norm
    vn = bsxfun(@rdivide, v, sqrt(sum(v.^2, 2)));
    %same as: vn = v./repmat(sqrt(sum(v.*v, 2)), [1 dim(2)]);
end


function MDDot=rotMatSecondDeriv2D(theta,thetaDot,thetaDDot,isClockwise)
%%ROTMATSECONDDERIV2D Given an initial angle of rotation, and the first and
%   second derivatives of the angle with respect to time, find the
%   derivative of the 2D rotation matrix. This is the second time
%   derivative of rotMat2D assuming that the rotation angle changes as a
%   function of time.
%
%INPUTS: theta The initial rotation angle radians about which the
%              time-varying 2D rotation matrix would rotate a 2X1 vector at
%              the current time.
%     thetaDot The derivative of theta with respect to time.
%    thetaDDot The second derivative of theta with respect to time.
%  isClockwise An optional boolean parameter indication whether the
%              rotation implied by theta is clockwise. The default if
%              omitted or an empty matrix is passed is false.
%
%OUTPUTS: MDDot The 2X2 second derivative of the rotation matrix (as one
%               could obtain using rotMat2D) with respect to time.
%
%EXAMPLE:
%This example shows that this function is consistent with numeric
%differentiation. The relative error of the finite differenced solution
%typically implies for 6-8 digits of precision.
% theta=2*pi*rand(1);
% thetaDot=randn(1);
% thetaDDot=randn(1);
% thetaState=[theta;thetaDot;thetaDDot];
% isClockwise=false;
% MDDot=rotMatSecondDeriv2D(theta,thetaDot,thetaDDot,isClockwise);
%
% MDot=rotMatDeriv2D(theta,thetaDot,isClockwise);
% %Propagate deltaT into the future for finite differencing. This is just a
% %second order linear model applied to the thetaState.
% deltaT=1e-8;
% order=2;
% F=FPolyKal(deltaT,3,order);
% thetaStateDelta=F*thetaState;
% MDotDelta=rotMatDeriv2D(thetaStateDelta(1),thetaStateDelta(2),isClockwise);
% MDDotNumDiff=(MDotDelta-MDot)/deltaT;
% RelErr=max(abs((MDDotNumDiff(:)-MDDot(:))./MDDot(:)))
%
%December 2022 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(nargin<4||isempty(isClockwise))
    isClockwise=false;
end

sinTheta=sin(theta);
cosTheta=cos(theta);

if(isClockwise)
    MDDot=[-cosTheta*thetaDot^2-sinTheta*thetaDDot,-sinTheta*thetaDot^2+cosTheta*thetaDDot;
            sinTheta*thetaDot^2-cosTheta*thetaDDot,-cosTheta*thetaDot^2-sinTheta*thetaDDot];
else
    MDDot=[-cosTheta*thetaDot^2-sinTheta*thetaDDot, sinTheta*thetaDot^2-cosTheta*thetaDDot;
           -sinTheta*thetaDot^2+cosTheta*thetaDDot,-cosTheta*thetaDot^2-sinTheta*thetaDDot];
end
end

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.


function g = givens2(a,b)
%GIVENS2 find a Givens rotation.
% Example:
%   g = givens2(a,b)
% See also: cs_demo

%   Copyright 2006-2007, Timothy A. Davis.
%   http://www.cise.ufl.edu/research/sparse

if (b == 0)
    c = 1 ; s = 0 ;
elseif (abs (b) > abs (a))
    tau = -a/b ; s = 1 / sqrt (1+tau^2) ; c = s*tau ;
else
    tau = -b/a ; c = 1 / sqrt (1+tau^2) ; s = c*tau ;
end
g = [c -s ; s c] ;


% Sparse covariance estimation for Gaussian variables
% Joëlle Skaf - 04/24/08
% (a figure is generated)
%
% Suppose y \in\reals^n is a Gaussian random variable with zero mean and
% covariance matrix R = \Expect(yy^T), with sparse inverse S = R^{-1}
% (S_ij = 0 means that y_i and y_j are conditionally independent).
% We want to estimate the covariance matrix R based on N independent
% samples y1,...,yN drawn from the distribution, and using prior knowledge
% that S is sparse
% A good heuristic for estimating R is to solve the problem
%           maximize    logdet(S) - tr(SY) - lambda*sum(sum(abs(S)))
%           subject to  S >= 0
% where Y is the sample covariance of y1,...,yN, and lambda is a sparsity
% parameter to be chosen or tuned.
% A figure showing the sparsity (number of nonzeros) of S versus lambda
% is generated.

% Input data
randn('state',0);
n = 10;
N = 100;
Strue = sprandsym(n,0.5,0.01,1);
nnz_true = sum(Strue(:)>1e-4);
R = inv(full(Strue));
y_sample = sqrtm(R)*randn(n,N);
Y = cov(y_sample');
Nlambda = 20;
lambda = logspace(-2, 3, Nlambda);
nnz = zeros(1,Nlambda);

for i=1:Nlambda
    disp(['i = ' num2str(i) ', lambda(i) = ' num2str(lambda(i))]);
    % Maximum likelihood estimate of R^{-1}
    cvx_begin sdp quiet
        variable S(n,n) symmetric
        maximize log_det(S) - trace(S*Y) - lambda(i)*sum(sum(abs(S)))
        S >= 0
    cvx_end
    nnz(i) = sum(S(:)>1e-4);
end

figure;
semilogx(lambda, nnz);
hold on;
semilogx(lambda, nnz_true*ones(1,Nlambda),'r');
xlabel('\lambda');
legend('nonzeros in S', 'nonzeros in R^{-1}');


function p = circle_imp_points_2d ( r, center, n )

%*****************************************************************************80
%
%% CIRCLE_IMP_POINTS_2D returns N equally spaced points on an implicit circle in 2D.
%
%  Discussion:
%
%    The first point is always ( CENTER(1) + R, CENTER(2) ), and subsequent
%    points proceed counterclockwise around the circle.
%
%    An implicit circle in 2D satisfies the equation:
%
%      ( X - CENTER(1) )^2 + ( Y - CENTER(2) )^2 = R^2
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    04 December 2010
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real R, the radius of the circle.
%
%    Input, real CENTER(2,1), the center of the circle.
%
%    Input, integer N, the number of points desired.  N must be at least 1.
%
%    Output, real P(2,N), the coordinates of points
%    on the circle.
%
  for i = 1 : n
    theta = ( 2.0 * pi * ( i - 1 ) ) / n;
    p(1,i) = center(1,1) + r * cos ( theta );
    p(2,i) = center(2,1) + r * sin ( theta );
  end

  return
end