Guest User

Untitled

a guest
Dec 11th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.60 KB | None | 0 0
  1. theta0 = -10:0.1:10; % parameter will vary from -10 to 10 by 0.1
  2. theta1 = theta0; % the same
  3.  
  4. % create all possible combinations:
  5. [theta0m,theta1m] = meshgrid(theta0, theta1);
  6.  
  7. x = 1:3; % training instances
  8. y = x;  % this is just y = 1*x + 0
  9. m = length(x);
  10.  
  11. h = zeros(size(theta0m)(1), size(theta0m)(2), m);
  12. J = zeros(size(theta0m));
  13.  
  14. for i = 1:m
  15.       h(:,:,i) = theta0m .+ theta1m * x(i);
  16.           J = J + (h(:,:,i) - y(i)) .^ 2;
  17. endfor
  18.  
  19. J = J/(2*m);
  20.  
  21. contour(theta0, theta1, J, 1:5);
  22. xlabel('\theta_0');
  23. ylabel('\theta_1');
  24. title('Contour plot for cost function J');
  25. print -dpng contour.png
Add Comment
Please, Sign In to add comment