plotFit.m 804 B

12345678910111213141516171819202122232425262728
  1. function plotFit(min_x, max_x, mu, sigma, theta, p)
  2. %PLOTFIT Plots a learned polynomial regression fit over an existing figure.
  3. %Also works with linear regression.
  4. % PLOTFIT(min_x, max_x, mu, sigma, theta, p) plots the learned polynomial
  5. % fit with power p and feature normalization (mu, sigma).
  6. % Hold on to the current figure
  7. hold on;
  8. % We plot a range slightly bigger than the min and max values to get
  9. % an idea of how the fit will vary outside the range of the data points
  10. x = (min_x - 15: 0.05 : max_x + 25)';
  11. % Map the X values
  12. X_poly = polyFeatures(x, p);
  13. X_poly = bsxfun(@minus, X_poly, mu);
  14. X_poly = bsxfun(@rdivide, X_poly, sigma);
  15. % Add ones
  16. X_poly = [ones(size(x, 1), 1) X_poly];
  17. % Plot
  18. plot(x, X_poly * theta, '--', 'LineWidth', 2)
  19. % Hold off to the current figure
  20. hold off
  21. end