Skip to content

Instantly share code, notes, and snippets.

@halbuki
Last active September 1, 2022 14:41
Show Gist options
  • Save halbuki/634c41f91eb19f22f4d7167e955d89b2 to your computer and use it in GitHub Desktop.
Save halbuki/634c41f91eb19f22f4d7167e955d89b2 to your computer and use it in GitHub Desktop.
Excel Lambda functions for numerical analyses
/* ROOTS OF FUNCTIONS */
BISEC = LAMBDA(f, lbound, ubound, [prec],
LET(
c, (lbound + ubound) / 2,
fl, f(lbound), fu, f(ubound), fc, f(c),
IF(
ABS(fc) < MAX(prec, 1E-15),
c,
IF(
SIGN(fl) = SIGN(fc),
BISEC(f, c, ubound, prec),
BISEC(f, lbound, c, prec)
)
)
)
);
FALSEPOS = LAMBDA(f, lbound, ubound, [prec],
LET(
fl, f(lbound), fu, f(ubound),
c, ubound - fu * (lbound - ubound) / (fl - fu),
fc, f(c),
IF(
ABS(fc) <= MAX(prec, 0.000001),
c,
IF(
SIGN(fl) = SIGN(fc),
FALSEPOS(f, c, ubound, prec),
FALSEPOS(f, lbound, c, prec)
)
)
)
);
NR = LAMBDA(f, x0, [prec],
LET(
fx, f(x0),
IF(
ABS(fx) <= MAX(prec, 0.000001),
x0,
LET(
xr, x0 - fx / DF(f)(x0),
NR(f, xr, prec)
)
)
)
);
/* LINEAR ALGEBRAIC EQUATIONS */
LINSOLVE = LAMBDA(_coefmat, _constmat, MMULT(MINVERSE(_coefmat), _constmat));
/* CURVE FITTING */
POLEST = LAMBDA(listY, listX, n, LINEST(listY, POWER(listX, TRANSPOSE(SEQUENCE(n, 1, 1, 1)))));
CPOLINT = LAMBDA(x, coeffs,
LET(
n, COUNT(coeffs),
MMULT(
POWER(x, TRANSPOSE(SEQUENCE(n, 1, n - 1, -1))) * coeffs,
SEQUENCE(n, 1, 1, 0)
)
)
);
DPOLINT = LAMBDA(listY, listX, n, x,
MMULT(
POWER(x, TRANSPOSE(SEQUENCE(n + 1, 1, n, -1))) * POLEST(listY, listX, n),
SEQUENCE(n + 1, 1, 1, 0)
)
);
/* NUMERICAL DIFFERENTATION */
DF = LAMBDA(f,
LAMBDA(x,
LET(
dx, 0.00001,
(f(x + dx) - f(x - dx)) / (2 * dx)
)
)
);
/* EXTREMA */
LOCALEXT = LAMBDA(f, lbound, ubound,
LET(
extx, BISEC(DF(f), lbound, ubound, 0.00001),
exty, f(extx),
MAKEARRAY(1, 2, LAMBDA(ir, ic, CHOOSE(ic, extx, exty)))
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment