Question

In: Computer Science

Consider two functions f (x; y) = (x10)2 + (y + 2)2; and g(x; y) =...

Consider two functions
f (x; y) = (x10)2 + (y + 2)2; and
g(x; y) = (x ? 10)2 + (x ? y + 5)4
Please implement a basic gradient descent algorithm using python
Starting with (x; y) = (0; 0), and set up a xed learning rate = 0:5,
run the gradient descent algorithm for each function. Run for 10
iterations, and report both (x; y) and the function value at the end of
each iteration.
Adjust the , and nd out the fastest convergence (smallest function
value) after T steps (try T = 10 or 100).

Solutions

Expert Solution

The Matlab code for your assignment is as GradientDescent1.m for first function and GradientDescent2 for second
*******************************GradientDescent1.m*************************
x0=0;

y0=0;

%alpha is the rate by which we update the x and y values

%at alpha=0.5 is the convergence fastest

alpha=0.5;

tol=0.001;

%the following is the function wrt x and y

f_xy=@(x,y)(x-10).^2+(y+2).^2;
%following are the derivatives wrt x and y

dfdx=@(x) 2*(x-10);
dfdy=@(y) 2*(y+2);
%negative of derivatives give the descent

s1=-dfdx(x0);

s2=-dfdy(y0);
x_next=x0+alpha*s1;

y_next=y0+alpha*s2;
err=inf;

i=0;

while i<10

  

i=i+1;

err=(f_xy(x_next,y_next)-0);

x0=x_next;

y0=y_next;

  

s1=-dfdx(x0);

s2=-dfdy(y0);

%update the x and y values

% using gradient descent

x_next=x0+alpha*s1;

y_next=y0+alpha*s2;

fprintf('x=%f,y=%f, f(x,y)=%f\n',x0,y0,f_xy(x0,y0))

end


*****************************GradientDescent2.m*************************************
x0=0;

y0=0;

%alpha is the rate by which we update the x and y values

%alpha less or equal to 0.01 works

%otherwise it offshoots

alpha=0.01;

tol=0.001;

%the following is the function wrt x and y

f_xy=@(x,y)(x-10).^2+(x-y+5).^4;
%following are the derivatives wrt x and y

dfdx=@(x,y) 2*(x-10)+4*(x-y+5)^3;
dfdy=@(x,y) -1*4*(x-y+5)^3;
%negative of derivatives give the descent

s1=-dfdx(x0,y0);

s2=-dfdy(x0,y0);
x_next=x0+alpha*s1;

y_next=y0+alpha*s2;
err=inf;

i=0;

while i<100

  

i=i+1;

err=(f_xy(x_next,y_next)-0);

x0=x_next;

y0=y_next;

  

s1=-dfdx(x0,y0);

s2=-dfdy(x0,y0);

%update the x and y values

% using gradient descent

x_next=x0+alpha*s1;

y_next=y0+alpha*s2;

fprintf('x=%f,y=%f, f(x,y)=%f\n',x0,y0,f_xy(x0,y0))

end


Related Solutions

Consider the scalar functions f(x,y,z)g(x,y,z)=x^2+y^2+z^2, g(x,y,z)=xy+xz+yz, and=h(x,y,z)=√xyz Which of the three vector fields ∇f∇f, ∇g∇g and...
Consider the scalar functions f(x,y,z)g(x,y,z)=x^2+y^2+z^2, g(x,y,z)=xy+xz+yz, and=h(x,y,z)=√xyz Which of the three vector fields ∇f∇f, ∇g∇g and ∇h∇h are conservative?
For the following functions f and g : f(x, y) = e ax − (1 −...
For the following functions f and g : f(x, y) = e ax − (1 − a)lny a > 0 g(x, y, z) = −3x 2 − 3y 2 − 3z 2 + 2xy + 2xz + 2yz 1. Calculate the Hessian matrices of f and g noted Hf (x, y) and Hg(x, y, z) 2. Show that Hg(x, y, z) is define negativly for all (x, y, z) ∈ Dg 3. For what value o a is , Hf...
Consider the following five utility functions. G(x,y) = (x)1/2 + y H(x,y) = (x+1)1/2 + y...
Consider the following five utility functions. G(x,y) = (x)1/2 + y H(x,y) = (x+1)1/2 + y + 1 L(x,y) = ln(x) + y U(x,y) = x ey W(x,y) = 100 x - 4x2 + 3 y Z(x,y) = -(100 - x)2 -3 (y-200)2 Which function or functions represent the same preferences as U?   a.G b.U c.None. d.W e.Z f.L g.H
3. Let F : X → Y and G: Y → Z be functions. i. If...
3. Let F : X → Y and G: Y → Z be functions. i. If G ◦ F is injective, then F is injective. ii. If G ◦ F is surjective, then G is surjective. iii. If G ◦ F is constant, then F is constant or G is constant. iv. If F is constant or G is constant, then G ◦ F is constant.
Let f : X → Y and g : Y → Z be functions. We can...
Let f : X → Y and g : Y → Z be functions. We can define the composition of f and g to be the function g◦ f : X → Z for which the image of each x ∈ X is g(f(x)). That is, plug x into f, then plug theresultinto g (justlikecompositioninalgebraandcalculus). (a) If f and g arebothinjective,must g◦ f beinjective? Explain. (b) If f and g arebothsurjective,must g◦ f besurjective? Explain. (c) Suppose g◦ f isinjective....
Consider the following functions. f(x) = x − 3, g(x) = |x + 3| Find (f...
Consider the following functions. f(x) = x − 3, g(x) = |x + 3| Find (f ∘ g)(x). Find the domain of (f ∘ g)(x). (Enter your answer using interval notation.) Find (g ∘ f)(x). Find the domain of (g ∘ f)(x). (Enter your answer using interval notation.) Find (f ∘ f)(x). Find the domain of (f ∘ f)(x). (Enter your answer using interval notation.) Find (g ∘ g)(x). Find the domain of (g ∘ g)(x). (Enter your answer using...
For the following functions f and g : f(x, y) = e^ax − (1 − a)lny...
For the following functions f and g : f(x, y) = e^ax − (1 − a)lny a > 0 g(x, y, z) = −3x^2 − 3y^2 − 3z^2 + 2xy + 2xz + 2yz 1. Calculate the Hessian matrices of f and g noted Hf (x, y) and Hg(x, y, z) 2. Show that Hg(x, y, z) is define negativly for all (x, y, z) ∈ Dg 3. For what value o a is , Hf (x, y) define positivly...
Consider the non linear ODE: (dx/dt) = -y = f(x,y) (dy/dt) = x^2-x = g(x,y) (a)....
Consider the non linear ODE: (dx/dt) = -y = f(x,y) (dy/dt) = x^2-x = g(x,y) (a). Compute all critical points (b) Derive the Jacobian matrix (c). Find the Jacobians for each critical point (d). Find the eigenvalues for each Jacobian matrix (e). Find the linearized solutions in the neighborhood of each critical point (f) Classify each critical point and discuss their stability (g) Sketch the local solution trajectories in the neighborhood of each critical point
Let f(x) and g(x) be two generic functions. Assume limx→0(f(x)+2g(x))=2 & limx→0(f(x)−g(x))=8. Compute limx→1(f(lnx)/g(x2−x)). A. It...
Let f(x) and g(x) be two generic functions. Assume limx→0(f(x)+2g(x))=2 & limx→0(f(x)−g(x))=8. Compute limx→1(f(lnx)/g(x2−x)). A. It cannot be computed B. 2 C. 4 D. -3 E. -4
Consider the following function: f (x , y , z ) = x 2 + y...
Consider the following function: f (x , y , z ) = x 2 + y 2 + z 2 − x y − y z + x + z (a) This function has one critical point. Find it. (b) Compute the Hessian of f , and use it to determine whether the critical point is a local man, local min, or neither? (c) Is the critical point a global max, global min, or neither? Justify your answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT