function [ yval, ypval, yppval ] = spline_cubic_val ( n, t, y, ypp, tval )

%*****************************************************************************80
%
%% SPLINE_CUBIC_VAL evaluates a piecewise cubic spline at a point.
%
%  Discussion:
%
%    SPLINE_CUBIC_SET must have already been called to define the
%    values of YPP.
%
%    For any point T in the interval T(IVAL), T(IVAL+1), the form of
%    the spline is
%
%      SPL(T) = A
%             + B * ( T - T(IVAL) )
%             + C * ( T - T(IVAL) )**2
%             + D * ( T - T(IVAL) )**3
%
%    Here:
%      A = Y(IVAL)
%      B = ( Y(IVAL+1) - Y(IVAL) ) / ( T(IVAL+1) - T(IVAL) )
%        - ( YPP(IVAL+1) + 2 * YPP(IVAL) ) * ( T(IVAL+1) - T(IVAL) ) / 6
%      C = YPP(IVAL) / 2
%      D = ( YPP(IVAL+1) - YPP(IVAL) ) / ( 6 * ( T(IVAL+1) - T(IVAL) ) )
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    13 February 2004
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Carl de Boor,
%    A Practical Guide to Splines,
%    Springer Verlag, 1978.
%
%  Parameters:
%
%    Input, integer N, the number of data values.
%
%    Input, real T(N), the knot values.
%
%    Input, real Y(N), the data values at the knots.
%
%    Input, real YPP(N), the second derivatives of the spline at the knots.
%
%    Input, real TVAL, a point, typically between T(1) and T(N), at
%    which the spline is to be evalulated.  If TVAL lies outside
%    this range, extrapolation is used.
%
%    Output, real YVAL, YPVAL, YPPVAL, the value of the spline, and
%    its first two derivatives at TVAL.
%

%
%  Determine the interval [T(LEFT), T(RIGHT)] that contains TVAL.
%  Values below T(1) or above T(N) use extrapolation.
%
  [ left, right ] = r8vec_bracket ( n, t, tval );
%
%  Evaluate the polynomial.
%
  dt = tval - t(left);
  h = t(right) - t(left);

  yval = y(left) ...
       + dt * ( ( y(right) - y(left) ) / h ...
              - ( ypp(right) / 6.0 + ypp(left) / 3.0 ) * h ...
       + dt * ( 0.5 * ypp(left) ...
       + dt * ( ( ypp(right) - ypp(left) ) / ( 6.0 * h ) ) ) );

  ypval = ( y(right) - y(left) ) / h ...
       - ( ypp(right) / 6.0 + ypp(left) / 3.0 ) * h ...
       + dt * ( ypp(left) ...
       + dt * ( 0.5 * ( ypp(right) - ypp(left) ) / h ) );

  yppval = ypp(left) + dt * ( ypp(right) - ypp(left) ) / h;

  return
end


function [R_ned_2_frame, ned_x, ned_y, ned_z] = computeNedFrameInFrame(rVect)
    %Source: https://en.wikipedia.org/wiki/North_east_down

    rNorm = norm(rVect);
    lambda = AngleZero2Pi(atan2(rVect(2),rVect(1)));
    phi = pi/2 - acos(rVect(3)/rNorm);

    RFrame2Ned = [-sin(phi)*cos(lambda), -sin(lambda), -cos(phi)*cos(lambda);
                 -sin(phi)*sin(lambda),  cos(lambda), -cos(phi)*sin(lambda);
                  cos(phi),              0,           -sin(phi)]';

    R_ned_2_frame = RFrame2Ned';
    ned_x = R_ned_2_frame(:,1);
    ned_y = R_ned_2_frame(:,2);
    ned_z = R_ned_2_frame(:,3);
end


function [v,beta]=HouseholderVec(x,forceSign)
%%HOUSEHOLDERVEC Given an mX1 column vector x, compute mX1 column
%          vector v and scalar beta such that P=eye(m,m)-beta*v*v' is
%          orthogonal and P*x=(+/-)norm(x)*e1 for real x, where e1 is a
%          vector with a 1 in the first entry and zeros elsewhere. If
%          forceSign is true, then it will be such that the + sign is
%          always chosen. For complex x,
%          P*x=(+/-)exp(1j*angle(x(1)))*norm(x)*e1, whereby the sign in
%          front can vary. The v vector is chosen such that v(1)=1. If a
%          scalar value is provided, then v=1, beta=0 are returned.
%
%INPUTS: x An mX1 vector, real or complex.
% forceSign If true and x is real, then P*x always equals +norm(x)*e1.
%          Otherwise, it can be + or -. Often, assumptions on recovering
%          matrices from factored form implicitly assume that
%          forceSign=false. The default if omitted or an empty matrix is
%          passed is false.
%
%OUTPUTS: v An mX1 vector such that P=eye(m)-beta*v*v' is an orthogonal
%           matrix and P*x having the only nonzero element be the first
%           one. If a scalar input is given, then v=0 is returned.
%      beta The real scalar beta as mentioned with v. beta= 2/(v'*v). If a
%           scalar input is given, then beta=0 is returned.
%
%For real vectors x, the Householder vector algorithm of Section 5.1.1 of
%[1] is used. For complex vectors, the Householder algorithm of Section
%5.1.13 is used.
%
%REFERENCES:
%[1] G. H. Golub and C. F. Van Loan, Matrix Computations, 4th ed.
%    Baltimore: Johns Hopkins University Press, 2013.
%
%November 2015 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(nargin<2||isempty(forceSign))
    forceSign=false;
end

m=length(x);

if(isempty(x))
   v=[];
   beta=[];
   return;
end

if(m==1)
   v=1;
   beta=0;
   return;
end

if(isreal(x))%The real case from Chapter 5.1.3 in [1].
    sigma=x(2:m)'*x(2:m);
    v=[1;x(2:m)];
    if(sigma==0&&x(1)>=0)
        if(forceSign)
            beta=0;
        else
            beta=2;
        end

        return;
    elseif(sigma==0&&x(1)<0)
        beta=2;%Sign corrected from that in the book.
        return;
    else
        mu=sqrt(x(1).*x(1)+sigma);
        %The book has <=; we are using < as it does not change things and
        %for some data types, such as Intervals, it is easier to overload <
        %than <=.
        if(x(1)<0)
            v(1)=x(1)-mu;
        else
            v(1)=-sigma/(x(1)+mu);
        end
        beta=2*v(1).*v(1)./(sigma+v(1).*v(1));
        v=v./v(1);
    end
else%The complex case from Chapter 5.1.13
    angVal=x(1)/abs(x(1));
    %Deal with NaNs due to x(1)=0.
    if(~isfinite(angVal))
        angVal=1;
    end

    x2Norm=norm(x,2);

    v=zeros(m,1);

    v(2:end)=x(2:end);
    v1=x(1)+angVal*x2Norm;
    v2=x(1)-angVal*x2Norm;

    %Of v1 and v2, choose the one with the largest norm.
    if(abs(v1)>abs(v2))
        v(1)=v1;
    else
        v(1)=v2;
    end

    v=v/v(1);
    beta=2/(v'*v);
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 ind=qilv(I,I2,Ws)

% QILV()
%==================================================================
% Quality Index based on Local Variance
% Santiago Aja-Fernandez
%
% santi@bwh.harhard.edu
% Accorging to
%
% S. Aja-Fernández, R. San José Estépar, C. Alberola-López and C.F. Westin,
% "Image quality assessment based on local variance", EMBC 2006,
% New York, Sept. 2006.
%
%------------------------------------------------------------------
%
% The function calculates a global compatibility measure
% between two images, based on their local variance distribution.
%
%------------------------------------------------------------------
%
% INPUT:   (1) I: The first image being compared
%          (2) I2: the second image being compared
%          (3) Ws: window for the estimation of statistics:
%		If Ws=0: default gaussian window
%               If Ws=[M N] MxN square window
%
%
% OUTPUT:
%          (1) ind: Quality index (between 0 and 1)
%
% Default usage:
%
%   ind=s_correct(I,I2,0);
%
%
%==================================================================

%the following variables can be added to avoid NaN, but
%usually it is not necessary

L=255;
K=[0.01 0.03];
C1 = (K(1)*L)^2;
C2 = (K(2)*L)^2;
%C1=0;
%C2=0;

%Window
if Ws==0
	window = fspecial('gaussian', 11, 1.5);
else
	window=ones(Ws);
end
window = window/sum(window(:));

%Local means
M1=filter2(window, I, 'valid') ;
M2=filter2(window, I2, 'valid') ;
%Local Variances
V1 = filter2(window, I.^ 2, 'valid') - M1.^ 2;
V2 = filter2(window, I2.^ 2, 'valid') - M2.^ 2;

%Global statistics:

m1=mean(V1(:));
m2=mean(V2(:));
s1=std(V1(:));
s2=std(V2(:));
s12=mean2((V1-m1).*(V2-m2));

%Index
ind1=((2*m1*m2+C1)./(m1.^2+m2.^ 2+C1));
ind2=(2*s1*s2+C2)./(s1.^ 2+s2.^ 2+C2);
ind3=(s12+C2/2)./(s1*s2+C2/2);
ind=ind1.*ind2.*ind3;


function rotMat = axisAngle2RotMat(axis,angle)
% Function to get the rotation matrix for a given axis and angle where x
% is a column vector, such that:
%    xNew = rotMat*x
%
% Alternate use where x is a row vector:
%    xNew = x*rotMat'
%
% Parameters
% ------------
%   axis : 1 x 3 float vector
%       Axis about which to rotate the point x
%   angle : float
%       Rotation angle (radian)
%
% Returns
% ------------
%   rotMat : 3 x 3 float vector
%       Rotation matrix from the input axis and angle
%

rotMat = zeros(3);
rotMat(1,1) = axis(1)*axis(1)*(1-cos(angle)) + cos(angle);
rotMat(1,2) = axis(2)*axis(1)*(1-cos(angle)) - axis(3)*sin(angle);
rotMat(1,3) = axis(3)*axis(1)*(1-cos(angle)) + axis(2)*sin(angle);
rotMat(2,1) = axis(1)*axis(2)*(1-cos(angle)) + axis(3)*sin(angle);
rotMat(2,2) = axis(2)*axis(2)*(1-cos(angle)) + cos(angle);
rotMat(2,3) = axis(3)*axis(2)*(1-cos(angle)) - axis(1)*sin(angle);
rotMat(3,1) = axis(1)*axis(3)*(1-cos(angle)) - axis(2)*sin(angle);
rotMat(3,2) = axis(2)*axis(3)*(1-cos(angle)) + axis(1)*sin(angle);
rotMat(3,3) = axis(3)*axis(3)*(1-cos(angle)) + cos(angle);

end


function [X, maxdot] = packing_on_the_sphere(d, n, epsilon, X0)
% Return a set of points spread out on the sphere.
%
% function [X, maxdot] = packing_on_the_sphere(d, n, epsilon, X0)
%
% Using optimization on the oblique manifold, that is, the product of
% spheres, this function returns a set of n points with unit norm in R^d in
% the form of a matrix X of size nxd, such that the points are spread out
% on the sphere. Ideally, we would minimize the maximum inner product
% between any two points X(i, :) and X(j, :), i~=j, but that is a nonsmooth
% cost function. Instead, we replace the max function by a classical
% log-sum-exp approximation and (attempt to) solve:
%
% min_{X in OB(d, n)} log( .5*sum_{i~=j} exp( xi'*xj/epsilon ) ),
%
% with xi = X(:, i) and epsilon is some "diffusion constant". As epsilon
% goes to zero, the cost function is a sharper approximation of the max
% function (under some assumptions), but the cost function becomes stiffer
% and hence harder to optimize.
%
% The second output, maxdot, is the maximum inner product between any two
% points in the returned X. This number is the one we truly are trying to
% minimize.
%
% Notice that this cost function is invariant under rotation of X:
% f(X) = f(XQ) for all orthogonal Q in O(d).
% This calls for optimization over the set of symmetric positive
% semidefinite matrices of size n and rank d with unit diagonal, which can
% be thought of as the quotient of the oblique manifold OB(d, n) by O(d):
% See elliptopefactory.
%
% This is known as the Thomson or, more specifically, the Tammes problem:
% http://en.wikipedia.org/wiki/Tammes_problem
% An interesting page by Neil Sloane collecting best known packings is
% available here http://neilsloane.com/packings/

% This file is part of Manopt and is copyrighted. See the license file.
%
% Main author: Nicolas Boumal, July 2, 2013
% Contributors:
%
% Change log:
%   Aug. 14, 2013 (NB) : Code now compatible to experiment with both the
%                        obliquefactory and the elliptopefactory.
%
%   Jan.  7, 2014 (NB) : Added reference to Neil Sloane's page and the
%                        maxdot output.
%
%   June 24, 2014 (NB) : Now shifting exponentials to alleviate numerical
%                        trouble when epsilon is too small.
%
%   Aug. 31, 2021 (XJ) : Added AD to compute the gradient

    if ~exist('d', 'var') || isempty(d)
        % Dimension of the embedding space: R^d
        d = 3;
    end
    if ~exist('n', 'var') || isempty(n)
        % Number n of points to place of the sphere in R^d.
        % For example, n=12 yields an icosahedron:
        % https://en.wikipedia.org/wiki/Icosahedron
        % Notice though that platonic solids are not always optimal.
        % Try for example n = 8: you don't get a cube.
        n = 24;
    end
    if ~exist('epsilon', 'var') || isempty(epsilon)
        % This value should be as close to 0 as affordable.
        % If it is too close to zero, optimization first becomes much
        % slower, than simply doesn't work anymore becomes of floating
        % point overflow errors (NaN's and Inf's start to appear).
        % If it is too large, then log-sum-exp is a poor approximation of
        % the max function, and the spread will be less uniform.
        % An okay value seems to be 0.01 or 0.001 for example. Note that a
        % better strategy than using a small epsilon straightaway is to
        % reduce epsilon bit by bit and to warm-start subsequent
        % optimization in that way. Trustregions will be more appropriate
        % for these fine tunings.
        epsilon = 0.0015;
    end

    % Pick your manifold (the elliptope factory quotients out the global
    % rotation invariance of the problem, which is more natural but
    % conceptually a bit more complicated --- for usage with the toolbox it
    % is the same though: just uncomment the appropriate line).
    manifold = obliquefactory(d, n, true);
    % manifold = elliptopefactory(n, d);

    % Generate a random initial guess if none was given.
    if ~exist('X0', 'var') || isempty(X0)
        X0 = manifold.rand();
    end

    % Define the cost function with caching system used: the store
    % structure we receive as input is tied to the input point X. Everytime
    % this cost function is called at this point X, we will receive the
    % same store structure back. We may modify the store structure inside
    % the function and return it: the changes will be remembered for next
    % time.
    function [f, store] = cost(X, store)
        if ~isfield(store, 'ready')
            XXt = X*X';
            % Shift the exponentials by the maximum value to reduce
            % numerical trouble due to possible overflows.
            s = max(max(triu(XXt, 1)));
            expXXt = exp((XXt-s)/epsilon);
            % Zero out the diagonal
            expXXt(1:(n+1):end) = 0;
            u = sum(sum(triu(expXXt, 1)));
            store.XXt = XXt;
            store.s = s;
            store.expXXt = expXXt;
            store.u = u;
            store.ready = true;
        end
        u = store.u;
        s = store.s;
        f = s + epsilon*log(u);
    end

    % Define the gradient of the cost. When the gradient is called at a
    % point X for which the cost was already called, the store structure we
    % receive remember everything that the cost function stored in it, so
    % we can reuse previously computed elements.
    function [g, store] = grad(X, store)
        if ~isfield(store, 'ready')
            [~, store] = cost(X, store);
        end
        % Compute the Euclidean gradient
        eg = store.expXXt*X / store.u;
        % Convert to the Riemannian gradient (by projection)
        g = manifold.egrad2rgrad(X, eg);
    end

    % Setup the problem structure with its manifold M and cost+grad
    % functions.
    problem.M = manifold;
    problem.cost = @cost;
    problem.grad = @grad;

    % An alternative way to compute the grad is to use automatic
    % differentiation provided in the deep learning toolbox (slower)
    % Notice that the function triu is not supported for AD so far.
    % Replace it with ctriu described in the file manoptADhelp.m
    % problem.cost = @cost_AD;
    %    function f = cost_AD(X)
    %        XXt = X*X';
    %        s = max(max(ctriu(XXt, 1)));
    %        expXXt = exp((XXt-s)/epsilon);
    %        expXXt(1:(n+1):end) = 0;
    %        u = sum(sum(ctriu(expXXt, 1)));
    %        f = s + epsilon*log(u);
    %    end
    % Call manoptAD to prepare AD for the problem structure
    % problem = manoptAD(problem,'egrad');

    % For debugging, it's always nice to check the gradient a few times.
    % checkgradient(problem);
    % pause;

    % Call a solver on our problem with a few options defined. We did not
    % specify the Hessian but it is still okay to call trustregion: Manopt
    % will approximate the Hessian with finite differences of the gradient.
    opts.tolgradnorm = 1e-8;
    opts.maxtime = 1200;
    opts.maxiter = 1e5;
    % X = trustregions(problem, X0, opts);
    X = conjugategradient(problem, X0, opts);

    % Evaluate the maximum inner product between any two points of X.
    XXt = X*X';
    dots = XXt(find(triu(ones(n), 1))); %#ok<FNDSB>
    maxdot = max(dots);

    % Similarly, even though we did not specify the Hessian, we may still
    % estimate its spectrum at the solution. It should reflect the
    % invariance of the cost function under a global rotatioon of the
    % sphere, which is an invariance under the group O(d) of dimension
    % d(d-1)/2 : this translates into d(d-1)/2 zero eigenvalues in the
    % spectrum of the Hessian.
    % The approximate Hessian is not a linear operator, and is it a
    % fortiori not symmetric. The result of this computation is thus not
    % reliable. It does display the zero eigenvalues as expected though.
    if manifold.dim() < 300
        evs = real(hessianspectrum(problem, X));
        figure;
        stem(1:length(evs), sort(evs), '.');
        title(['Eigenvalues of the approximate Hessian of the cost ' ...
               'function at the solution']);
    end

    % Show how the inner products X(:, i)'*X(:, j) are distributed.
    figure;
    histogram(real(acos(dots)), 20);
    title('Histogram of the geodesic distances');

    % This is the quantity we actually want to minimize.
    fprintf('Maximum inner product between two points: %g\n', maxdot);

    % Give some visualization if the dimension allows
    if d == 2
        % For the circle, the optimal solution consists in spreading the
        % points with angles uniformly sampled in (0, 2pi). This
        % corresponds to the following value for the max inner product:
        fprintf('Optimal value for the max inner product: %g\n', cos(2*pi/n));
        figure;
        t = linspace(-pi, pi, 201);
        plot(cos(t), sin(t), '-', 'LineWidth', 3, 'Color', [152,186,220]/255);
        daspect([1 1 1]);
        box off;
        axis off;
        hold on;
        plot(X(:, 1), X(:, 2), 'r.', 'MarkerSize', 25);
        hold off;
    end
    if d == 3
        figure;
        set(gcf, 'Color', 'w');
        % Plot the sphere
        [sphere_x, sphere_y, sphere_z] = sphere(50);
        handle = surf(sphere_x, sphere_y, sphere_z);
        set(handle, 'FaceColor', [152,186,220]/255);
        set(handle, 'FaceAlpha', .5);
        set(handle, 'EdgeColor', [152,186,220]/255);
        set(handle, 'EdgeAlpha', .5);
        daspect([1 1 1]);
        box off;
        axis off;
        hold on;
        % Add the chosen points
        Y = 1.02*X';
        plot3(Y(1, :), Y(2, :), Y(3, :), 'r.', 'MarkerSize', 25);
        % And connect the points which are at minimal distance,
        % within some tolerance.
        min_distance = real(acos(maxdot));
        connected = real(acos(XXt)) <= 1.20*min_distance;
        [Ic, Jc] = find(triu(connected, 1));
        for k = 1 : length(Ic)
            i = Ic(k); j = Jc(k);
            plot3(Y(1, [i j]), Y(2, [i j]), Y(3, [i j]), 'k-');
        end
        hold off;
    end

end


function [train_eval,test_eval]  = ml_kcv_regression_eval( X,y,g,train,test,k,train_eval,test_eval)
%ML_KCV_REGRESSION_EVAL
%
%   Evaluation of the Goodness of Fit of your regression model. Have look
%   at http://uk.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html
%   for a reference.
%
%  input ------------------------------------------------------------------
%
%       o   X        : (N x D),  input variables
%
%       o   y        : (N x 1),  output variables
%
%       o   g        : function handle, y = g(x)
%
%       o   train    : (M x 1),  set of indicies of X to use as train data.
%
%       o   test     : (P x 1),  set of indicies of X to use for testing.
%
%       o   k        : (1 x 1),  current iteration of k-fold
%

%   output ----------------------------------------------------------------
%
%       o   train_eval : struct
%
%       o   test_eval  : struct

%% Evaluate regression function on training data

hy = g(X(train(:),:));

gf = gfit2(y(train(:)),hy,'all');

train_eval.mse(k)   = gf(1);    %  '1'  - mean squared error (mse)
train_eval.nmse(k)  = gf(2);    %  '2'  - normalised mean squared error (nmse)
train_eval.rmse(k)  = gf(3);    %  '3'  - root mean squared error (rmse)
train_eval.nrmse(k) = gf(4);    %  '4'  - normalised root mean squared error (nrmse)
train_eval.mae(k)   = gf(5);    %  '5'  - mean absolute error (mae)
train_eval.mare(k)  = gf(6);    %  '6'  - mean  absolute relative error  (mare)
train_eval.r(k)     = gf(7);    %  '7'  - coefficient of correlation (r)
train_eval.d(k)     = gf(8);    %  '8'  - coefficient of determination (d)
train_eval.e(k)     = gf(9);    %  '9'  - coefficient of efficiency (e)
train_eval.me(k)    = gf(10);   %  '10' - maximum absolute error
train_eval.mre(k)   = gf(11);   %  '11' - maximum absolute relative error

%% Evaluate regression function on test data

hy = g(X(test(:),:));

gf = gfit2(y(test(:)),hy,'all');

test_eval.mse(k)   = gf(1);    %  '1'  - mean squared error (mse)
test_eval.nmse(k)  = gf(2);    %  '2'  - normalised mean squared error (nmse)
test_eval.rmse(k)  = gf(3);    %  '3'  - root mean squared error (rmse)
test_eval.nrmse(k) = gf(4);    %  '4'  - normalised root mean squared error (nrmse)
test_eval.mae(k)   = gf(5);    %  '5'  - mean absolute error (mae)
test_eval.mare(k)  = gf(6);    %  '6'  - mean  absolute relative error  (mare)
test_eval.r(k)     = gf(7);    %  '7'  - coefficient of correlation (r)
test_eval.d(k)     = gf(8);    %  '8'  - coefficient of determination (d)
test_eval.e(k)     = gf(9);    %  '9'  - coefficient of efficiency (e)
test_eval.me(k)    = gf(10);   %  '10' - maximum absolute error
test_eval.mre(k)   = gf(11);   %  '11' - maximum absolute relative error

end


function inside = circle_sector_contains_point_2d ( r, center, theta1, theta2, p )

%*****************************************************************************80
%
%% CIRCLE_SECTOR_CONTAINS_POINT_2D : is a point inside a circular sector?
%
%  Discussion:
%
%    A circular sector is formed by a circular arc, and the two straight line
%    segments that join its ends to the center of the circle.
%
%    A circular sector is defined by
%
%      ( X - CENTER(1) )**2 + ( Y - CENTER(2) )**2 = R**2
%
%    and
%
%      Theta1 <= Theta <= Theta2
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    08 January 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real R, the radius of the circle.
%
%    Input, real CENTER(2,1), the center of the circle.
%
%    Input, real THETA1, THETA2, the angles defining the arc,
%    in radians.  Normally, THETA1 < THETA2.
%
%    Input, real P(2,1), the point to be checked.
%
%    Output, logical INSIDE, is TRUE if the point is inside or on the
%    circular sector, FALSE otherwise.
%
  inside = 0;
%
%  Is the point inside the (full) circle?
%
  if ( ( p(1,1) - center(1,1) ) * ( p(1,1) - center(1,1) ) ...
     + ( p(2,1) - center(2,1) ) * ( p(2,1) - center(2,1) ) <= r * r )
%
%  Is the point's angle within the arc's range?
%  Try to force the angles to lie between 0 and 2 * PI.
%
    theta = r8_atan ( p(2,1) - center(2,1), p(1,1) - center(1,1) );

    if ( r8_modp ( theta  - theta1,  2.0 * pi ) <= ...
         r8_modp ( theta2 - theta1,  2.0 * pi ) )

      inside = 1;

    end

  end

  return
end


function out=proj_Lorentz(x)
%PROJ_LORENTZ computes the orthogonal projection onto the Lorentz cone {x:||x(1,..,n)||<=x(n+1)}
%
%  Usage:
%  out = PROJ_LORENTZ(x)
%  ===========================================
%  Input:
%  x - (n+1)-length vector to be projected
%  ===========================================
%  Output:
%  out - projection vector

% This file is part of the FOM package - a collection of first order methods for solving convex optimization problems
% Copyright (C) 2017 Amir and Nili Beck
%
% 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 3 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, see <http://www.gnu.org/licenses/>.

%reading the user
if (nargin < 1)
    error ('usage: proj_Lorentz(x)') ;
end
n = length(x)-1;
s = x(n+1) ;
x = x(1:n) ;

if (norm(x,s) >= abs(s))
    outx = (norm(x,2) + s)/(2*norm(x,2)) * x;
    outs = (norm(x,2) + s)/2 ;
else
    if (norm(x,2) <= s)
        outx = x ;
        outs = s ;
    else
        % s < norm(x,2) < -s
        outx = zeros(n,1) ;
        outs = 0 ;
    end
end

out = [outx; outs] ;

end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Bayesian Linear Regression 1D Example  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%          1) Generate 1D Regression Datasets                %%
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Generate some data
clear all;close all;clc;
nbSamples = 50;
X         = linspace(0,4,nbSamples);
w         = [0.5 25];
y         = X * w(1) + normrnd(0,0.2,1,nbSamples) + w(2);

options             = [];
options.points_size = 20;
options.title       = 'Training data';

if exist('h1','var') && isvalid(h1), delete(h1);end
h1      = ml_plot_data([X(:),y(:)],options);

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                 2) Train BLR Model                       %%
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Train Baysian Gaussian Linear regressin
var         = 0.2;        % variance in the noise of the data
Sigma_p     = eye(2,2);
Xb          = [X];
yb          = y;
[w,invA]    = train_blr(Xb',y',var,Sigma_p);

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                  3) Test BLR Model                       %%
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Prediction

x_test  = [linspace(0,4,100);ones(1,100)];
y_test  = x_test' * w;
Sigma   = x_test' * invA * x_test;

if exist('h2','var') && isvalid(h2), delete(h2);end
if exist('h3','var') && isvalid(h3), delete(h3);end

h2 = figure;

subplot(1,2,1);
box on;
plot_gaussian_contour(gca,zeros(2,1),Sigma_p);
xlabel('b','FontSize',11);
ylabel('a','FontSize',11);
title('Prior weights','FontSize',11);
grid on;
axis square;
axis_limits = axis;

subplot(1,2,2);
box on;
plot_gaussian_contour(gca,w,invA);
xlabel('b','FontSize',11);
ylabel('a','FontSize',11);
title('Posterior weights','FontSize',11);
grid on;
axis square;

h3 = figure;
hold on;box on;
scatter(X,y,20,'filled');
hp = plot(x_test(1,:),y_test,'--r','LineWidth',2);
grid on;
axis square;
legend(hp,'regression line');
title('y = b * x + a');


function [N,errL2,errH1,erruIuh] = cubePoissoncvtmeshnew
% CUBEPOISSON solves Poisson equation in a cube.
%
% Copyright (C) Long Chen. See COPYRIGHT.txt for details.

close all; clear all;
%% Parameters
maxIt = 4; N = zeros(maxIt,1);
errL2 = zeros(maxIt,1); errH1 = zeros(maxIt,1); erruIuh = zeros(maxIt,1);

%% Get the data of the pde
pde = sincosdata3;
% pde = polydata3;

%% Finite Element Method
for k = 1:maxIt
    [node,elem] = cvtuniformmesh(2^k);
    % solve the equation
    option.solver = 'direct';
    [u,Du,A,eqn] = Poisson3(node,elem,pde,[],[],option);
    N(k) = size(node,1);
    % compute error
    errL2(k) = getL2error3(node,elem,pde.exactu,u);
    errH1(k) = getH1error3(node,elem,pde.Du,Du);
    uI = pde.exactu(node);  % nodal interpolation
    erruIuh(k) = sqrt((u-uI)'*eqn.AD*(u-uI));
end

%% Plot convergence rates
figure(2);
r1 = showrate(N,errH1,2,'-*');
hold on;
r2 = showrate(N,errL2,2,'k-+');
r3 = showrate(N,erruIuh,2,'m-+');
legend('||Du-Du_h||',['N^{' num2str(r1) '}'], ...
       '||u-u_h||',['N^{' num2str(r2) '}'], ...
       '||DuI-Du_h||',['N^{' num2str(r3) '}'], 'LOCATION','Best');
%%
% The error in H1 and L2 norm converges at optimal rate. But no
% superconvergence on criss-cross grids.
end


function B = dofan(phi, alphaFromTo, alphaStep, tN, tStep)
% Discretization matrix for
% Left-sided Distributed-order fractional derivative
%
% Function "phi", which describes the distribution
% of the order of differentiation, must be given
% as a string with variable "alf", for example:
% '6*alf.*(1-alf)'
%
% (C) Igor Podlubny, 2011

% Suppose the input parameters are given correctly
% (we do not validate the input parameters for now)

% Check if the FEX package 22071 is on your MATLAB path,
% and if it is not there, then require the FEX packages
% 31069 ("requireFEXpackage") and 22071 ("Matrix approach
% to discretization of ODEs and PDEs of arbitrary real order"):
if ~(exist('ban', 'file') == 2 && exist('fan', 'file') == 2 ...
    && exist('ranort', 'file') && exist('eliminator', 'file') == 2)
    P = requireFEXpackage(31069); % "requireFEXpackage"
    P = requireFEXpackage(22071);  % "Matrix approach..."
end

alphas = alphaFromTo(1):alphaStep:alphaFromTo(2);
alphaCount = length(alphas);

alf = alphas;
phik = eval(phi);

% Pre-allocate memory for layers with different orders 'alf'
B = zeros(tN, tN);

for k = 1:alphaCount
    B = B + phik(k) * alphaStep * fan(alf(k), tN, tStep);
end


% Mathematics Q454519
% https://math.stackexchange.com/questions/454519
% Least Squares with L2 Linear Norm Regularization (Not Squared)
% References:
%   1.  aa
% Remarks:
%   1.  sa
% TODO:
% 	1.  ds
% Release Notes
% - 1.0.000     22/08/2017
%   *   First release.

%% General Parameters

run('InitScript.m');

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

generateFigures = OFF;

%% Simulation Parameters

numRows     = 4;
paramBeta   = 0.5;

numIterations   = 25;

%% Generate Data

vU = randn([numRows, 1]);
vX = 1 + rand([numRows, 1]);

mX = diag(1 ./ vX);

%% Solution by CVX

cvx_begin('quiet')
    cvx_precision('best');
    variable vW(numRows)
    minimize( (0.5 * sum_square(vW - vU)) + (paramBeta * norm(vW ./ vX)) )
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(vW.'), ' ]']);
disp([' ']);

%% Solution by Iterative Reweighted Least Squares (IRLS)

hObjFun = @(vW) (0.5 * sum((vW - vU) .^ 2)) + (paramBeta * norm(vW ./ vX));

vObjVal = zeros([numIterations, 1]);

vW = vU;
vObjVal(1) = hObjFun(vW);

for ii = 2:numIterations

    % Calculation of the inverse term. Since all diagonlas terms can be
    % done using vectors.
    fctrTerm    = paramBeta / norm(vW ./ vX);
    vXX         = (1 ./ (vX .^ 2));
    vInvXX      = 1 ./ ((fctrTerm * vXX) + ones([numRows, 1]));

    vW = vInvXX .* vU;

    vObjVal(ii) = hObjFun(vW);
end

disp([' ']);
disp(['Iterative Reweighted Least Squares (IRLS) Solution Summary']);
disp(['The Optimal Value Is Given By - ', num2str(vObjVal(numIterations))]);
disp(['The Optimal Argument Is Given By - [ ', num2str(vW.'), ' ]']);
disp([' ']);

hFigure     = figure('Position', figPosLarge);
hAxes       = axes();
hLineSeries = plot(1:numIterations, [vObjVal, cvx_optval * ones([numIterations, 1])]);
set(hLineSeries, 'LineWidth', lineWidthNormal);
set(hLineSeries(2), 'LineStyle', ':');
set(get(hAxes, 'Title'), 'String', ['Objective Function Value vs. Iteration'], ...
    'FontSize', fontSizeTitle);
set(get(hAxes, 'XLabel'), 'String', 'Iteration Number', ...
    'FontSize', fontSizeAxis);
set(get(hAxes, 'YLabel'), 'String', 'Objective Function Value', ...
    'FontSize', fontSizeAxis);
set(hAxes, 'XLim', [1, numIterations]);
hLegend = ClickableLegend({['IRLS'], ['Optimal Value (CVX)']});
set(hAxes, 'LooseInset', [0.07, 0.07, 0.07, 0.07]);

if(generateFigures == ON)
    saveas(hFigure,['Figure', num2str(figureIdx, figureCounterSpec), '.png']);
end

%% Restore Defaults

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


function result = circle_cum ( func, xc, yc, radius, norder )

%*****************************************************************************80
%
%% CIRCLE_CUM approximates an integral on the circumference of a circle in 2D.
%
%  Integration region:
%
%    Points (X,Y) such that:
%
%      ( X - XC )^2 + ( Y - YC )^2 = RADIUS^2.
%
%  Discussion:
%
%    An NORDER point, (NORDER-1)-th degree formula is used,
%    Stroud number U2:M-1.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    22 May 2004
%
%  Reference:
%
%    Arthur H Stroud,
%    Approximate Calculation of Multiple Integrals,
%    Prentice Hall, 1971.
%
%  Parameters:
%
%    Input, function VALUE = FUNC ( X, Y ), the name of the user supplied
%    function of two variables which is to be integrated.  The name should
%    be passed as a quoted string.
%
%    Input, real XC, YC, the coordinates of the center of
%    the circle.
%
%    Input, real RADIUS, the radius of the circle.
%
%    Input, integer NORDER, the number of points to use.
%
%    Output, real RESULT, the approximate integral of the function.
%
  quad = 0.0E+00;

  for i = 1 : norder
    angle = ( 2 * i ) * pi / norder;
    x = xc + radius * cos ( angle );
    y = yc + radius * sin ( angle );
    quad = quad + feval ( func, x, y );
  end

  quad = quad / norder;

  volume = pi * radius * radius;
  result = quad * volume;

  return
end


function p = mvnormpdf(varargin)
%MVNORMPDF    Multivariate normal probability density function.
% MVNORMPDF(x) returns a row vector giving the density at each column of x
%   under a standard multivariate normal.
% MVNORMPDF(x,m) subtracts m from x first.
%   If cols(m) == 1, subtracts m from each column of x.
%   If cols(m) == cols(x), subtracts corresponding columns.
%   If cols(x) == 1, x is repeated to match cols(m).
% MVNORMPDF(x,m,S) specifies the standard deviation, or more generally
%   an upper triangular Cholesky factor of the covariance matrix.
%   In the univariate case, multiple standard deviations can be specified.
%   If m is empty, no subtraction is done (zero mean).
% MVNORMPDF(x,m,[],V) specifies the variance or covariance matrix.
% MVNORMPDF(x,m,'inv',iV) specifies the inverse of the covariance matrix, i.e.
%   the precision matrix.
% MVNORMPDF(x,m,iS,'inv') specifies the reciprocal of the standard deviation,
%   or more generally the upper triangular Cholesky factor of the
%   inverse covariance matrix.
%   This is the most efficient option.
% See test_normpdf for a timing test.

% this may look strange, but computing normpdf directly is no faster or
% more stable than exp(mvnormpdfln).
p = exp(mvnormpdfln(varargin{:}));


function z = snowflake(n,a)
%SNOWFLAKE Koch Snowflake Curve
%   Z = SNOWFLAKE(N,A) is a closed curve in the complex plane
%   with 3*2^N+1 points. N is a nonnegative integer and A is a
%   complex number with |A| < 1 and |1-A| < 1.
%   Default is A = 1/2 + i*sqrt(3)/6.
%
%   % Examples
%   plot(snowflake(10)), axis equal
%   plot(snowflake(10,0.45+0.35i)), axis equal

%   Author: Jonas Lundgren <splinefit@gmail.com> 2010

if nargin < 1, n = 0; end
if nargin < 2, a = 1/2 + sqrt(-3)/6; end

% Constants
b = 1 - a;
c = 1/2 + sqrt(-3)/2;
d = 1 - c;

% Generate point sequence
z = 1;
for k = 1:n
    z = conj(z);
    z = [a*z; b*z+a];
end

% Close snowflake
z = [0; z; 1-c*z; 1-c-d*z];


function [ as, bs, cs ] = sphere01_triangle_vertices_to_sides ( v1, v2, v3 )

%*****************************************************************************80
%
%% SPHERE01_TRIANGLE_VERTICES_TO_SIDES computes spherical triangle sides on unit sphere.
%
%  Discussion:
%
%    We can use the ACOS system call here, but the ARC_COSINE routine
%    will automatically take care of cases where the input argument is
%    (usually slightly) out of bounds.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    22 September 2010
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real V1(3), V2(3), V3(3), the vertices of the spherical
%    triangle.
%
%    Output, real AS, BS, CS, the (geodesic) length of the sides
%    of the triangle.
%
  as = r8_acos ( v2(1:3)' * v3(1:3) );
  bs = r8_acos ( v3(1:3)' * v1(1:3) );
  cs = r8_acos ( v1(1:3)' * v2(1:3) );

  return
end


close all;
clearvars;
clc;
dataset_file = fullfile(spx.data_dir, 'clustering', ...
    'self_tuning_paper_clustering_data');
data = load(dataset_file);
datasets = data.XX;
raw_data = datasets{1};
num_clusters = data.group_num(1);
X = raw_data(:, 1);
Y = raw_data(:, 2);
figure;
axis equal;
plot(X, Y, '.', 'MarkerSize',16);
export_fig images/demo_sc_1_unscaled.png -r120 -nocrop;
% Scale the raw_data
raw_data = raw_data - repmat(mean(raw_data),size(raw_data,1),1);
raw_data = raw_data/max(max(abs(raw_data)));
X = raw_data(:, 1);
Y = raw_data(:, 2);
figure;
axis equal;
plot(X, Y, '.', 'MarkerSize',16);
export_fig images/demo_sc_1_scaled.png -r120 -nocrop;

% Compute the distance matrix
sqrt_dist_mat = spx.commons.distance.sqrd_l2_distances_rw(raw_data);
%% Scale to use for standard spectral clustering
scale = 0.04;
% Compute the similarity matrix
sim_mat = spx.cluster.similarity.gauss_sim_from_sqrd_dist_mat(sqrt_dist_mat, scale);
% Run clustering on the dataset
clusterer = spx.cluster.spectral.Clustering(sim_mat);
clusterer.NumClusters = num_clusters;
%cluster_labels = clusterer.cluster_symmetric();
cluster_labels = clusterer.cluster_random_walk();
figure;
colors = [1,0,0;0,1,0;0,0,1;1,1,0;1,0,1;0,1,1;0,0,0];
hold on;
axis equal;
for c=1:num_clusters
    % Identify points in this cluster
    points = raw_data(cluster_labels == c, :);
    X = points(:, 1);
    Y = points(:, 2);
    plot(X, Y, '.','Color',colors(c,:), 'MarkerSize',16);
end
export_fig images/demo_sc_1_clustered.png -r120 -nocrop;


% book : Signals and Systems Laboratory with MATLAB
% authors : Alex Palamides & Anastasia Veloni
%
%
%
% periodic signals

t=1:5;
x=sin(t);
T=2*pi;
for k=1:10
xk(k,:)=sin(t+k*T);
end
xk

%Sum of periodic continuous time signals
 t=0:.1:6*pi;
 x=cos(t)+sin(3*t);
 plot(t,x)

 %	Construction of periodic signals
 figure
 [s,t]=gensig('square',3,20,0.01);
 plot(t,s)
 ylim([-.3 1.3])

 figure
 [s,t]=gensig('pulse',2,10);
 plot(t,s)
 ylim([-.3 1.3])

 figure
 t=0:0.1:10;
 s=square(t);
 plot(t,s);
 ylim([-1.3 1.3])

 figure
 s2=square(2*pi*t);
 plot(t,s2);
 ylim([-1.3 1.3]) ;

 figure
 t=0:0.1:20;
 s=sawtooth(t);
 plot(t,s);
 ylim([-1.3 1.3])

 figure
 t=0:.1:10;
 x=t.*exp(-t);
 xp=repmat(x,1,8);
 tp=linspace(0,80,length(xp));
 plot(tp,xp)

 figure
 N=10;
 x=[1 1 -1 -1];
 xp=repmat(x,1,N);
 n=0:length(xp)-1;
 stem(n,xp);


clear all; close all;
I=imread('cameraman.tif');
I=im2double(I);
J=fftshift(fft2(I));
[x, y]=meshgrid(-128:127, -128:127);
[M, N]=size(I);
n1=floor(M/2);
n2=floor(N/2);
z=sqrt((x-n1).^2+(y-n2).^2);
D1=10;  D2=30;
n=6;
H1=1./(1+(z/D1).^(2*n));
H2=1./(1+(z/D2).^(2*n));
K1=J.*H1;
K2=J.*H2;
L1=ifft2(ifftshift(K1));
L2=ifft2(ifftshift(K2));
figure;
subplot(131);
imshow(I);
subplot(132);
imshow(real(L1));
subplot(133);
imshow(real(L2))


function [V3D] = Vandermonde3D(N, r, s, t);

% function [V3D] = Vandermonde3D(N, r, s, t);
% Purpose : Initialize the 3D Vandermonde Matrix, V_{ij} = phi_j(r_i, s_i, t_i);

V3D = zeros(length(r),(N+1)*(N+2)*(N+3)/6);

% Transfer to (a,b) coordinates
[a, b, c] = rsttoabc(r, s, t);

% build the Vandermonde matrix
sk = 1;

for i=0:N % old ordering
  for j=0:N - i
    for k=0:N - i - j
      V3D(:,sk) = Simplex3DP(a,b,c,i,j,k);
      sk = sk+1;
    end
  end
end
return;


function [ a, b, c, d ] = plane_normal2imp_3d ( pp, normal )

%*****************************************************************************80
%
%% PLANE_NORMAL2IMP_3D converts a normal form plane to implicit form in 3D.
%
%  Discussion:
%
%    The normal form of a plane in 3D is
%
%      PP, a point on the plane, and
%      N, the unit normal to the plane.
%
%    The implicit form of a plane in 3D is
%
%      A * X + B * Y + C * Z + D = 0.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    26 February 2005
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real PP(3), a point on the plane.
%
%    Input, real NORMAL(3), the unit normal vector to the plane.
%
%    Output, real A, B, C, D, the implicit plane parameters.
%
  a = normal(1);
  b = normal(2);
  c = normal(3);
  d = - a * pp(1) - b * pp(2) - c * pp(3);

  return
end


%% Demo for "Fitit"
% Copyright (c) 2011, The MathWorks, Inc.

%% Generate a set of random data

clear all
clc

s = RandStream('mt19937ar','seed',1971);
RandStream.setDefaultStream(s);

X = linspace(1,10,100);
X = X';

% Specify the parameters for a second order Fourier series

w = .6067;
a0 = 1.6345;
a1 = -.6235;
b1 = -1.3501;
a2 = -1.1622;
b2 = -.9443;

% Fourier2 is the true (unknown) relationship between X and Y
Y = a0 + a1*cos(X*w) + b1*sin(X*w) + a2*cos(2*X*w) + b2*sin(2*X*w);

% Add in a noise vector

K = max(Y) - min(Y);
noisy = Y +  .2*K*randn(100,1);

%% Generate a set of random data

s = RandStream('mt19937ar','seed',1973);
RandStream.setDefaultStream(s);

hold off

X1 = linspace(1,10,100);
X1 = X1';

% Specify the parameters for a second order Fourier series

w = rand;
a0 = randn;
a1 = randn;
b1 = randn;
a2 = randn;
b2 = randn;

% Fourier2 is the true (unknown) relationship between X and Y
Y1 = a0 + a1*cos(X*w) + b1*sin(X*w) + a2*cos(2*X*w) + b2*sin(2*X*w);

% Add in a noise vector

K = max(Y1) - min(Y1);
noisy1 = Y1 +  .2*K*randn(100,1);

foo = fitit(X1,noisy1)

% Compare the output of "fitit" with the original model

hold on
plot(X1,Y1, 'r', 'linewidth', 2)

%%  Working with the fit object

figure
plot(My_Fit)
My_Fit(noisy(1:10))

figure;
subplot(2,1,1); plot(X,My_Fit(X)); title('My Fit');
subplot(2,1,2); plot(X, differentiate(My_Fit, X)); refline(0,0),title('First derivative of My Fit');

methods(My_Fit)

%%  Add confidence intervals into the mix

[avgfit,fitstd1,fitstd2] = fitit(X,noisy,1000);

%%  Explain Localized Regression

figure
scatter(X, noisy)

fit1 = smooth(X,noisy,0.02,'loess')
hold on
plot(X, fit1, 'r', 'linewidth', 2)

fit2 = smooth(X,noisy,0.95,'loess')
plot(X, fit2, 'k', 'linewidth', 2)

fit3 = smooth(X,noisy,0.2,'loess')
plot(X, fit3, 'b', 'linewidth', 2)

%% Explain Cross Validation

% Divide data set into a test set and a training set

cp = cvpartition(size(noisy,1),'k',10);

trainingset1 = X(training(cp,1));
trainingset2 = noisy(training(cp,1));

testset1 = X(test(cp,1));
testset2 = noisy(test(cp,1));

figure
trainingscatter = scatter(trainingset1,trainingset2,'+', 'r');
hold on
testscatter = scatter(testset1, testset2,'FaceColor','b','Filled');
legend('Training Set','Test Set', 'location', 'NorthWest');

%% Use Training Set to create a LOESS smooth

delete(testscatter);
Z = smooth(trainingset1,trainingset2,.5,'loess');
ZPlot = plot(trainingset1, Z, 'color','r','linestyle','-','linewidth',2);

%% Use the test set to evaluate goodness of fit

foobar = fit(trainingset1, Z, 'cubicinterp')
scatter(testset1, testset2,'FaceColor','b','Filled')

for i = 1: length(testset1)

    plot([testset1(i) testset1(i)], [foobar(testset1(i)) testset2(i) ])

end

%%  Explain Bootstrap

figure
scatter(X, noisy, 'b')

% Create a new data set
index = randsample(length(X),length(X), 'true');
hold on
scatter(X(index), noisy(index), 'filled', 'r')
bar = fit(X(index), noisy(index), 'fourier2');
plot(bar)
legend('Original Data Set', 'Random Sample', 'Fit', 'location', 'NorthWest')

%%  Repeat

index = randsample(length(X),length(X), 'true');
scatter(X(index), noisy(index), 'filled', 'k')
bar = fit(X(index), noisy(index), 'fourier2');
plot(bar, 'k')
legend('off')

%% Repeat 20 more times

for i = 1:20

    index = randsample(length(X),length(X), 'true');
    bar = fit(X(index), noisy(index), 'fourier2');
    plot(bar, 'b')

end

legend off


function [u,X,T,uf,t]=forcmove(a,v,tmax,nt)

% [u,X,T,uf,t]=forcmove(a,v,tmax,nt)
% See Article 9.4
% This function computes the dynamic response
% of a taut string subjected to a force moving
% along the string with constant speed. The
% string is fixed at x=0 and and x=+infinity.
% The system is initially at rest when the
% force is imposed at the left end and moves
% to the right at constant speed. If the force
% speed is larger than the wave propagation
% speed, then no disturbance occurs ahead of
% the force. If the force moves slower than the
% wave propagation speed, then the deflection
% propagates ahead of the force at the speed
% of wave propagation.
%
% v     - speed of the moving load
% a     - speed of wave propagation in the
%         string
% tmax  - maximum time for which the
%         solution is computed
% u     - matrix of deflection values where
%         time and position vary row-wise and
%         column-wise, respectively
% T,X   - matrices of time and position values
%         corresponding to the deflection
%         matrix U
% uf    - deflection values where the force acts
% t     - vector of times (same as colunms of T)

if nargin==0, a=.8; v=1; tmax=10; nt=15; end

% Obtain solution values and plot results
[u,X,T,uf,t]=ustring(a,v,tmax,nt);
if a>v, xf=X(:,2); uf=u(:,2); xw=X(:,3);
else, xf=X(:,3); uf=u(:,3); end
close, subplot(211)
waterfall(X,T,-u), xlabel('x axis')
ylabel('time'), zlabel('deflection')
titl=['INVERTED MOTION SURFACE FOR  A = ',...
      num2str(a),'  AND  V = ',num2str(v)];
title(titl), grid on, hold on
plot3(xf,t,-uf,'.k',xf,t,-uf,'k')
colormap([0 0 0]), view([-10,30]), shg
umin=min(u(:)); umax=max(u(:)); xmax=X(1,4);
range=[0,xmax,2*umin,2*umax]; hold on
Titl='T = %4.2f'; subplot(212) , axis off

% Use a dense set of points for animation
nt=80; [uu,XX,TT,uuf,tt]=ustring(a,v,tmax,nt);
umax=max(abs(uu(:))); uu=uu/umax; uuf=uuf/umax;
XX=XX/xmax; range=[0,1,-1,0.5]; h=.4;
arx=h*[0,.02,-.02,0,0]; ary=h*[0,.25,.25,0,1];
for j=1:nt
   uj=uu(j,:); xj=XX(j,:);
   xfj=v/xmax*tt(j); ufj=uuf(j);
   plot(xj,uj,'k',xfj+arx,ufj+ary,'-k')
   axis off, time=(sprintf(Titl,tt(j)));
   text(.45,.25,time), axis(range), drawnow
   pause(.05), figure(gcf), if j<nt, cla, end
end
hold off;

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

function [u,X,T,uf,t]=ustring(a,v,tmax,nt)
% [u,X,T,uf,t]=ustring(a,v,tmax,nt)
% This function computes the deflection u(x,t)
% of a semi-infinite string subjected to a
% moving force. The equation for the normalized
% deflection
% u(x,t)=1/a/(a^2-v^2)*((v-a-v*abs(x-a*t)...
%                           +a*abs(x-v*t));
% a    - speed of wave propagation in the string
% v    - speed of the force moving to the right
% tmax - maximum time for computing the solution
% nt   - number of time increments computed
% uu   - array of displacement values normalized
%        by dividing by a factor equal to the force
%        magnitude over twice the density per unit
%        length. Position varies column-wise and
%        time varies row-wise in the array.
% X,T  - position and time arrays for the solution
% uf   - deflection vector under the force
% t    - time vector for the solution (same as the
%        columns of T)
%
t=linspace(0,tmax,nt)'; xmax=1.05*tmax*max(a,v);
u=zeros(nt,4); nx=4; X=zeros(nt,nx); X(:,nx)=xmax;
c=1/a/(a^2-v^2); xw=a*t; xf=v*t; T=repmat(t,1,4);
uw=c*xw*(v-a+abs(v-a)); uf=c*xf*(v-a-abs(v-a));
if a>v
   X(:,2)=xf; X(:,3)=xw; u(:,2)=uf;
else
   X(:,2)=xw; X(:,3)=xf; u(:,2)=uw;
end


function variance = semicircular_variance ( a, b )

%*****************************************************************************80
%
%% SEMICIRCULAR_VARIANCE returns the variance of the Semicircular PDF.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    20 September 2004
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real A, B, the parameters of the PDF.
%    0.0 < B.
%
%    Output, real VARIANCE, the variance of the PDF.
%
  variance = b * b / 4.0;

  return
end


function [ ndp, xdp, ydp ] = dif_deriv ( nd, xd, yd )

%*****************************************************************************80
%
%% DIF_DERIV computes the derivative of a polynomial in divided difference form.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    23 June 2011
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Carl deBoor,
%    A Practical Guide to Splines,
%    Springer, 2001,
%    ISBN: 0387953663,
%    LC: QA1.A647.v27.
%
%  Parameters:
%
%    Input, integer ND, the size of the input table.
%
%    Input, real XD(ND), the abscissas for the divided
%    difference table.
%
%    Input, real YD(ND), the divided difference table.
%
%    Output, integer NDP, the size of the output table, which is ND-1.
%
%    Input, real XDP(NDP), the abscissas for the divided
%    difference table for the derivative.
%
%    Output, real YDP(NDP), the divided difference
%    table for the derivative.
%

%  Using a temporary copy of the difference table, shift the
%  abscissas to zero.
%
  xd_temp(1:nd) = xd(1:nd);
  yd_temp(1:nd) = yd(1:nd);

  [ xd_temp, yd_temp ] = dif_shift_zero ( nd, xd_temp, yd_temp );
%
%  Construct the derivative.
%
  ndp = nd - 1;

  xdp(1:ndp) = 0.0;

  for i = 1 : ndp
    ydp(i) = i * yd_temp(i+1);
  end

  return
end


%% UNO Rosenbrock
clc
%Objective
nlprob.objective = @(x) (1-x(1))^2 + 100 *(x(2)-x(1)^2)^2;
x0 = [0 1]';

opts = [];
opts.algorithm = nloptSolver('LN_PRAXIS');
opts.maxfeval = 1000;
opts.maxtime = 1;
opts.display = 2;
nlprob.options = opts;

[x,f,e,i] = nlopt(nlprob,x0)

%% UNO Rosenbrock [GRAD]
clc
%Objective
nlprob = [];
nlprob.objective = @(x) (1-x(1))^2 + 100 *(x(2)-x(1)^2)^2;
nlprob.gradient = @(x) mklJac(nlprob.objective,x);
x0 = [0 1]';

opts = [];
opts.algorithm = nloptSolver('LD_LBFGS');
opts.maxfeval = 100;
opts.maxtime = 1;
opts.display = 2;
nlprob.options = opts;

[x,f,e,i] = nlopt(nlprob,x0)

%% BOUNDED Rosenbrock
clc
%Objective
nlprob = [];
nlprob.objective = @(x) (1-x(1))^2 + 100 *(x(2)-x(1)^2)^2;
nlprob.lb = [-5;-5];
nlprob.ub = [1;0.8];
x0 = [0 0]';

opts = [];
opts.algorithm = nloptSolver('LN_PRAXIS');
opts.maxfeval = 1000;
opts.maxtime = 10;
opts.display = 2;
nlprob.options = opts;

[x,f,e,i] = nlopt(nlprob,x0)

%% BOUNDED GRAD Rosenbrock
clc
%Objective
nlprob = [];
nlprob.objective = @(x) (1-x(1))^2 + 100 *(x(2)-x(1)^2)^2;
nlprob.gradient = @(x) mklJac(nlprob.objective,x);
nlprob.lb = [-5;-5];
nlprob.ub = [1;0.8];
x0 = [0 0]';

opts = [];
opts.algorithm = nloptSolver('LD_LBFGS');
opts.maxfeval = 1000;
opts.maxtime = 10;
opts.display = 2;
nlprob.options = opts;

[x,f,e,i] = nlopt(nlprob,x0)

%% NLCON HS71
clc
%Objective
obj = @(x) x(1)*x(4)*sum(x(1:3)) + x(3);
%Linear Constraints
lb = ones(4,1);
ub = 5*ones(4,1);
%Nonlinear Constraints
nlcon = @(x) [prod(x); sum(x.^2)];

nlprob =[];
nlprob.objective = obj;
nlprob.nlcon = nlcon;
nlprob.nlrhs = [25;40];
nlprob.nle = [1;0];
nlprob.lb = lb;
nlprob.ub = ub;

opts = [];
opts.algorithm = nloptSolver('LN_COBYLA');
opts.maxfeval = 1000;
opts.maxtime = 2;
opts.display = 2;
nlprob.options = opts;

x0 = zeros(4,1);

[x,f,e,i] = nlopt(nlprob,x0,opts)

%% NLCON GRAD HS71
%Objective & Gradient
obj = @(x) x(1)*x(4)*sum(x(1:3)) + x(3);
grad = @(x) [ x(1)*x(4) + x(4)*sum(x(1:3));
              x(1)*x(4);
              x(1)*x(4) + 1;
              x(1)*sum(x(1:3)) ];
%Linear Constraints
lb = ones(4,1);
ub = 5*ones(4,1);
%Nonlinear Constraints
nlcon = @(x) [prod(x); sum(x.^2)];
nljac = @(x) [prod(x)./x'; 2*x'];

nlprob =[];
nlprob.objective = obj;
nlprob.gradient = grad;
nlprob.nlcon = nlcon;
nlprob.nljac = nljac;
nlprob.nlrhs = [25;40];
nlprob.nle = [1;0];
nlprob.lb = lb;
nlprob.ub = ub;

opts = [];
opts.algorithm = nloptSolver('LD_SLSQP');
opts.maxfeval = 1000;
opts.maxtime = 2;
opts.display = 2;
nlprob.options = opts;

x0 = [1 5 5 1]';

[x,f,e,i] = nlopt(nlprob,x0,opts)

%% NLP1 Hock & Schittkowski #71
clc
%Objective & Gradient
obj = @(x) x(1)*x(4)*sum(x(1:3)) + x(3);
grad = @(x) [ x(1)*x(4) + x(4)*sum(x(1:3));
              x(1)*x(4);
              x(1)*x(4) + 1;
              x(1)*sum(x(1:3)) ];
%Linear Constraints
lb = ones(4,1);
ub = 5*ones(4,1);
%Nonlinear Constraints
nlcon = @(x) [ prod(x);
               sum(x.^2)];
nljac = @(x) [ prod(x)./x';
                2*x' ];
nlrhs = [25 40]';
nle = [1 0]'; % (>=, ==)
%Setup Options
opts = optiset('solver','nlopt','warnings','on','display','iter','solverOpts',nloptset('algorithm','LD_SLSQP'));
%Build & Solve
Opt = opti('obj',obj,'grad',grad,'nlmix',nlcon,nlrhs,nle,'nljac',nljac,'bounds',lb,ub,'options',opts)
x0 = [1 5 5 1]';
[x,fval,exitflag,info]= solve(Opt,x0)

%% LP [-31.4]
clc
% clear all
%Objective & Constraints
f = -[6 5]';
A = ([1,4; 6,4; 2, -5]);
b = [16;28;6];

nlprob = [];
nlprob.objective = @(x) f'*x;
nlprob.gradient = @(x) f;

nlprob.nlcon = @(x) A*x;
nlprob.nljac = @(x) A;
nlprob.nlrhs = b;
nlprob.nle = [-1;-1;-1];

nlprob.lb = [0;0];
nlprob.ub = [10;10];

opts = [];
opts.algorithm = nloptSolver('LD_SLSQP');
opts.maxfeval = 100;
opts.maxtime = 2;
opts.display = 2;
nlprob.options = opts;

x0 = [0;0];

[x,f,e,i] = nlopt(nlprob,x0)

%% LP1
clc
%Objective & Constraints
f = -[6 5]';
A = ([1,4; 6,4; 2, -5]);
b = [16;28;6];
%Build Object
Opt = opti('obj',@(x) f'*x,'grad',@(x) f,'ineq',A,b,'bounds',[0;0],[10;10],'options',optiset('solver','nlopt','solverOpts',nloptset('algorithm','LD_SLSQP')))
%Build & Solve
[x,fval,exitflag,info] = solve(Opt,[0;0])
%Plot
plot(Opt)
%Check Solution
[ok,msg] = checkSol(Opt)

%% Auglag NLP
clc
fun = @(x) log(1+x(1)^2) - x(2);
grad = @(x)[[(2*x(1))/(x(1)^2+1)];[-1]];
nlcon = @(x) (1 + x(1)^2)^2 + x(2)^2 - 4;
nljac = @(x)[[4*x(1)*(x(1)^2+1),2*x(2)]];
nlrhs = 0;
nle = 0;
x0 = [2;2];

nlprob = [];
nlprob.objective = fun;
nlprob.gradient = grad;

nlprob.nlcon = nlcon;
nlprob.nljac = nljac;
nlprob.nlrhs = nlrhs;
nlprob.nle = nle;

opts = [];
opts.algorithm = nloptSolver('LD_AUGLAG');
opts.maxfeval = 120;
opts.maxtime = 2;
opts.display = 2;

opts.local_optimizer.algorithm = nloptSolver('LD_LBFGS');
nlprob.options = opts;

[x,f,e,i] = nlopt(nlprob,x0)

%% Error checking
clc
clear
nlprob.objective = @(x) -1;
nlprob.lb = [-1;-1];
nlprob.ub = [1;1];

nlprob.nlcon = @(x) [-1;-1];
nlprob.nlrhs = [-1;-1];
nlprob.nle = [1;1];

nlprob.options.algorithm = 25;
x0 = [1;1];

[x,f,e,i] = nlopt(nlprob,x0)

%% Stop Val Checking
clc
%Objective
nlprob = [];
nlprob.objective = @(x) (1-x(1))^2 + 100 *(x(2)-x(1)^2)^2;
nlprob.lb = [-5;-5];
nlprob.ub = [1;1];
x0 = [0 0]';

opts = [];
opts.algorithm = nloptSolver('LN_PRAXIS');
opts.maxfeval = 1000;
opts.maxtime = 10;
opts.display = 2;
opts.stopval = 1e-6;
nlprob.options = opts;

[x,f,e,i] = nlopt(nlprob,x0)

%% Vector Storage Checking
clc
%Objective
nlprob = [];
nlprob.objective = @(x) (1-x(1))^2 + 100 *(x(2)-x(1)^2)^2;
nlprob.gradient = @(x) mklJac(nlprob.objective,x);
nlprob.lb = [-5;-5];
nlprob.ub = [1;0.8];
x0 = [0 0]';

opts = [];
opts.algorithm = nloptSolver('LD_LBFGS');
opts.maxfeval = 1000;
opts.maxtime = 10;
opts.display = 2;
opts.vector_storage = 1;
nlprob.options = opts;

[x,f,e,i] = nlopt(nlprob,x0)

%% Initial Pop Testing
clc
%Objective
nlprob = [];
nlprob.objective = @(x) (1-x(1))^2 + 100 *(x(2)-x(1)^2)^2;
nlprob.lb = [0;0];
nlprob.ub = [1;1];
x0 = [0 0]';

opts = [];
opts.algorithm = nloptSolver('GN_CRS2_LM');
opts.maxfeval = 1000;
opts.maxtime = 10;
opts.display = 1;
opts.initial_pop = 30;
nlprob.options = opts;

[x,f,e,i] = nlopt(nlprob,x0)


function prob_test034 ( )

%*****************************************************************************80
%
%% TEST034 tests CHI_SQUARE_MEAN, CHI_SQUARE_SAMPLE, CHI_SQUARE_VARIANCE.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    15 April 2009
%
%  Author:
%
%    John Burkardt
%
  nsample = 1000;
  seed = 123456789;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST034\n' );
  fprintf ( 1, '  For the central chi square PDF:\n' );
  fprintf ( 1, '  CHI_SQUARE_MEAN computes the mean;\n' );
  fprintf ( 1, '  CHI_SQUARE_SAMPLE samples;\n' );
  fprintf ( 1, '  CHI_SQUARE_VARIANCE computes the variance.\n' );

  a = 10.0;

  check = chi_square_check ( a );

  if ( ~check );
    fprintf ( 1, '\n' );
    fprintf ( 1, 'TEST034 - Fatal error!\n' );
    fprintf ( 1, '  The parameters are not legal.\n' );
    return
  end

  mean = chi_square_mean ( a );
  variance = chi_square_variance ( a );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  PDF parameter A = %14f\n', a );
  fprintf ( 1, '  PDF mean =        %14f\n', mean );
  fprintf ( 1, '  PDF variance =    %14f\n', variance );

  for i = 1 : nsample
    [ x(i), seed ] = chi_square_sample ( a, seed );
  end

  mean = r8vec_mean ( nsample, x );
  variance = r8vec_variance ( nsample, x );
  xmax = max ( x(1:nsample) );
  xmin = min ( x(1:nsample) );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Sample size =     %6d\n', nsample );
  fprintf ( 1, '  Sample mean =     %14f\n', mean );
  fprintf ( 1, '  Sample variance = %14f\n', variance );
  fprintf ( 1, '  Sample maximum =  %14f\n', xmax );
  fprintf ( 1, '  Sample minimum =  %14f\n', xmin );

  return
end


function [m,v]=v_chimv(n,l,s)
%V_CHIMV approximate mean and variance of non-central chi distribution [m,v]=(n,l,s)
%
%  Inputs:	n = degrees of freedom
%           l = non-centrality parameter = sqrt(sum(mean^2)) [default 0]
%               (can be a vector or matrix to calculate many different values at once)
%           s = standard deviation of Gaussian [default 1]
%
% Outputs:  m = mean of chi distribution
%           v = variance of chi distribution
%
% If x=c+randn(n,1) is a column vector of Gaussian random numbers with mean vector c, then
% z=sqrt(x'*x) has a chi distributon with n degrees of freedom and non-centrality parameter
% l=sqrt(c'*c). The mean and variance of a chi distribution are given precisely by
%
%     m = sqrt(2)*exp(gammaln(0.5*n+0.5)-gammaln(0.5*n))*hypergeom(-0.5,0.5*n,-0.5*l^2)
%       = sqrt(pi/2) L(0.5,0.5*n-1,-0.5*l^2)
%     v = n+l^2-m^2
%
% where L(n,a,x) is the generalized Laguerre polynomial L_n^{(a)}(x) but this is very slow
% to calculate so this routine approximates these expressions.
%
% For n=1, the accuracy is high; for n>1, accuracy improves with increasing n.
% Accuracy is worst when the non-centrality parameter, l, is close to s*sqrt(n).
% Worst case errors as a function of n are:
%                       n:    1       2      3       5      10
%   worst case error in m:  1e-15   0.007  0.004  0.0015  0.0005

%      Copyright (C) Mike Brookes 2014
%      Version: $Id: v_chimv.m 4969 2014-08-05 18:24:30Z 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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
persistent ab pp qq nab
if isempty(ab)
    nab=200; % cache a few low values of n
    pp=[ 0.595336298258636  -1.213013700592756  -0.018016200037799   1.999986150447582 0];
    qq=[ -0.161514114798972   0.368983655790737  -0.136992134476950  -0.499681107630725 2];
    ni=1./(1:nab);
    ab=[polyval(qq,ni);polyval(pp,ni)];
end
if nargin<3
    s=1;
    if nargin<2
        l=0;
    end
end
ls=l/s;
l2=(ls).^2;
s2=s^2;
if n<=nab
    if n==1
        m=l.*(1-2*normcdf(-ls))+2*s*normpdf(-ls);
    else
        m=sqrt(l2+n-1+(ab(1,n)+ab(2,n)*l2).^(-1))*s;
    end
else
    m=sqrt(l2+n-1+(polyval(qq,1/n)+polyval(pp,1/n)*l2).^(-1))*s;
end
v=(n+l2)*s2-m.^2;


function point = intersectThreePlanes(plane1, plane2, plane3)
%INTERSECTTHREEPLANES Return intersection point between 3 planes in space.
%
%   LINE = intersectThreePlanes(PLANE1, PLANE2, PLANE3)
%   Returns the point or straight line belonging to three planes.
%   PLANE:  [x0 y0 z0  dx1 dy1 dz1  dx2 dy2 dz2]
%   POINT:  [x0 y0 z0]
%   IF rank of the coefficient matrix r1 = 3 and
%   Rank of the augmented matrix r2 = 3 return point
%   Otherwise returns point with NaN values.
%
%   See also
%   planes3d, intersectPlanes, intersectLinePlane

% ------
% Author: Roozbeh Geraili Mikola
% E-mail: roozbehg@berkeley.edu or roozbehg@live.com
% Created: 2017-09-20
% Copyright 2017-2022

% plane normal
n1 = normalizeVector3d(cross(plane1(:,4:6), plane1(:, 7:9), 2));
n2 = normalizeVector3d(cross(plane2(:,4:6), plane2(:, 7:9), 2));
n3 = normalizeVector3d(cross(plane3(:,4:6), plane3(:, 7:9), 2));

% Uses Hessian form, ie : N.p = d
% I this case, d can be found as : -N.p0, when N is normalized
d1 = dot(n1, plane1(:,1:3), 2);
d2 = dot(n2, plane2(:,1:3), 2);
d3 = dot(n3, plane3(:,1:3), 2);

% create coefficient and augmented matrices
A = [n1;n2;n3];
D = [d1;d2;d3];
AD = [n1,d1;n2,d2;n3,d3];

% calculate rank of the coefficient and augmented matrices
r1 = rank(A);
r2 = rank(AD);

% if rank of the coefficient matrix r1 = 3 and
% rank of the augmented matrix r2 = 3 return point
% and if r1 = 2 and r2 = 2 return line,
% otherwise returns point with NaN values.
if r1 == 3 && r2 == 3
    % Intersecting at a point
    point = (A\D)';
else
    point = [NaN NaN NaN];
end


function unique_num = point_tol_unique_count ( m, n, a, tol )

%*****************************************************************************80
%
%% POINT_TOL_UNIQUE_COUNT counts the tolerably unique points.
%
%  Discussion:
%
%    The input data is an M x N array A, representing the M-dimensional
%    coordinates of N points.
%
%    This function uses a simple but expensive approach.  The first point
%    is accepted as unique.  Each subsequent point is accepted as unique
%    only if it is at least a tolerance away from all accepted unique points.
%    This means the expected amount of work is O(N^2).
%
%    The output is the number of unique points in the list.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    24 July 2010
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer M, the number of rows.
%
%    Input, integer N, the number of columns.
%
%    Input, real A(M,N), the array of N columns of data.
%
%    Input, real TOL, a tolerance.
%
%    Output, integer UNIQUE_NUM, the number of unique points.
%
  unique(1:n) = 1;
  unique_num = n;

  for i = 2 : n

    for j = 1 : i - 1
      if ( unique(j) )
        dist = sqrt ( sum ( ( a(1:m,i) - a(1:m,j) ).^2 ) );
        if ( dist <= tol )
          unique(i) = 0;
          unique_num = unique_num - 1;
          break
        end
      end
    end

  end

  return
end


function npart = setpart_enum ( m )

%*****************************************************************************80
%
%% SETPART_ENUM enumerates the partitions of a set of M elements.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 January 2011
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Donald Kreher, Douglas Simpson,
%    Combinatorial Algorithms,
%    CRC Press, 1998,
%    ISBN: 0-8493-3988-X,
%    LC: QA164.K73.
%
%  Parameters:
%
%    Input, integer M, the number of elements in the set.
%    M must be positive.  However, for the enumeration routine only,
%    it is legal to call with any value of M.
%
%    Output, integer NPART, the number of partitions of the set.
%
  if ( m < 0 )

    npart = 0;

  elseif ( m == 0 )

    npart = 1;

  else

    offset = 1;
    b(0+offset) = 1;
    for j = 1 : m
      b(j+offset) = 0;
      for i = 0 : j - 1
        b(j+offset) = b(j+offset) + i4_choose ( j - 1, i ) * b(i+offset);
      end
    end

    npart = b(m+offset);

  end

  return
end


function H =  sndspd_mean(z,ssp,d)
% harmonic_mean
%
% Val Schmidt
% Center for Coastal and Ocean Mapping
% University of New Hampshire
% 2012
%
% A routine to calculate the harmonic mean sound speed.
%
%   z:         Vector of depth values. (m)
%   ssp:       Vector of sound speed values at the associated depths. (m/s)
%   d:         [Optional] maximum depth (m) (see below)
%
% Suppose one is given a sound speed profile (sound speeds [ssp] and depths [z]).
% One wishes to know how long it will take a sound pulse (say from a sonar)
% to travel down through the water column.
%
% Most people will realize that one cannot simply average the sound speeds
% and divide by the total depth. However many will incorrectly assume that
% a depth-weighted mean sound will produce the correct answer.
% Unfortunately it does not.
%
% One must instead divide the total depth by the sum of the time it takes
% to the sound to pass through each layer of constant sound speed. This is
% called the 'harmonic mean'. The harbmonic mean is calculated like so.
%
%              |    i=n                | -1
%              |  SUM    delta z(i)    |
%              |   i=1   ---------     |
%              |            c(i)       |
% C_harmonic = |   ------------------- |
%              |    i=n                |
%              |  SUM     delta z(i)   |
%              |     i=1               |
%              |                       |
%
% When the maximum depth, 'd', is specified, two possible things may result
% depending on whether d is greater or less than the maximum depth in z.
%
% Sometimes one has a complete sound speed profile, but only needs the
% harmonic sound speed to some max depth less than the maximum depth of the
% profile. One may then specify the maximum depth as the argument 'd', and
% the function will return a harmonic mean to only that depth.
%
% Alternatively, one frequently has a sound speed profile for the upper
% portion of the water column only, but desireds a harmonic sound speed all
% the way to the bottom. Below some max depth temperature and salnity are
% constant and sound speed increases nearly linearly (to about 1 part in
% 10^4) with increasing depth. In this case, one may specify a the bottom
% depth, d. The function will then approximate the the sound speed at the
% bottom linearly from the final value in the ssp vector and calculate a
% harmonic sound speed for the entire path.
%
% When the sound speed profile is a constant linear gradient rather than
% discrete values one can calculate the time required to traverse the
% layer directly rather than approximating the gradient in discrete steps.
% That time is given by
%
%       1      c(i+1)
% t =   - ln ( ------ )
%       g       c(i)
%
% This value is then added to the summation of travel times (with a
% corresponding addition to the summation of depth differences) in the
% haronic mean calculation above.

%%

% Initialize a variable. variables
t=0;

% Mackenzie Formula first-order term for sound speed as a function of
% depth.
D1=.0163;

% Check some details.
if ( ~isequal( size(z),size(ssp) ) )
    error('ERROR input vectors are not of same size');
end

% The algorithm below will produce incorrect results if the first depth
% value is not zero. So we force it to be zero, shifting the other depths
% appropriately.
top=z(1);
z=z-top;

% One can optionally specify a max bottom depth. This value may be less
% than, or deeper than, the lowest depth in the z vector. If it is less than
% than z(end), the harmonic mean to the depth, d is calculated. If it is more
% than z(end), the sound speed profile is extended to the depth d using the
% first-order depth dependent term from the Mackenzie equation and the
% results of the Harmonic mean calculation for a gradient.

if ( nargin==3 )

    % First we shift the d value as we did the z values to make sure
    % everything matches.
    d=d-top;

    % If d is less than the max depth in z truncate the z and ssp vectors
    % to that depth.
    if (d < max(z) )
        idx=find( z <= d);
        z=z(idx);
        ssp=ssp(idx);

    % Otherwise we calculate the extra time in this final layer.
    else

    % We need the sound speed at hte specified bottom.
    ss_bottom = ssp(end) + ( d - z(end) ) * D1;

    % This is the equation for the time spent in a layer having a gradient
    % sound speed, D1, and where the difference in sound speed from the top to
    % the bottom of the layer is delta_ss.
    t= 1/D1 *log( ss_bottom / ssp(end) );

    end

else
    % Give d the max depth regardless.
    d=z(end);
end

% Calculate the harmonic mean sound speed.
H = ( (sum(diff(z)./ssp(1:(length(ssp)-1))) + t ) / d )^(-1);


function node_rhs = rhs ( node_num, node_xy )

%*****************************************************************************80
%
%% RHS gives the right-hand side of the differential equation.
%
%  Discussion:
%
%    This routine is set up for the L-shaped region, with exact solution
%    U = X^2 + Y^2.  Hence, the right hand side of the equation is
%    exactly -4.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    06 December 2010
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer NODE_NUM, the number of nodes.
%
%    Input, real NODE_XY(2,NODE_NUM),
%    the coordinates of the points.
%
%    Output, real NODE_RHS(NODE_NUM,1), the value of the
%    right hand side function at the points.
%
  node_rhs(1:node_num,1) = -4.0                     ...
                       + node_xy(1,1:node_num).^2 ...
                       + node_xy(2,1:node_num).^2;

  return
end


function IQR=interquartileRange(x,dim,definition)
%%INTERQUARTILERANGE Determine the interquartile range of the values in x.
%             This is notionally the difference between the value in the
%             75th percentile (the third quartile) and the 25th percentile
%             (the first quartile) of the data. However, the strict
%             definition varies, which is why there are multiple options.
%
%INPUTS: x A vector, matrix or hypermatrix of real values. The
%          interquartile range is only taken over a particular dimension.
%      dim An optional parameter specifying the dimensions across which
%          the interquartile range is found. If this value is omitted or an
%          empty matrix is passed, then the interquartile range is computed
%          across the first non-singleton dimension of x.
% definition This selects the definition fo the interquartile range that is
%          to be used. Let n be the number of points in dimension dim of x.
%          The following definitions are considering each independent
%          vector in dimension dim being processed. Possible values are:
%          0 (The default if omitted or an empty matrix is passed) If an
%            odd number of points is passed, then the first quartile is the
%            median of ordered (ascending) points up to floor(n/2) and the
%            third quartile is the median of ordered points from ceil(n/2)
%            to n. Thus, the median is assigned to the upper half. If an
%            even number of points is passed, then the first quartile is
%            the median of the ordered points up to n/2 and the third
%            quartile is the median of ordered points n/2+1 to n. Thus, the
%            halves are split.
%          1 This is the same as 0, except when given an odd number of
%            points, the middle point is omitted, not assigned to the upper
%            quartile. If only 1 point is given, 0 is returned.
%          2 This is the same as 0 and 1 except when given an odd number of
%            points, the median point is included in both the lower and the
%            upper quartile. This type of interquartile range is also known
%            as the midhinge.
%          3 The first and third quartiles are given by percentileVal with
%            its definition=0.
%          4 The first and third quartiles are given by percentileVal with
%            its definition=1.
%          5 This uses the kthOrderStat to get the 1+floor(0.25*(n-1)) and
%            1+ceil(0.75*(n-1)) order statistics.
%          6 In the set of ordered points, let the split point be
%            k50=ceil(n/2). The lower quartile value is the one at index
%            ceil(k50/2) and the upper quartile value is the one at index
%            k50+ceil((n-k50)/2). This is identical to considering the
%            points part of an empirical distribution and choosing the
%            percentiles using EmpiricalD.invCDF. If none aling directly
%            with 1/4 and 3/4, choose the next highest point.
%
%OUTPUTS: IQR The interquartile range values. This is a matrix with the
%             same dimensions as x except dimension dim has become unitary.
%             If dimension dim is a singleton, then IRQ will be zero for
%             all definitions.
%
%EXAMPLE:
%Here, one sees some of the differences:
% for def=0:6
%     IQR=interquartileRange([1,2,3,4,5],[],def)
% end
% one will get 2.5, 3, 2, 2.5, 2, 2, and 2.
%
%September 2018 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(isempty(x))
    IQR=[];
    return;
end

%Select the first non-singleton dimension if dim is not provided.
if(nargin<2||isempty(dim))
    dim=find(size(x)>1,1);
    if(isempty(dim))
        dim=1;
    end
end

if(nargin<3||isempty(definition))
    definition=0;
end

n=size(x,dim);

if(n==1)%If the interquartile range is a singleton, then zero is returned
        %for all methods.
    IQR=zeros(size(x));
    return;
end

switch(definition)
    case 0%The definition used in Mathematica and in Matlab's IQR function.
          %The middle point for an odd number of points is assigned to the
          %third quartile.
        idxVec=repmat({':'},1,ndims(x));

        x=sort(x,dim,'ascend');
        if(mod(n,2)==0)%Split the halves
            idxVec{dim}=1:(n/2);
            Q1=median(x(idxVec{:}),dim);

            idxVec{dim}=(n/2+1):n;
            Q3=median(x(idxVec{:}),dim);
        else%Assign the median point to the third quartile.
            idxVec{dim}=1:floor(n/2);
            Q1=median(x(idxVec{:}),dim);

            idxVec{dim}=ceil(n/2):n;
            Q3=median(x(idxVec{:}),dim);
        end
    case 1%Alternative successive median definition 1.
        idxVec=repmat({':'},1,ndims(x));

        x=sort(x,dim,'ascend');
        if(mod(n,2)==0)%Split the halves
            idxVec{dim}=1:(n/2);
            Q1=median(x(idxVec{:}),dim);

            idxVec{dim}=(n/2+1):n;
            Q3=median(x(idxVec{:}),dim);
        else%Omit the median point.
            idxVec{dim}=1:floor(n/2);
            Q1=median(x(idxVec{:}),dim);

            idxVec{dim}=(ceil(n/2)+1):n;
            Q3=median(x(idxVec{:}),dim);
        end
    case 2%Alternative successive median definition 2.
        idxVec=repmat({':'},1,ndims(x));

        if(mod(n,2)==0)%Split the halves
            idxVec{dim}=1:(n/2);
            Q1=median(x(idxVec{:}),dim);

            idxVec{dim}=(n/2+1):n;
            Q3=median(x(idxVec{:}),dim);
        else%Include the median point in both the upper and the lower
            %quartiles.
            idxVec{dim}=1:ceil(n/2);
            Q1=median(x(idxVec{:}),dim);

            idxVec{dim}=(ceil(n/2)):n;
            Q3=median(x(idxVec{:}),dim);
        end

    case 3%The percentile definition, option 0.
        Q1=percentileVal(x,25,dim,0);
        Q3=percentileVal(x,75,dim,0);
    case 4%The percentile definition, option 1.
        Q1=percentileVal(x,25,dim,1);
        Q3=percentileVal(x,75,dim,1);
    case 5%The order statistic definition.
        [Q1,x]=kthOrderStat(x,1+floor(0.25*(n-1)),dim);
        Q3=kthOrderStat(x,1+ceil(0.75*(n-1)),dim);
    case 6
        idxVec=repmat({':'},1,ndims(x));

        x=sort(x,dim,'ascend');

        k50=ceil(n/2);
        k25=ceil(k50/2);
        k75=k50+ceil((n-k50)/2);

        idxVec{dim}=k25;
        Q1=x(idxVec{:});

        idxVec{dim}=k75;
        Q3=x(idxVec{:});
    otherwise
        error('Unknown definition of the interquartile range specified.')
end

IQR=Q3-Q1;

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.


%% Analyzing Neural Time Series Data
% Matlab code for Chapter 12
% Mike X Cohen
%
% This code accompanies the book, titled "Analyzing Neural Time Series Data"
% (MIT Press). Using the code without following the book may lead to confusion,
% incorrect data analyses, and misinterpretations of results.
% Mike X Cohen assumes no responsibility for inappropriate or incorrect use of this code.

%% Figure 12.1

load sampleEEGdata

time = -1:1/EEG.srate:1;
f = 4; % frequency of sine wave in Hz

% create sine wave (actually, a cosine wave, for reasons that will become
% clear in Chapter 13)
sine_wave = cos(2*pi*f.*time);

% make a Gaussian
s=4/(2*pi*f);
gaussian_win = exp(-time.^2./(2*s^2));

figure
plot(time,sine_wave.*gaussian_win)

%% Figure 12.2

figure
subplot(511)
plot(squeeze(EEG.data(47,:,1)))
axis tight

subplot(512)
sine_wave = cos(2*pi*12.*time);
plot(sine_wave)
axis tight

subplot(513)
% boxcar envelope
boxcar = zeros(size(sine_wave));
midpoint = (length(time)-1)/2;
boxcar(midpoint-round(EEG.srate/12/5):midpoint+round(EEG.srate/12/1.25)) = 1;
plot(sine_wave.*boxcar)
axis tight

subplot(514)
% boxcar envelope
boxcar = zeros(size(sine_wave));
midpoint = (length(time)-1)/2;
boxcar(midpoint-50:midpoint+50) = 1;
plot(sine_wave.*boxcar)
axis tight

subplot(515)
% redefine gaussian for new sine wave
s=1.5/(2*pi*12); gaussian_win = exp(-time.^2./(2*s^2));
plot(time,sine_wave.*gaussian_win)
axis tight

%% Figure 12.3

srate = 500; % sampling rate in Hz
f = 10; % frequency of the sine wave in Hz
time = -1:1/srate:1; % time, from -1 to 1 second in steps of 1/sampling-rate

sine_wave = exp(2*pi*1i*f.*time); % complex wavelet

% make a Gaussian
s=6/(2*pi*f);
gaussian_win = exp(-time.^2./(2*s^2));

% and together they make a wavelet!
wavelet = sine_wave .* gaussian_win;

% make plots containing each component
figure
subplot(311)
plot(time,real(sine_wave)) % only plot the real component... we�ll learn about this later
title('Sine wave')

subplot(312)
plot(time,gaussian_win) % plots the Gaussian window
title('Gaussian window')

subplot(313)
plot(time,real(wavelet)); % plots the wavelet
title('My first wavelet!')
xlabel('Time (ms)')

%% Figure 12.4

num_wavelets      =  80;  % number of frequency bands
lowest_frequency  =   2;  % in Hz
highest_frequency = 100;  % in Hz

frequencies=linspace(lowest_frequency,highest_frequency,num_wavelets);
% note: the "linspace" function creates linearly spaced numbers between the first and second
% inputs, with the number of steps corresponding to the third input.
figure, plot(frequencies,'-*')
xlabel('Frequency order')
ylabel('Frequency in Hz')

% initialize wavelet family
wavelet_family = zeros(num_wavelets,length(time));

% Loop through frequencies and make a family of wavelets.
for fi=1:num_wavelets

    % create a sine wave at this frequency
    sinewave = exp(2*1i*pi*frequencies(fi).*time); % the "1i" makes it a complex wavelet

    % create a Gaussian window
    gaus_win = exp(-time.^2./(2*(6/(2*pi*frequencies(fi)))^2));

    % create wavelet via element-by-element multiplication of the sinewave and gaussian window
    wavelet_family(fi,:) = sinewave.*gaus_win;

    % note that you can also do this on one line:
    wavelet_family(fi,:) = exp(2*1i*pi*frequencies(fi).*time) .* exp(-time.^2./(2*(6/(2*pi*frequencies(fi)))^2));
end

% Plot a few wavelets
figure
subplot(2,1,1)
plot(time,real(wavelet_family(1:round(rand*30):end,:))')
title('A few wavelets...')

% Note that in the subplot command you don't need commas if you have fewer than 10
% rows/cols and if you are not using any variables.
subplot(212)
plot(time,real(wavelet_family(30,:)))
hold on
plot(time,imag(wavelet_family(30,:)),':')
title('Real and imaginary parts of one wavelet')
legend({'real';'imaginary'})

% finally, image the wavelet family.
figure
imagesc(time,frequencies,real(wavelet_family))
axis xy % equivalent to "set(gca,'ydir','normal')
xlabel('Time (s)')
ylabel('Frequency (Hz)')

%% bonus feature

% Running this cell will generate a movie that shows how a line -- in this
% case, a wavelet -- goes from a line that goes up and down, to a colored
% 'flat' line in a 2-D image. This is the principle that underlies viewing
% time-frequency plots.

figure, set(gcf,'color','k')
surf(repmat(real(wavelet),2,1))
shading interp
axis off
axis([0 length(time) -20 21 -1 1])
view([ -4 4 ])

for i=4:2:90
    view([ -4 i ])
    pause(.1)
end

rotate3d

%% Figure 12.5

% EEG data from one trial (electrode FCz)
eegdata = squeeze(EEG.data(47,:,10));

% create wavelet
time = -1:1/EEG.srate:1;
f = 6; % frequency of sine wave in Hz
sine_wave = exp(1i*2*pi*f.*time);
s = 4.5/(2*pi*f);
gaussian_win = exp(-time.^2./(2*s^2));
wavelet = sine_wave .* gaussian_win;
% half of the wavelet size, useful for chopping off edges after convolution.
halfwaveletsize = ceil(length(wavelet)/2);

% convolve with data
% compute Gaussian
n_conv = length(wavelet) + EEG.pnts - 1;

fft_w = fft(wavelet,n_conv);
fft_e = fft(eegdata,n_conv);
ift   = ifft(fft_e.*fft_w,n_conv)*sqrt(s)/10; % sqrt... is an empirical scaling factor that works here
wavelet_conv_data = real(ift(halfwaveletsize:end-halfwaveletsize+1));

% create filter and apply to data
% (more on how to interpret this code in a few chapters!)
nyquist       = EEG.srate/2;
transition_width = 0.2; % percent
filter_low    = 4; % Hz
filter_high   = 8; % Hz
ffrequencies  = [ 0 filter_low*(1-transition_width) filter_low filter_high filter_high*(1+transition_width) nyquist ]/nyquist;
idealresponse = [ 0 0 1 1 0 0 ];
filterweights = firls(round(3*(EEG.srate/filter_low)),ffrequencies,idealresponse);
eeg_4to8      = filtfilt(filterweights,1,double(eegdata));

% now plot all the pieces
figure

plot(EEG.times,eegdata)
hold on
plot(EEG.times,wavelet_conv_data,'r','linew',2)
plot(EEG.times,eeg_4to8,'m','linew',2)
set(gca,'xlim',[-200 1200],'ydir','r')
xlabel('Time (ms)'), ylabel('Voltage (\muV)')
legend({'Raw data';'wavelet convolved';'band-pass filtered'},0)

%% Figure 12.6

% make a theta-band-centered wavelet
time   = -1:1/EEG.srate:1;
n_conv = EEG.pnts + length(time) - 1;
n2p1   = floor(n_conv/2)+1; % n2p1 = n/2+1

f = 6;
s = 6/(2*pi*f);
wavelet = exp(2*pi*1i*f.*time) .* exp(-time.^2./(2*s^2));
halfwaveletsize = ceil(length(wavelet)/2);

eegdata = squeeze(EEG.data(47,:,10));

figure

subplot(311)
plot(EEG.times,eegdata)
set(gca,'xlim',[-500 1200])

subplot(323)
fft_w = fft(wavelet,n_conv);
hz    = linspace(0,EEG.srate/2,n2p1);
plot(hz,abs(fft_w(1:n2p1))./max(abs(fft_w(1:n2p1))),'k')
hold on

fft_e = fft(eegdata,n_conv);
hz    = linspace(0,EEG.srate/2,n2p1);
plot(hz,abs(fft_e(1:n2p1))./max(abs(fft_e(1:n2p1))),'r')
set(gca,'xlim',[0 40],'ylim',[0 1.05])
title('individual power spectra')

subplot(324)
plot(hz,abs(fft_e(1:n2p1)).*abs(fft_w(1:n2p1)))
set(gca,'xlim',[0 40])

subplot(313)
plot(EEG.times,eegdata)
hold on
ift = ifft(fft_e.*fft_w,n_conv)*sqrt(s)/10;
plot(EEG.times,real(ift(halfwaveletsize:end-halfwaveletsize+1)),'r')
set(gca,'xlim',[-500 1200])

%% Figure 12.7

% create 10 Hz wavelet (kernel)
time = -EEG.pnts/EEG.srate/2 : 1/EEG.srate : EEG.pnts/EEG.srate/2-1/EEG.srate;
f    = 10; % frequency of sine wave in Hz
s    = 4/(2*pi*f);
wavelet = cos(2*pi*f.*time) .* exp(-time.^2./(2*s^2));

% signal is one sine cycle
timeS  = 0:1/EEG.srate:(1/f); % one cycle is 1/f
signal = sin(2*pi*f.*timeS);

% now zero-pad signal
signal = [ zeros(1,EEG.pnts/2-length(timeS)/2) signal zeros(1,EEG.pnts/2-length(timeS)/2) ];

figure

% plot waves
subplot(321)
plot(wavelet)
set(gca,'xlim',[200 length(time)-200])

subplot(323)
plot(signal)
set(gca,'xlim',[200 length(time)-200])

subplot(325)
plot(conv(wavelet,signal,'same'))
set(gca,'xlim',[200 length(time)-200],'ylim',[-12 12])

% now plot dot products at selected phase lags
subplot(322)
plot(wavelet(round(100/f)-2:end),'r')
hold on
plot(signal)
set(gca,'xlim',[200 length(time)-200])
title([ 'dot product: ' num2str( fix(sum(wavelet(round(100/f)-2:end).*signal(1:end-round(100/f)+3))) ) ])

subplot(324)
plot(wavelet(round(2.3*(100/f)-2):end),'r')
hold on
plot(signal)
set(gca,'xlim',[200 length(time)-200])
title([ 'dot product: ' num2str( fix(sum(wavelet(round(2.3*(100/f)-2):end).*signal(1:end-round(2.3*(100/f)-3))) )) ])

subplot(326)
plot(wavelet,'r')
hold on
plot(signal)
set(gca,'xlim',[200 length(time)-200])
title([ 'dot product: ' num2str( fix(sum(wavelet.*signal)) ) ])

%% end


function y = fun(x)

y=-20*exp(-0.2*sqrt((x(1)^2+x(2)^2)/2))-exp((cos(2*pi*x(1))+cos(2*pi*x(2)))/2)+20+2.71289;


%SE2 Create planar translation and rotation transformation
%
% T = SE2(X, Y, THETA) is an SE(2) homogeneous transformation (3x3)
% representing translation X and Y, and rotation THETA in the plane.
%
% T = SE2(XY) as above where XY=[X,Y] and rotation is zero
%
% T = SE2(XY, THETA) as above where XY=[X,Y]
%
% T = SE2(XYT) as above where XYT=[X,Y,THETA]
%
% See also TRANSL2, ROT2, ISHOMOG2, ISROT2, TRPLOT2.

% Copyright (C) 1993-2015, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for MATLAB (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB 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 Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB.  If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com

function t = se2(a, b, c, varargin)

    opt.deg = false;

    opt = tb_optparse(opt, varargin);

    if length(a) == 3
        x = a(1);
        y = a(2);
        th = a(3);
    elseif length(a) == 2
        x = a(1);
        y = a(2);
        if nargin < 2
            th = 0;
        else
            th = b;
        end
    else
        x = a;
        y = b;
        if nargin < 3
            th = 0;
        else
            th = c;
        end
    end

    if opt.deg
        th = th * pi/180.0;
    end
    cth = cos(th);
    sth = sin(th);
    R = [cth -sth; sth cth];

    t = [R [x; y]; 0 0 1];


function geometry_test2071 ( )

%*****************************************************************************80
%
%% TEST2071 tests TRIANGLE_POINT_DIST_2D;
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    04 December 2010
%
%  Author:
%
%    John Burkardt
%
  ntest = 7;

  ptest = [ ...
     0.25,   0.25; ...
     0.75,   0.25; ...
     1.00,   1.00; ...
    11.00,   0.50; ...
     0.00,   1.00; ...
     0.50, -10.00; ...
     0.60,   0.60 ]';
  t = [ ...
    0.0, 1.0; ...
    0.0, 0.0; ...
    1.0, 0.0 ]';

  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST2071\n' );
  fprintf ( 1, '  For a triangle in 2D,\n' );
  fprintf ( 1, '  TRIANGLE_POINT_DIST_2D computes the distance\n' );
  fprintf ( 1, '  to a point;\n' );

  r8mat_transpose_print ( 2, 3, t, '  Triangle vertices:' );

  fprintf ( 1, '\n' );
  fprintf ( 1, '       P       DIST\n' );
  fprintf ( 1, '\n' );

  for i = 1 : ntest

    p(1:2,1) = ptest(1:2,i);

    dist = triangle_point_dist_2d ( t, p );

    fprintf ( 1, '  %10f  %10f  %10f\n', p(1:2,1), dist );

  end

  return
end


function T=dewPointTemp4Pres(p,algChoice)
%%DEWPOINTTEMP4PRES For a given partial vapor pressure of water, find the
%                   dew point temperature. that is the temperature at which
%                   the air would be saturated with water, in equilibrium.
%
%INPUTS: p The (partial) pressure of the water vapor for which the dew
%          point temperature is desired in units of Newtons per square
%          meter (Pascals).
% algChoice An optional parameter specifying the algorithm to use. The
%          choices are
%          0 The corrected version of the Clausius-Clapeyron equation for
%            use over land or in the upper air.
%          1 The empirical Magnus-type equation for use over water or in
%            the upper air (-40C to 50C temperature).
%          2 The empirical Magnus-type equation for use over ice (-80C to
%            0C temperature)
%          If algChoice is omitted, then the default value of 0, the
%          corrected Clausius-Clapeyron equation is used. Choice 0 is taken
%          from [1]. Choices 1 and 2, are taken from [2].
%
%As is the case in the function dewPointPres4Temp, which is the inverse of
%this function, formulae 0 is from [1], where the numerical inversion
%method given in equations 44 and 45 in Section 5 of the paper are used.
%
%Formulas 1 and 2 are obtained by solving for the inverse of the Magnus
%approximations given in [2]. Simple, explicit solutions for the function
%inverses can be found.
%
%Note that the function dewPointPres4Temp is the inverse of this function.
%
%REFERENCES:
%[1] D. Koutsoyiannis, "Clausius-Clapeyron equation and saturation vapour
%    pressure: simple theory reconcided with practice," European Journal of
%    Physics, vol. 33, no. 2, pp. 295-305, Mar. 2012.
%[2] O. A. Alduchov and R. E. Eskridge, "Improved Magnus Form Approximation
%    of Saturation Vapor Pressure," Journal of Applied Meteorology, vol.
%    35, no. 4, pp. 601-609, Apr. 1996.
%
%February 2014 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(nargin<2||isempty(algChoice))
    algChoice=0;
end

switch(algChoice)
    case 1
        %This evaluates the inverse of Equation 21 in the Alduchov and
        %Eskridge paper.

        p=p/100;%Convert to hectoPascals.
        logRat=log(p/6.1094);

        T=-243.04*logRat./(logRat-17.625);
        %Convert the temperature to degrees Kelvin.
        T=T-Constants.absoluteZero;
    case 2
        %This evaluates the inverse of Equation 23 in the Alduchov and
        %Eskridge paper for the saturation temperature over ice.

        p=p/100;%Convert to hectoPascals.
        logRat=log(p/6.1121);

        T=-273.86*logRat./(logRat-22.587);
        %Convert the temperature to degrees Kelvin.
        T=T-Constants.absoluteZero;
    otherwise
        %The inverse of Equation 23 in the Koutsoyiannis paper is not
        %simple to find. However, the paper provides iterative solutions in
        %the form of Equations 44 and 45 for specific values related to
        %water. For general values, the initial estimate in Equation 44 is
        %T0/T=1+1/(alpha/(R*T0)-(cL-cP)/R)*ln(p0/p);
        %and the iteration to refine the ratio is
        %T0/T_{new}=1+(R*T0/alpha)*ln(p0/p)+((cL-cP)/R)/(alpha/(R*T0))*ln(T0/T_{old})
        %where R is the specific gas constant of water vapor with units of
        %J/(kg*K), T0 is the temperature at the triple point of water, with
        %units of Kelvin, p0 is the pressure at the triple point of water
        %in hecoPascals, cP and cL are the specific heat of water vapor and
        %liquid water at constant pressure with units of J/(kg*K), and
        %alpha=L0+(cL-cP)*T0, where L0 is the latent heat of water for a
        %constant pressure at the triple point with units of J/kg.
        %
        %In the implementation here, the specific numerical from the paper
        %are used. This means that alpha has been tweaked a little bit to
        %better match their real data.

        numIter=27;

        p0=6.11657*100;%The pressure at the triple point of water in Pascals.
        T0=273.16;%The temperature at the triple point of water in Kelvin.

        LPRat=log(p0./p);

        TRat=1+1/(24.921-5.06)*LPRat;
        for curIter=1:numIter
            TRat=1+(1/(24.921))*LPRat+(5.06/24.921).*log(TRat);
        end
        T=T0./TRat;
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 X = circle_graph_layout(G,radius)
% CIRCLE_LAYOUT Layout the vertices of a graph on a circle
%
% X = circle_layout(G) generates a layout of graph with vertices uniformly
% placed on the circle.  This function does no interesting layout and just
% places the vertices around the circle in order.
%
% X = circle_layout(G,radius) places the vertices with a radius other than
% 1.0.
%
% Example:
%   G = cycle_graph(6);
%   X = circle_graph_layout(G);
%   gplot(G,X);

% David F. Gleich
% Copyright, Stanford University, 2008

%% History
%  2008-09-25: Initial coding
%%

if ~exist('radius','var') || isempty(radius), radius = 1.0; end;
pi = 3.14159;
n = num_vertices(G);
X = zeros(n,2);
X(:,1) = radius*cos( (0:n-1)'*2*pi/n );
X(:,2) = radius*sin( (0:n-1)'*2*pi/n );