Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Matlab HW help!! Gradient descent is a numerical method for finding the minimum of a function y=f(x). In this Hw we are going to write

Matlab HW help!!

image text in transcribed

Gradient descent is a numerical method for finding the minimum of a function y=f(x). In this Hw we are going to write a function that performs the gradient descent method on y-fix). Then we are going to see the method being applied to ?3x2 by calling the function we wrote. 1. Open a script and write the following code in the script. a. Define a variable 'x to be an array of numbers from -10 to 10 with step sizes of 0.1 b. Define the array y' to be y-x2, and plot 'y' as a function of'x c. From the plot created in (1.b), answer the following questions: i. Is 'Y a convex function of 'x? ii. Does 'y' have a minimum over the range of'x? If so, what is the minimum value of 'y, and at what value of does that minimum occur? what is the gradient of y-x at that minimum point? d. Save your script under the name 'myExample.m 2. Open a new script to write a function called mvradientDescent' that takes 'x and y as an input and gives 'min ' and x at min y as an output. 3. Now we want to implement the gradient descent method in the function 'mySradientDescent The gradient descent method starts at an initial point x0 and keeps moving towards the minimum point with a step size of 'a' until it converges to the minimum. To move towards the minimum point means to move in the direction that reduces the gradient. To converge to the minimum point means that you reach a point where the gradient at that point converges to zero, i.e., the gradient at that point is within a distance of 'e' from zero. a. Define x0-10, a-0.1, e-0.01 and the condition snd-1. b. Calculate the gradient of 'y at x as grad y_at x0-(y at x0-y at xo_minus_al/a where y at x0 is the value of Y at the point 'x0' and 'y at xo_minus_a' is the value of 'y at the point 'xo a c. Write a while loop which keeps executing until snd-0. In the loop: i. The value stored in'x0' is updated using xd = -(grady-at-x0 * a) i. The gradient of Y is recalculated at the new point'xO using grad y_at_xo (y at x0-y at_x0_minus_a)/a. iii. The value of the condition 'snd' is updated using snd- grad y_ at_x0ce. d. When the while loop is done executing, the variable 'xo would contain the point where the minimum occurred and the variable 'y at_x0' will contain the minimum value of 'y Now, outside the while loop set minn-v-at-x0 and &at,minx-xo. 4. Open your script 'myExample.m' and continue coding in there: a. Call the function 'mradientDescent' using the input 'x and y and let the output of your function call appear on the command window by not placing a semi-colon at the end of your function call i. Using 'hold on', plot the output of the function call as a stari.e., on top of the plot created in part 1.b. Is the' placed at the minimum of y=x2 where you expect it to be? If not, you have a mistake in your code and you need to debug your code! Gradient descent is a numerical method for finding the minimum of a function y=f(x). In this Hw we are going to write a function that performs the gradient descent method on y-fix). Then we are going to see the method being applied to ?3x2 by calling the function we wrote. 1. Open a script and write the following code in the script. a. Define a variable 'x to be an array of numbers from -10 to 10 with step sizes of 0.1 b. Define the array y' to be y-x2, and plot 'y' as a function of'x c. From the plot created in (1.b), answer the following questions: i. Is 'Y a convex function of 'x? ii. Does 'y' have a minimum over the range of'x? If so, what is the minimum value of 'y, and at what value of does that minimum occur? what is the gradient of y-x at that minimum point? d. Save your script under the name 'myExample.m 2. Open a new script to write a function called mvradientDescent' that takes 'x and y as an input and gives 'min ' and x at min y as an output. 3. Now we want to implement the gradient descent method in the function 'mySradientDescent The gradient descent method starts at an initial point x0 and keeps moving towards the minimum point with a step size of 'a' until it converges to the minimum. To move towards the minimum point means to move in the direction that reduces the gradient. To converge to the minimum point means that you reach a point where the gradient at that point converges to zero, i.e., the gradient at that point is within a distance of 'e' from zero. a. Define x0-10, a-0.1, e-0.01 and the condition snd-1. b. Calculate the gradient of 'y at x as grad y_at x0-(y at x0-y at xo_minus_al/a where y at x0 is the value of Y at the point 'x0' and 'y at xo_minus_a' is the value of 'y at the point 'xo a c. Write a while loop which keeps executing until snd-0. In the loop: i. The value stored in'x0' is updated using xd = -(grady-at-x0 * a) i. The gradient of Y is recalculated at the new point'xO using grad y_at_xo (y at x0-y at_x0_minus_a)/a. iii. The value of the condition 'snd' is updated using snd- grad y_ at_x0ce. d. When the while loop is done executing, the variable 'xo would contain the point where the minimum occurred and the variable 'y at_x0' will contain the minimum value of 'y Now, outside the while loop set minn-v-at-x0 and &at,minx-xo. 4. Open your script 'myExample.m' and continue coding in there: a. Call the function 'mradientDescent' using the input 'x and y and let the output of your function call appear on the command window by not placing a semi-colon at the end of your function call i. Using 'hold on', plot the output of the function call as a stari.e., on top of the plot created in part 1.b. Is the' placed at the minimum of y=x2 where you expect it to be? If not, you have a mistake in your code and you need to debug your code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions