// usual aids
clear; // can we save A across clears?
lines(0);
funcprot(2);

// The iterate function assumes A and X are defined
function [] = iterate()
	// Assumes A is a 2x2 symmetric matrix
	// And X starts out as [1 ; 0]
	// and searches for the maximum x^TAx on the circle x^T x = 1
	if ~exists('A') then
		error('The matrix A is not defined');
	end
	if ~exists('X') then
		error('The column vector X is not defined');
	end
	//global('X');
	n = size(A,1);
	for i = 1:10,
		X = A*X;
		N = norm(X);
		X = X/N;
		if n == 2,
			printf("N = %f, X = [%f; %f], i = %d\n", N, X(1,1), X(2,1), i);
		else
			printf("N = %f, X = [%f; %f; %f], i = %d\n", ..
				N, X(1,1), X(2,1), X(3,1), i);
		end;
	end;
	[X, N] = resume(X, N); // local to global?
endfunction

// The plotquad function assumes A is sysmetric.
function [] = plotquad(A)
//	if ~exists('A') then
//		error('The matrix A is not defined');
//	end
// Plot Quad plots the conic section for a given 2x2 symmetric matrix
// It assumes that the matrix is A and it is already defined.
// It plots black curves
	theta = 0:%pi/50:2*%pi; // %pi is scilab for the constant pi
	n = size(theta,2); // theta is a 1 x n row vector
	x = zeros(theta); // x and y start as zero vectors
	y = zeros(theta);
	for i = 1:n,
		angle = theta(i); // this angle
		c = cos(angle);
		s = sin(angle);
		X = [ c, s ];
		r = sqrt(1/abs(X*A*X')); // formula from section 4
		x(i) = r*c; // r cos(theta)
		y(i) = r*s;
	end;
	plot(x,y,'k-');
	a = gca(); a.isoview="on";
	ti="Conic for A = ["+string(A(1,1))+" "+string(A(1,2)) +"; ";
	ti= ti + string(A(2,1))+" "+string(A(2,2))+"]";
	xtitle(ti,"x","y");
endfunction

// Arrow demo -- xarrow
function [] = arrowdemo()
	clf();
	plot([0,1],[0,1],'w.');
	// the 2 is blue, the 1 is the size of arrowhead
	xarrows([0.1,0.9],[0.2,0.1],1,2)
	// the 5 is red, the 1 is the size of arrowhead
	xarrows([0.4,0.6],[0.4,0.7],1,5)
	// the 3 is green, the 1 is the size of arrowhead
	xarrows([0.7,0],[0.5,0],1,3)
	xgrid;
	title('Arrow Demo is it [x1 x2],[y1 y2] or [x1 y1] [x2 y2]?');
endfunction
