-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathau_levmarq_test.m
68 lines (56 loc) · 1.28 KB
/
au_levmarq_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
function logout = au_levmarq_test
% au_levmarq_test Unit test for au_levmarq
% Constant in Rosenbrock function
K = 10;
% Starting point, around the corner from global min
x=[-1.9, 2];
%% Set GX = 0 to do without the graphical display
GX = 1;
if GX
disp('plotting')
%% plot function
range = linspace(-4,4,97);
[xx,yy] = meshgrid(range);
zz = xx;
for i=1:numel(xx)
e = au_rosenbrock([xx(i) yy(i)], K);
zz(i) = sum(e.^2);
end
clf
contour(xx,yy,zz,2.^[-1 2 4 6:18])
hold on
axis xy
drawnow
% GX.ax = get(h,'parent');
end
% define optimization function
f = @(x) au_rosenbrock(x, K);
% check it at some values
%[e,J] = f(x);
%f([1,1]);
% call LM
opts = au_levmarq('opts');
opts.Display = 'iter';
if GX
opts.IterStartFcn = @(x) plot_fun(x);
end
opts.USE_LINMIN = 1;
au_levmarq(f, x, opts);
opts.IterStartFcn = [];
opts.Display = 'final+';
au_levmarq(f, x, opts);
opts.Display = 'final';
au_levmarq(f, x, opts);
opts.Display = 'none';
tic
opts.USE_LINMIN = 1;
au_levmarq(f, x, opts);
fprintf('Time with linmin %g sec\n', toc);
tic
opts.USE_LINMIN = 0;
au_levmarq(f, x, opts);
fprintf('Time without linmin %g sec (should be much faster as J is tiny)\n', toc);
function x = plot_fun(x)
h = plot(x(1),x(2), 'b.');
% set(h,'marker', 'o', 'markersize', 10, 'color', 'r');
drawnow