0

The program is an optimized gradient descent.

Here is the code :

    clear all
    close all
    [x,y] = meshgrid(-2:0.1:2);                                            
   z = x.^2 + 100*y.^2;                                          
   n = 1;
   k(n)=0.01;
   arret = 0.0001;                                                         
   mesh(x,y,z);                                                            
   [x1(n),y1(n)] = ginput(1);                                             
   diff_x(n) = 2*x1(n);                                                   
   diff_y(n) = 200*y1(n);                                                 
   while sqrt(diff_x(n)^2 + diff_y(n)^2) > arret && n < 30
     k(n) = sqrt(diff_x(n)^2 + diff_y(n)^2)/(8*x1(n).^2+2*10.^6*y1(n).^2);         
     x1(n+1) = x1(n) - k(n)*diff_x(n);                                    
     y1(n+1) = y1(n) - k(n)*diff_y(n);    
     n = n+1;  
     diff_x(n) = 2*x1(n);
     diff_y(n) = 200*y1(n);
     z1(n) = x1(n).^2 + 100*y1(n).^2;                                     
     plot3(x1(n),y1(n),z1(n));                                             
   end

   x1(n)
   y1(n)
   n

So I got this and I don't understand why.

error: GradientPasOptFinal2: A(I): index out of bounds; value 2 out of bound 1

error: called from:

error: error: C:\Octave\octave-3.8.2\GradientPasOptFinal2.m at line 13, column 8

SOLVED : the k(n) between y1(n) and n was the reason, i don't know why, but now the program works, thank you !

Community
  • 1
  • 1
avers
  • 17
  • 1
  • 3
  • Thank you Doelleri for the correction, i wrote it quickly and english isn't my native language... – avers May 12 '15 at 20:37

1 Answers1

0

You have only defined k(1). k(2) is undefined. Move this line:

k(n) = sqrt(diff_x(n)^2 + diff_y(n)^2)/(8*x1(n).^2+2*10.^6*y1(n).^2); 

Ahead of these lines:

x1(n+1) = x1(n) - k(n)*diff_x(n);                                    
y1(n+1) = y1(n) - k(n)*diff_y(n); 

And it should work.

kaz
  • 1,190
  • 8
  • 19
  • Thank you for the fast answer, i'm a novice on Octave, but x1(n) and y1(n) need a value of k(n), on the other side, k(n) needs the values of x1(n) and y1(n). other edit : in the while, does the computer understand that he has k(n) = 0.01, so he can calculate x1(n+1) and y1(n+1), then he can calculate k(n+1), then x1(n+2) and y1(n+2) .... i don't understand the problem. – avers May 12 '15 at 19:44
  • I'm still stuck, i tried many things it doesn't work, i don't know what to do. – avers May 12 '15 at 20:12
  • Thank for your time, i tried it, it doesn't work.랬error: GradientPasOptFinal2: A(I): index out of bounds; value 2 out of bound 1 error: called from: error: C:\Octave\octave-3.8.2\GradientPasOptFinal2.m at line 13, column 8 I've just no idea to fix it, line 13 column 8 it's diff_x(n), all is defined, it's very strange ... – avers May 12 '15 at 20:23
  • now i have it : error: C:\Octave\octave-3.8.2\GradientPasOptFinal2.m at line 25, column 1 error: GradientPasOptFinal2: A(I): index out of bounds; value 10 out of bound 9 – avers May 12 '15 at 20:33