% Here is the data we want to fit a straight line to: x = [ -1 1 0.3]; y = [ 1 -1 0.2]; % Set up the min and max angles for the line and its intercept: min_angle = -89; max_angle = 89; min_intercept = -10; max_intercept = 10; % Create arrays of all these angles and intercepts angle = linspace (min_angle, max_angle); intercept = linspace (min_intercept, max_intercept); % Obtain the lengths of these arrays na = length (angle); nb = length (intercept); % Convert from angles to slopes ( = tan (angle)) slope = tan (deg2rad(angle)); % Initialize total misfit grid to zero E = zeros(nb, na); % Clear Figure 1 which will show both a contour plot and a surf plot of E figure(1) clf for row = 1:nb for col = 1:na E(row, col) = gg250_lab9_misfit (x, y, slope(col), intercept(row), 0); end end % Find the (slope, intercept) where E is minimum [row, col] = find (E == min(E(:))); subplot (2,1,1) contour (angle, intercept, E) subplot (2,1,2) surf (angle, intercept, E) figure(2) clf gg250_lab9_plotline (x, y, slope(col), intercept(row))