Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 6/Programming Assignment - 5/machine-learning-ex5/ex5/plotFit.m
864 views
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
7
% Hold on to the current figure
8
hold on;
9
10
% We plot a range slightly bigger than the min and max values to get
11
% an idea of how the fit will vary outside the range of the data points
12
x = (min_x - 15: 0.05 : max_x + 25)';
13
14
% Map the X values
15
X_poly = polyFeatures(x, p);
16
X_poly = bsxfun(@minus, X_poly, mu);
17
X_poly = bsxfun(@rdivide, X_poly, sigma);
18
19
% Add ones
20
X_poly = [ones(size(x, 1), 1) X_poly];
21
22
% Plot
23
plot(x, X_poly * theta, '--', 'LineWidth', 2)
24
25
% Hold off to the current figure
26
hold off
27
28
end
29
30