Repository navigation
Expand file tree
/
Copy pathmain_polynomials.m
More file actions
57 lines (51 loc) · 1.76 KB
/
Copy pathmain_polynomials.m
File metadata and controls
57 lines (51 loc) · 1.76 KB
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
close all;
clear all;
addpath(genpath('src'));
addpath('Initialization');
file_name_load = "data/IEEETIE_machine2_smooth.mat";
k_skip = 5;
mae_tol = 0.01;
n_iter = 100;
ns = [3, 3, 3];
ms = [0, 1, 2];
opts = optimoptions('lsqcurvefit', 'Display', 'off', 'MaxFunctionEvaluations', 1e4);
vars = load(file_name_load);
data = grid_to_data(vars.grid, k_skip);
for i_val = 1:data.n_val
results = array2table(nan(data.n_seg,6), 'VariableNames', {'seg','m','n','rmse','mae','data_n'});
for i_seg = 1:data.n_seg
i_seg
T = array2table(nan(length(ns),4), 'VariableNames', {'m','n','rmse','mae'});
[x, y] = data.extract_data(i_seg, i_val);
fitters = cell(length(ns), 1);
for i_n = 1:length(ns)
fitter = Fitter(ns(i_n), ms(i_n), opts);
for i_iter = 1:n_iter
p0 = randn(fitter.p_size, 1);
[p, mae] = fitter.fit(p0, x, y);
if i_iter == 1 || mae < mae_best
mae_best = mae;
p_best = p;
end
if mae <= mae_tol
break
end
end
fitter.set_p(p_best);
y_pred = fitter.predict(x);
fitters{i_n} = fitter;
T.m(i_n) = ms(i_n);
T.n(i_n) = ns(i_n);
T.rmse(i_n) = sqrt(mean((y_pred - y).^2));
T.mae(i_n) = max(abs(y_pred-y));
if T.mae(i_n) <= mae_tol
break
end
end
[~, i_min] = min(T.mae);
fitter = fitters{i_min};
[~, name] = data.i_to_val_name(i_val);
file_name = "data/fitter/" + name + "_" + string(i_val) + "_" + string(i_seg) + "_" + string(k_skip) + ".mat";
save(file_name, 'fitter');
end
end