www.omatrix.com





:: O-Matrix ::
> Overview
> Examples
> Performance
> Analysis Functions
> Data Visualization
> The O-Matrix Language
> Data Manipulation/IO
> Application Development
> Using Matlab m-files




CONJDIRX.OMS

Script File:
# Description:
# Minimizes Rosenbrock's function using the conjugate directions method.
#
clear
# Rosenbrock's function
function rosen(xy) begin
	x = xy(1)
	y = xy(2)
	return 100 * (y - x^2)^2 + (1 - x)^2
end
#
# Minimize Rosenbrock's function.
# initial value of x and y
xyin = { - 1.2, 1.}
# convergence criteria 
step = {1e-3, 1e-3}
# maximum number of iterations
mitr = 40
# level of tracing
level = 1
# value of x and y at all iterations
xyall = conjdir(function rosen, xyin, step, mitr, level)
#
# Evaluate Rosenbrock's function on an x - y grid.
# size of grid
N = 21
# grid of values for x in [ - 1.5, 1.5]
x = 2 * (seq(N) - 1) * 1.5 / (N - 1) - 1.5
# grid of values for y in [ - 1.5, 1.5]
y = x'
# value of x for each matrix element
X = fillcols(x, N)
# value of y for each matrix element
Y = fillrows(y, N)
# value of Rosenbrock's function
R = 100 * (Y - X^2)^2 + (1 - X)^2
#
# Plot results.
# place a square viewport in the upper 
# seven tenths of the plot window.
gaspect(1. / .7)
gaddview(0., .3, 1., .7);
# contour Rosenbrock's function in this viewport
# title for this viewport
gtitle("Rosenbrock's Function")
# axis limits, major and minor divisions
gxaxis("linear", - 1.5, 1.5, 3, 5)
gyaxis("linear", - 1.5, 1.5, 3, 5)
# grid spacing proportional to cube of level value
Level = max(R) * (seq(10) / 10.)^2
# contour Rosenbrock's function
contour(R, Level, x, y)
#
# Place the iterates in red on the contour plot.
gcolor("red")
# plot the iterates with + 
gplot( xyall.row(1)' , xyall.row(2)', "plus" )
#
# Plot Rosenbrock's function versus x for each iterate.
# viewport for this plot
gaddview(0., 0., 1. , .35);
# axis limits, major and minor divisions
gxaxis("linear", - 1.5, 1.5, 3, 5)
gyaxis("linear", 0, 3, 3, 5)
# color for plot
gcolor("red")
# define the vector F
F = fill(0., coldim(xyall), 1)
for i = 1 to coldim(xyall) begin
	# Rosenbrock's function
	F(i) = rosen(xyall.col(i))
end
# plot function for each iterate
gplot(xyall.row(1)', F, "plus")
gtitle("Function value")
	  

Output:
analytic derivative              =  [    1.00000 ,    4.00000 ,    9.00000 ] 
f(xitr)=    2.79988 
f(xitr)=    2.15540 
f(xitr)=    1.63606 
f(xitr)=    0.43527 
f(xitr)=    0.27041 
f(xitr)=    0.17625 
f(xitr)=    0.10513 
f(xitr)=    0.05955 
f(xitr)=    0.03135 
f(xitr)=    0.01447 
f(xitr)=    0.00541 
f(xitr)=    0.00138 
f(xitr)=    0.00011 
f(xitr)=    0.00000 
f(xitr)=    0.00000 
	  


Company |  Products |  Showcase |  Support |  Ordering
Copyright© 1994-2009 Harmonic Software Inc. - All rights reserved.