Skip to content

Instantly share code, notes, and snippets.

View higham's full-sized avatar

Nick Higham higham

View GitHub Profile
@higham
higham / rel_err_formula.m
Last active August 6, 2017 13:23
MATLAB script to compare two formulas for relative error.
%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
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;
@higham
higham / ncm_compare.m
Created February 3, 2018 14:28
MATLAB expriment for correlation matrix compleition
%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
@higham
higham / edelman.m
Last active June 14, 2018 08:41
MATLAB function producing Alan Edelman's matrix for which det is computed as the wrong integer.
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
@higham
higham / complex_step.m
Created April 21, 2018 12:58
Complex step approximation to derivative.
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;
(use-package hydra
:config
(global-set-key (kbd "C-x m") 'hydra-major/body)
(global-set-key (kbd "<f3>") 'hydra-bib-etc/body)
(global-set-key (kbd "C-<f3>") 'hydra-dired/body)
)
(defhydra hydra-major (:color blue :columns 4)
"major-mode"
("b" bibtex-mode "bibtex")
@higham
higham / complex_step_example.m
Last active November 2, 2021 15:45
Complex step example
% 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);