Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:23:58+00:00 2026-06-13T14:23:58+00:00

I am trying gradient descent, I wrote following however not getting any answer, n=0;

  • 0

I am trying gradient descent, I wrote following however not getting any answer,

n=0;            %initialize iteration counter 
eps=1;          %initialize error 
a=0.8;         %set iteration parameter 
x=[1;1];        %set starting value
f=6*x(1)^2+8*x(2)^2-3*x(1)*x(2);
%Computation loop 
while eps>1e-12||n<100 
   gradf=[12*x(1)-3*x(2); 16*x(2)-3*x(1)];  %gradf(x) 
   eps=(norm(gradf)/(1+abs(f)));                                %error 
   y=x-a*gradf;                                                 %iterate 
   x=y;                                                         %update x 
   n=n+1;                                                       %counter+1 
end 
n;x;eps;        %display end values

When I add this file to path and type x it shows NaN, NaN. what is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:23:59+00:00Added an answer on June 13, 2026 at 2:23 pm

    There are several errors in your code. Consider this (I put comments where corrections were needed)

     n=0;            
     eps=1;         
     a=0.1;                    %You need a way smaller parameter to converge!
     x=[1;1];       
    
     A = [6 -3/2 ; -3/2 8];     %You have a bilinear positive definite form,
                                %you may use matrix form for convenience
    
    
    while eps>1e-12 && n<100    %You had wrong termination conditions!!
        gradf=2*A*x;            %(gradf in terms of matrix)
        f=x'*A*x;               %you need to update f every iteration!!
        eps=(norm(gradf)/(1+abs(f)))                              
        disp(eps > 1e-12)
        x=x-a*gradf;                                               
    
           %Now you can see the orbit  towards minimum                                     
        plot(x(1),x(2),'o'); hold on        
        n=n+1;                                                        
    end 
    n
    x
    eps        
    

    for instance with the current value a=.1 I get

    n = 100
    eps = 1.2308e-011
    
    x = 
      1.0e-012 *
    
      -0.2509
      0.4688
    

    That is I had to perform 100 iteration because my epsilon is still above the threshold. If I allow 200 iterations I get

    n =  110
    eps = 
       7.9705e-013
    
    x = 
    1.0e-013 *
      -0.1625
       0.3036
    

    I.e. 110 iterations are sufficient.

    Points get dense around the solution <code>(0,0)</code>


    Case of a general f (i.e. not a quadratic form).

    You can use, for instance, function handles, i.e. you define (before the while)

    foo = @(x) 6*x(1)^2+8*x(2)^2-3*x(1)*x(2);
    foo_x = @(x) 12*x(1)-3*x(2); 
    foo_y = @(x) 16*x(2)-3*x(1);
    

    then, in the while you substitute

    gradf = [foo_x(x);foo_y(x)];
    f = foo(x);
    

    P.S. for what concerns the while cycle, please keep in mind that you keep on iterating while you are not satisfied with your precision (eps>1e-12) AND your total number of iteration is below a given threshold (n<100).

    Consider also that you are working in finite precision: a numerical algorithm can never reach the analytic solution (i.e. what you have with infinite precision and infinite iterations), therefore, you always have to set a threshold (eps, which should be above the machine precision \approx1e-16) and that is your 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to replicate this: http://webdesignerwall.com/tutorials/css-gradient-text-effect however I can't seem to get it to
I am trying to run gradient descent and cannot get the same result as
Im trying to reproduce the following gradient programmatically. <shape xmlns:android=http://schemas.android.com/apk/res/android> <gradient android:startColor=@color/startcolor android:centerColor=#343434 android:endColor=#00000000
I'm trying to write out a bit of code for the gradient descent algorithm
I'm trying to use the FXlabel when following this (Adding a gradient label section)
I'm trying to implement Stochastic gradient descent in MATLAB. I followed the algorithm exactly
Im trying to generate a color gradient using ColdFusion. My current code below works
I am trying to create a gradient fill for a series in an area
I am trying to position a gradient over an inline / inline-block anchor link,
I'm trying to make a gradient that emits from the middle of the screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.