/ concept-collection / barycentric-rational
concept-collection / barycentric-rational
barycentric-rational / src / matlab / lib / testfun.m
27 lines · 837 BCodeBlameHistory
5392320Interactive illustration of Floater-Hormann barycentric rational interpolationJeremy Magland 1function [y, dy] = testfun(name, x, expr)
2%TESTFUN The functions interpolated in Section 5 of the paper, plus a custom one.
3%
4% [y, dy] = testfun(name, x, expr) returns f(x) and f'(x). The derivative is
5% only used for the clamped end conditions of the C^2 cubic spline that the
6% paper compares against in Tables 3 and 4.
8switch name
9 case 'runge'
10 y = 1 ./ (1 + x.^2);
11 dy = -2 * x ./ (1 + x.^2).^2;
12 case 'sine'
13 y = sin(x);
14 dy = cos(x);
15 case 'abs'
16 y = abs(x);
17 dy = sign(x);
18 case 'custom'
19 y = evalexpr(expr, x);
20 h = 1e-6 * max(1, max(abs(x(:))));
21 dy = (evalexpr(expr, x + h) - evalexpr(expr, x - h)) / (2 * h);
22 otherwise
23 error('testfun: unknown function ''%s''', name);
24end
25y = reshape(y, size(x));
26dy = reshape(dy, size(x));
27end