Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- syms k;
- x1 = linspace(100,200,101); %area of the house
- w = 7000; %cost/meter square
- y1 = 3 * x1.^2 + 3000; %price of the house
- %Scaling and normalizing of the data set
- x = sd(x1);
- y = sd(y1);
- t0 = 0; %initialize theta as zero
- t1 = 0; %initialize theta as zero
- a = 0.001; %setting learning rate(alpha)
- m = 5;
- i = 0; %number of iteration(to measure performance of the algorithm)
- %While loop to get the required theta to minimize cost function
- while true
- i = i + 1;
- j(i) = cost_function(t0, t1, x, y);
- term_0 = (a/m) * (t0 + t1*summation(x) - summation(y));
- term_1 = (a/m) * (t0 * summation(x) + t1 * summation(x .^2) - summation(x .* y));
- temp0 = t0 - term_0;
- temp1 = t1 - term_1;
- if temp0 == t0 && temp1 == t1
- break;
- end
- t0 = temp0;
- t1 = temp1;
- end
- %calculating the prediction function and plotting the results
- h = t0 + t1*x;
- num_iterations = linspace(1, i, i);
- subplot(1,2,1)
- plot(x, y,'.');
- hold on;
- plot(x, h, 'r');
- hold off;
- subplot(1,2,2),plot(num_iterations, j);
- xlabel('Number of Iterations');
- ylabel('Cost Function (J)');
Add Comment
Please, Sign In to add comment