source: mystic/examples_other/sam_cg_zimmermann.py @ 855

Revision 855, 1.4 KB checked in by mmckerns, 5 months ago (diff)

updated copyright to 2016

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2#
3# Author: Patrick Hung (patrickh @caltech)
4# Copyright (c) 1997-2016 California Institute of Technology.
5# License: 3-clause BSD.  The full license text is available at:
6#  - http://mmckerns.github.io/project/mystic/browser/mystic/LICENSE
7"""
8See test_zimmermann.py.
9
10This one uses Scipy's CG (Polak-Ribiere) plus matlab viz.
11"""
12
13import sam
14from test_zimmermann import *
15from scipy.optimize import fmin_cg
16import numpy
17from mystic.tools import getch
18
19def draw_contour():
20    import numpy
21
22    x, y = numpy.mgrid[-.51:7.5:0.05,-.51:7.5:0.05]
23    c = 0*x
24    s,t = x.shape
25    for i in range(s):
26       for j in range(t):
27          xx,yy = x[i,j], y[i,j]
28          c[i,j] = CostFunction([xx,yy])
29
30
31    sam.putarray('X',x)
32    sam.putarray('Y',y)
33    sam.putarray('C',c)
34
35    sam.verbose()   
36    sam.eval("[c,h]=contourf(X,Y,log(C*20+1)+2,100);set(h,'EdgeColor','none')")
37    sam.eval("title('Zimmermann''s Corner. Min at 7,2')")
38    sam.eval('hold on')
39
40
41def run_once(x0,x1):
42    sol = fmin_cg(CostFunction, [x0, x1], retall = True, full_output=1)
43    xy = numpy.asarray(sol[-1])
44    sam.putarray('xy',xy)
45    sam.eval("plot(xy(:,1),xy(:,2),'w-','LineWidth',2)")
46    sam.eval("plot(xy(:,1),xy(:,2),'wo','MarkerSize',6)")
47    return sol
48   
49if __name__ == '__main__':
50    draw_contour()
51    run_once(1,3)
52    run_once(4,2)
53    run_once(7,0.1)
54    run_once(6,4)
55    run_once(0,7)
56
57    getch("Press any key to quit")
58
59# end of file
Note: See TracBrowser for help on using the repository browser.