# Answered! Computer exercises Several of the exercises use the data in the following table….

Computer exercises
Several of the exercises use the data in the following table.

Don't use plagiarized sources. Get Your Custom Essay on
Answered! Computer exercises Several of the exercises use the data in the following table….
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS \$13/PAGE

Section 5.4

1. Consider basic gradient descent (Algorithm 1) and Newton’s algorithm (Algorithm 2) applied to the data in the tables.

(a) Apply both to the three-dimensional data in categories ω1 and ω3. For the gradient descent use η(k) = 0.1. Plot the criterion function as function of the iteration number.

(b) Estimate the total number of mathematical operations in the two algorithms.

(c) Plot the convergence time versus learning rate. What is the minimum learning rate that fails to lead to convergence?

MAIN.M

% Storing the values in seperate matrices

W1 = dataSet(:, 1);

w3 = dataSet(:, 2);

```% Do you want feature normalization?
```
```
```
```normalization = true;
```
```
```
```% Applying mean normalization to our dataset
```
```
```
```if (normalization)
```
```
```
```    maxX = max(W1);
```
```    minX = min(W1);
```
```   W1= (W1 - maxX) / (maxX - minX);
```
```
```
```End
```
```
```
```% Running gradient descent on the data
```
```
```
```% 'w1' is our input matrix
```
```
```
```% 'w3' is our output matrix
```
```
```
```% 'parameters' is a matrix containing our initial theta and slope
```
```
```
```parameters = [0; 0];
```
```
```
```learningRate = 0.1;
```
```
```
```iteration = 1500;
```
```
```
```[parameters, criterionHistory] = gradient(w1, w3, parameters, learningRate, iteration);
```
```% Plotting our final hypothesis
```
```
```
```figure;
```
```
```
```plot(min(w1 (:, 2)):max(w1 (:, 2)), parameters(1) + parameters(2) * (min(w1 (:, 2)):max(w1 (:, 2))));
```
```
```
```hold on;
```
```% Plotting our criterion function on a different figure to see how we did
```
```
```
```figure;
```
```
```
```plot(criterionHistory, 1:iteration);
```
```
```
```% Finally predicting the output of the provided input
```
```
```
```input = 120;
```
```
```
```if (normalization)
```
```    input = (input - maxX) / (maxX - minX);
```
```end
```
```
```
```output = parameters(1) + parameters(2) * input;
```
```
```
```disp(output);
```

function [ parameters, criterionHistory ] = gradient(w1, w3, parameters, learningRate, iteration )

```% Getting the length of our dataset
```
```
```
```m = length(w3);
```
```
```
```% Creating a matrix of zeros for storing our criterion function history
```
```
```
```criterionHistory = zeros(iteration, 1);
```

for i = 1:iteration

% Calculating the transpose of our hypothesis

h = (w1* parameters – w3′;

% Updating the parameters

parameters(1) = parameters(1) – learningRate * (1/m) * h * w1(:, 1);

parameters(2) = parameters(2) – learningRate * (1/m) * h * w1 (:, 2);

% Keeping track of the criterion function

criterionHistory(i) = criterion(w1, w3, parameters);

end

criterion.m

```parameters(1) = parameters(1) - learningRate * (1/m) * h * w1(:, 1);
```
```parameters(2) = parameters(2) - learningRate * (1/m) * h * w1(:, 2);
```
```function [ criterion ] = criterion( w1, w3, parameters )
```
```
```
```    %   Calculates the criterion function
```
```
```
```    criterion = (w1 * parameters – w3)' * (w1 * parameters – w3) / (2 * length(w3));
```
```
```
`end`