Guest User

Untitled

a guest
Jul 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. syms k;
  2. x1 = linspace(100,200,101); %area of the house
  3. w = 7000; %cost/meter square
  4. y1 = 3 * x1.^2 + 3000; %price of the house
  5.  
  6. %Scaling and normalizing of the data set
  7. x = sd(x1);
  8. y = sd(y1);
  9.  
  10. t0 = 0; %initialize theta as zero
  11. t1 = 0; %initialize theta as zero
  12. a = 0.001; %setting learning rate(alpha)
  13. m = 5;
  14. i = 0; %number of iteration(to measure performance of the algorithm)
  15. %While loop to get the required theta to minimize cost function
  16. while true
  17. i = i + 1;
  18. j(i) = cost_function(t0, t1, x, y);
  19. term_0 = (a/m) * (t0 + t1*summation(x) - summation(y));
  20. term_1 = (a/m) * (t0 * summation(x) + t1 * summation(x .^2) - summation(x .* y));
  21. temp0 = t0 - term_0;
  22. temp1 = t1 - term_1;
  23. if temp0 == t0 && temp1 == t1
  24. break;
  25. end
  26. t0 = temp0;
  27. t1 = temp1;
  28. end
  29. %calculating the prediction function and plotting the results
  30. h = t0 + t1*x;
  31. num_iterations = linspace(1, i, i);
  32.  
  33.  
  34. subplot(1,2,1)
  35. plot(x, y,'.');
  36. hold on;
  37. plot(x, h, 'r');
  38. hold off;
  39. subplot(1,2,2),plot(num_iterations, j);
  40. xlabel('Number of Iterations');
  41. ylabel('Cost Function (J)');
Add Comment
Please, Sign In to add comment