% % File: ContourPlot.m Version 1 % % A. O. Hausknecht Spring 2012 % Mathematics Department, UMass Dartmouth % % contourPlot(Finline, xMin, xMax, yMin, yMax, Clist, n) % % PURPOSE: % Automates the process of plotting contours of a function z = f(x, y) % % ITS PARAMETERS: % Finline: An inline function representation of f(x,y) % xMin, xMax: the x-axis limits % yMin, yMax: the y-axis limits % Clist = [c1 c2 ... cK ]: A vector of Z-values for the contours % n: The number of plotting points used in each direction (=> n^2 in all) % % EXAMPLE: % Step 0: As with all .m files, countourPlot must be in Octave's current % working directory or in a directory in Octave's path. % Step 1: Define f using vector operations as needed % octave-3.4.0:01> f = inline('(x.^2 - 1).*(y.^2 - 1)', 'x', 'y'); % Step 2: Define a list of contour values % octave-3.4.0:02> Clist = [-1 -0.25 -0.5 0.25 0.5 1]; % Step 3: Invoke the 'countourPlot' function % octave-3.4.0:03> contourPlot(f, -2, 2,-2, 2, Clist, 100) % function contourPlot(Finline, xMin, xMax, yMin, yMax, Clist, n) if (n < 20) n = 20; end if (xMin == xMax) xMin = xMin - 2; xMax = xMax + 2; end if (xMin > xMax) temp = xMin; xMin = xMax; xMax = temp; end if (yMin == yMax) yMin = yMin - 2; yMax = yMax + 2; end if (yMin > yMax) temp = yMin; yMin = yMax; yMax = temp; end x = linspace(xMin, xMax, n); y = linspace(yMin, yMax, n); [X, Y] = meshgrid(x,y); Z = Finline(X,Y); contour(X, Y, Z, Clist, 'linewidth',3); xlabel('X'); ylabel('Y'); % Set the axes lengths and draw axes axis([xMin xMax yMin yMax]); line([xMin xMax], [0, 0], 'linewidth', 4); line([0, 0], [yMin yMax], 'linewidth', 4); end