text:
function sim = gaussianKernel(x1, x2, sigma)
%RBFKERNEL returns a radial basis function kernel between x1 and x2
%   sim = gaussianKernel(x1, x2) returns a gaussian kernel between x1 and x2
%   and returns the value in sim

% Ensure that x1 and x2 are column vectors
x1 = x1(:); x2 = x2(:);

% You need to return the following variables correctly.
sim = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return the similarity between x1
%               and x2 computed using a Gaussian kernel with bandwidth
%               sigma
%
%

sim = exp(-sum((x1-x2).^2)/(sigma.^2*2));

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

end

author: Ayatans

repo: Machine-Learning-homework

lm_label:
1. YES
2. YES


text:
function sim = gaussianKernel(x1, x2, sigma)
%RBFKERNEL returns a radial basis function kernel between x1 and x2
%   sim = gaussianKernel(x1, x2) returns a gaussian kernel between x1 and x2
%   and returns the value in sim

% Ensure that x1 and x2 are column vectors
x1 = x1(:); x2 = x2(:);

% You need to return the following variables correctly.
sim = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return the similarity between x1
%               and x2 computed using a Gaussian kernel with bandwidth
%               sigma
%
%

sim = exp(-sum((x1-x2).^2)/2/(sigma^2));

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

end

author: zzlyw

repo: machine-learning-exercises


text:
function sim = gaussianKernel(x1, x2, sigma)
%RBFKERNEL returns a radial basis function kernel between x1 and x2
%   sim = gaussianKernel(x1, x2) returns a gaussian kernel between x1 and x2
%   and returns the value in sim

% Ensure that x1 and x2 are column vectors
x1 = x1(:); x2 = x2(:);

% You need to return the following variables correctly.
sim = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return the similarity between x1
%               and x2 computed using a Gaussian kernel with bandwidth
%               sigma
%
%

sim = exp((- sum((x1 - x2) .^ 2)) / (2 * (sigma ^ 2)));

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

end

author: UtkarshPathrabe


text:
%% Project: 1-D Finite Difference Method
%
% The purpose of this project is to introduce the finite difference. We use
% 1-D Poisson equation in (0,1) with Dirichlet boundary condition
%
% $$ - u'' = f \, (0,1), \quad u(0) = a, u(1) = b. $$

%% Step 1: Generate a grid
%
% Generate a vector representing a uniform grid with size h of (0,1).
N = 10;
x = linspace(0,1,N);
plot(x,0,'b.','Markersize',16)

%% Step 2: Generate a matrix equation
%
% Based on the grid, the function $u$ is discretized by a vector |u|. The
% derivative $u', u''$ are approximated by centeral finite difference:
%
% $$ u'(x_i) \approx \frac{u(i+1) - u(i-1)}{2h} $$
%
% $$ u''(x_i) \approx \frac{u(i-1) - 2*u(i) + u(i-1)}{h^2} $$
%
% The equation $-u''(x) = f(x)$ is discretized at $x_i, i=1,...,N$ as
%
% $$ \frac{-u(i-1) + 2*u(i) - u(i-1)}{h^2} \quad = f(i) $$
%
% where $f(i) = f(x_i)$. These linear equations can be written as a matrix
% equation |A*u = f|, where |A| is a tri-diagonal matrix |(-1,2,-1)/h^2|.
n = 5;
e = ones(n,1);
A = spdiags([e -2*e e], -1:1, n, n);
display(full(A));
%%
% We use spdiags to speed up the generation of the matrix. If using diag,
% ....

%% Stpe 3: Modify the matrix and right hand side to impose boundary condition
%
% The discretization fails at boundary vertices since no nodes outside the
% interval. Howevery the boundary value is given by the problem: |u(1) = a,
% u(N) = b|.
%
% These two equations can be included in the matrix by changing |A(1,:) =
% [1, 0 ..., 0] and A(:,N) = [0, 0, ..., 1] and |f(1) = a/h^2, f(N) =
% b/h^2|.
A(1,1) = 1;
A(1,2) = 0;
A(n,n) = 1;
A(n,n-1) = 0;
display(full(A));

%% Step 4: Test of code
[u,x] = poisson1D(0,1,5);
plot(x,sin(x),x,u,'r*');
legend('exact solution','approximate solution')
%%
% We test ... The result seems correct since the computed solution fits
% well on the curve of the true solution.

%% Step 5: Check the Convergence Rate
err = zeros(4,1);
h = zeros(4,1);
for k = 1:4
    [u,x] = poisson1D(0,1,2^k+1);
    uI = sin(x);
    err(k) = max(abs(u-uI));
    h(k) = 1/2^k;
end
display(err)
loglog(h,err,h,h.^2);
legend('error','h^2');
axis tight;
%%
% Since these two lines have the same lope, the err decays like h^2.

repo: Aero-matlab


text:
function y = sigmoid(x,c,a)
% sigmoid evaluates a simple sigmoid function along x:
%
%         ______1________
%     y =        -a(x-c)
%          1 + e^
%
%% Syntax
%
% y = sigmoid(x)
% y = sigmoid(x,c)
% y = sigmoid(x,c,a)
%
%% Description
%
% y = sigmoid(x) generates a sigmoid function along x.
%
% y = sigmoid(x,c) makes a sigmoid that scaled from zero to one, where
% c corresponds to the x value where y = 0.5. If c is not specified, a
% default value of c = 0 is assumed.
%
% y = sigmoid(x,c,a) specifies a, the rate of change. If a is close to
% zero, the sigmoid function will be gradual. If a is large, the sigmoid
% function will have a steep or sharp transition. If a is negative, the
% sigmoid will go from 1 to zero. A default value of a = 1 is assumed if
% a is not declared.
%
%% Example 1
% A simple sigmoid:
%
% x = -10:.01:10;
% plot(x,sigmoid(x))
%
%% Example 2:
% Make a sigmoid function along x = 1 to 100, such that y(x=60) = 0.5:
%
% x = 1:100;
% y = sigmoid(x,60);
% plot(x,y,'b','linewidth',2)
%
% Now do the same thing as above, but make the transition more gradual:
%
% y2 = sigmoid(x,60,0.1);
% hold on
% plot(x,y2,'r','linewidth',2)
% legend('default a = 1','a = 1/10')
% legend boxoff
%
%% Author Info:
% Chad Greene, May 28, 2015.
% http://www.chadagreene.com

%% Parse Inputs:

narginchk(1,3)
if nargin<3
    a = 1;
else
    assert(isscalar(a)==1,'a must be a scalar.')
end

if nargin<2
    c = 0;
else
    assert(isscalar(c)==1,'c must be a scalar.')
end

%% Perform mathematics:

y = 1./(1 + exp(-a.*(x-c)));

author: buzsakilab

repo: buzcode

save_path: github-repos/MATLAB/buzsakilab-buzcode


text:
function sim = gaussianKernel(x1, x2, sigma)
%RBFKERNEL returns a radial basis function kernel between x1 and x2
%   sim = gaussianKernel(x1, x2) returns a gaussian kernel between x1 and x2
%   and returns the value in sim

% Ensure that x1 and x2 are column vectors
x1 = x1(:); x2 = x2(:);

% You need to return the following variables correctly.
sim = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return the similarity between x1
%               and x2 computed using a Gaussian kernel with bandwidth
%               sigma
%
%

sim = exp( -1 * sum((x1 - x2).^2) / (2 * (sigma ^ 2)));

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

end

author: gopaczewski

repo: coursera-ml


text:
function y=hav(x)
%%HAV Compute the haversine of x. The haversine arises in navigation
%     problems. As in [1], it is just sin(x/2).^2. Haversines tend to arise
%     in navigation problems.
%
%INPUTS: x A numeric matrix.
%
%OUTPUTS: y The haversine of x. y has the same dimensionality as x.
%
%REFERENCES:
%[1] Weisstein, Eric W. "Haversine." From MathWorld--A Wolfram Web
%    Resource. https://mathworld.wolfram.com/Haversine.html
%
%October 2022 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

y=sin(x/2).^2;

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.

author: USNavalResearchLaboratory

repo: TrackerComponentLibrary


text:
function area=hypersphereArea(r,numDim)
%%HYPERSPHEREAREA Determine the surface area of a hypersphere of a given
%                 radius in a given number of dimensions.
%
%INPUTS:     r The radius of the hypersphere. r>0.
%       numDim The number of dimensions of the hypersphere. numDim>=1. One
%              dimensions is a line, two a circle, three a sphere, etc.
%
%OUTPUTS: area The area of the hypersphere.
%
%The surface area of a hypersphere is given in terms of the gamma function
%in [1].
%
%REFERENCES:
%[1] S. Li, "Concise formulas for the area and volume of a hyperspherical
%    cap," Asian Journal of Mathematics and Statistics, vol. 4, no. 1, pp.
%    66-70, 2011.
%
%October 2016 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

area=2*pi^(numDim/2)/gamma(numDim/2)*r^(numDim-1);

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.


text:
function sim = gaussianKernel(x1, x2, sigma)
%RBFKERNEL returns a radial basis function kernel between x1 and x2
%   sim = gaussianKernel(x1, x2) returns a gaussian kernel between x1 and x2
%   and returns the value in sim

% Ensure that x1 and x2 are column vectors
x1 = x1(:); x2 = x2(:);

% You need to return the following variables correctly.
sim = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return the similarity between x1
%               and x2 computed using a Gaussian kernel with bandwidth
%               sigma
%
%

sim = exp(-sum((x1 - x2).^2)/(2 * sigma^2));

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

end

author: rmarquis

repo: coursera-machinelearning


text:
function sim = gaussianKernel(x1, x2, sigma)
%RBFKERNEL returns a radial basis function kernel between x1 and x2
%   sim = gaussianKernel(x1, x2) returns a gaussian kernel between x1 and x2
%   and returns the value in sim

% Ensure that x1 and x2 are column vectors
x1 = x1(:); x2 = x2(:);

% You need to return the following variables correctly.
sim = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return the similarity between x1
%               and x2 computed using a Gaussian kernel with bandwidth
%               sigma
%
%

upper = sum((x1 .- x2) .^ 2);
sim = exp(-upper / (2 * sigma ^ 2));

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

end

author: ecmadao

repo: Coding-Guide