Advertisement
Guest User

Untitled

a guest
Sep 15th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. fprintf('Visualizing J(theta_0, theta_1) ...\n')
  2.  
  3. % Grid over which we will calculate J
  4. theta0_vals = linspace(-10, 10, 100);
  5. theta1_vals = linspace(-1, 4, 100);
  6.  
  7. % initialize J_vals to a matrix of 0's
  8. J_vals = zeros(length(theta0_vals), length(theta1_vals));
  9.  
  10. % Fill out J_vals
  11. for i = 1:length(theta0_vals)
  12. for j = 1:length(theta1_vals)
  13. t = [theta0_vals(i); theta1_vals(j)];
  14. J_vals(i,j) = computeCost(X, y, t);
  15. end
  16. end
  17.  
  18.  
  19. % Because of the way meshgrids work in the surf command, we need to
  20. % transpose J_vals before calling surf, or else the axes will be flipped
  21. J_vals = J_vals';
  22. % Surface plot
  23. figure;
  24. surf(theta0_vals, theta1_vals, J_vals)
  25. xlabel('\theta_0'); ylabel('\theta_1');
  26.  
  27. % Contour plot
  28. figure;
  29. % Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
  30. contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))
  31. xlabel('\theta_0'); ylabel('\theta_1');
  32. hold on;
  33. plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement