Answered step by step
Verified Expert Solution
Question
1 Approved Answer
We now will compare the normal equation method from problem 7 to gradient descent with the cost function: J()=2n1i=1n(h(x(i))y(i))2, where h=Tx=0x0+1x1. Before you can implement
We now will compare the normal equation method from problem 7 to gradient descent with the cost function: J()=2n1i=1n(h(x(i))y(i))2, where h=Tx=0x0+1x1. Before you can implement gradient descent, you need a way to track your performance, which computes J(). Write a matlab function computeCost.m, that takes in three arguments: X Xdata, size nD, Theta, size D1, Y, size n1, where n is the number of samples, and D is the dimension of the sample plus 1 (the plus 1 accounts for the constant column equal to 1 : X will be 972 in this example); and returns a single overall cost. The function cannot include any loops (it can be done with one line of code). Show the code here. Do not continue until you get a cost value of 32.0727. clear ; data =load( 'ex1data1.txt'); \% Dataset from Andrew Ng, Machine Learning MOOC X=data(:,1) y=data(:,2); Xdata =[ ones ( length (X),1)X] theta =zeros(2,1);% initialize fitting parameters to zero computeCost(Xdata,y,theta); Given the computeCost.m from the previous problem, you can now implement gradient descent using the given function gradientDescentLinear.m. Use the following matlab code: clear ; close all; data = load('ex1data1.txt'); \% Dataset from Andrew Ng, Machine Learning MOOC X=data(:,1); y=data(:,2) M=[ ones(length (X),1)X] theta_init = zeros (2,1);% initialize fitting parameters to zero % Some gradient descent settings iterations =1500 alpha =0.01; % run gradient descent theta = gradientDescentLinear(M, y, theta_init, alpha, iterations); Do not continue until you get a theta value of [-3.6303 1.1664]. For this problem, plot the linear regression line that results from gradient descent as a blue solid line, linewidth =3. In the same figure plot also as a green line your result from problem 7, where you did linear regression using the normal equations method. Show code and include plot here x-axis should go from 0 to 25 and y-axis should go from -5 to 25 (Note: part of your code will repeat your solution to problem 7, to get the results using the normal equations method)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started