Instantly share code, notes, and snippets.

View complex_step_example.m
% Complex step example. Requires Symbolic Math Toolbox.
format long
syms x; f = atan(x)/(1+exp(-x^2))
% Derivative at $a = 2$:
a = 2; fd = double(subs( diff(f), a))
% Convert symbolic function to MATLAB function.
f = matlabFunction(f);
View complex_step.m
function fd = complex_step(f,x,h)
%COMPLEX_STEP Complex step approximation to derivative.
% fd = COMPLEX_STEP(f,x,h) computes the complex step approximation
% fd to the derivative of f at x, using step h (default 1e-100).
if nargin < 3, h = 1e-100; end
fd = imag( f(x + sqrt(-1)*h) )/h;
View ncm_compare.m
%NCM_COMPARE
% M-file to carry out experiment in "Explicit Solutions to Correlation
% Matrix Completion Problems, with an Application to Risk Management and
% Insurance" by Dan I. Georgescu, Nicholas J. Higham and Gareth W. Peters.
C = [%
1 0.25 0.6 0.55 0.65 0 0.4 0.6 0.2 0.3
0.25 1 0 0 0 0 NaN NaN NaN NaN
0.6 0 1 0.75 0.75 0 NaN NaN NaN NaN
0.55 0 0.75 1 0.5 0 NaN NaN NaN NaN
View edelman.m
function A = edelman
%EDELMAN Alan Edelman's matrix for which det is computed as the wrong integer.
% A = EDELMAN is a 27-by-27 matrix of 1s and -1s for which the
% MATLAB det function returns an odd integer, though the exact
% determinant is an even integer.
A = [%
1 1 1 1 -1 -1 -1 1 1 -1 1 -1 -1 1 -1 1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1
1 -1 -1 1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 1 -1 1 1 1
-1 1 -1 -1 1 1 1 1 1 -1 1 -1 1 1 1 1 -1 -1 1 -1 -1 1 1 -1 1 1 -1
View rel_err_formula.m
%REL_ERR_FORMULA Compare two formulas for relative error.
n = 500;
a = 3;
x = a*ones(n,1,'single');
y = zeros(size(x));
d = eps(single(a));
for i=1:n
i-n/2
View funm_randomized
function F = funm_randomized(A,fun)
%FUNM_RANDOMIZED Evaluate general matrix function using
% randomized approximate diagonalization method of Davies (2007).
tol = 8*eps(A(1,1)); % Tolerance for single or double precision A.
E = randn(size(A));
[V,D] = eig(A + (tol*norm(A,'fro')/norm(E,'fro'))*E);
F = V*diag(fun(diag(D)))/V;