-
Notifications
You must be signed in to change notification settings - Fork 5
/
Robustlsqcurvefit_test.m
119 lines (90 loc) · 3.51 KB
/
Robustlsqcurvefit_test.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
classdef Robustlsqcurvefit_test < matlab.unittest.TestCase
properties
Unit = @robustlsqcurvefit;
TrueParams;
X;
Y;
Noise;
ModelFun;
X0
Options;
end
methods (TestClassSetup)
function setupData(testCase)
param = [0.5, 1.25];
x = linspace(0, 6, 100);
modelFun = @(param, x) param(1)*exp(param(2) * x);
noise = sin(x).^2 .* randn(size(x));
y = modelFun(param, x) + noise;
y(10) = 200;
y(50) = 400;
options = optimset(@lsqcurvefit);
options.Display = 'off';
testCase.TrueParams = param;
testCase.X = x;
testCase.Y = y;
testCase.Noise = noise;
testCase.ModelFun = modelFun;
testCase.X0 = [1, 1];
testCase.Options = options;
end
end
methods (Test)
function testSimpleCall(testCase)
x = testCase.X;
y = testCase.Y;
fun = testCase.ModelFun;
x0 = testCase.X0;
options = testCase.Options;
outputs = testCase.Unit(fun, x0, x, y, [], [], [], options);
testCase.verifyNumElements(outputs, 2);
testCase.verifyLessThan(norm(outputs - testCase.TrueParams).^2, 1e-4);
end
function testFullCall(testCase)
x = testCase.X;
y = testCase.Y;
fun = testCase.ModelFun;
x0 = testCase.X0;
options = testCase.Options;
lb = [0.1, 1];
ub = [ 1, 2];
weightMethod = 'bisquare';
outputs = testCase.Unit(fun, x0, x, y, lb, ub, weightMethod, options);
testCase.verifyNumElements(outputs, 2);
testCase.verifyLessThan(norm(outputs - testCase.TrueParams).^2, 1e-4);
end
function testInputValidation(testCase)
x = testCase.X;
y = testCase.Y;
fun = testCase.ModelFun;
x0 = testCase.X0;
options = testCase.Options;
% Wrong WeightMethod Input
actual = @() testCase.Unit(fun, x0, x, y, [], [], 'foo', options);
testCase.verifyError(actual, 'MATLAB:unrecognizedStringChoice');
% Wrong lb Input
actual = @() testCase.Unit(fun, x0, x, y, 'foo', [], [], options);
testCase.verifyError(actual, 'MATLAB:robustlsqcurvefit:invalidType');
% Correct Input
actual = @() testCase.Unit(fun, x0, x, y, [], [], 'ols', options);
testCase.verifyWarningFree(actual);
end
function testPrecision(testCase)
x = testCase.X;
y = testCase.Y;
fun = testCase.ModelFun;
x0 = testCase.X0;
options = testCase.Options;
est = testCase.Unit(fun, x0, x, y, [], [], [], options);
testCase.verifyLessThan(norm(est - testCase.TrueParams).^2, 1e-4);
end
function canUseLessParameters(testCase)
x = testCase.X;
y = testCase.Y;
fun = testCase.ModelFun;
x0 = testCase.X0;
outputs = testCase.Unit(fun, x0, x, y);
testCase.verifyNumElements(outputs, 2);
end
end
end