Skip to main content
added 1056 characters in body
Source Link
Shuhao Cao
  • 2.6k
  • 17
  • 30

If you are trying to find the entries with numerical difference being zero, you might end up with nothing because the differences may be non-zero for all entries because they are double, and assuming these 616 entries are approximation to something.

I suggest you might wanna try to set a tolerance Tol: for example

 X = sin(linspace(0,2*pi,616)'); 
 % this gives you an X being the grid approximation to sine
 Y = diff(X);
 Tol = 1e-2*2*pi/616;615;
 indY = find(abs(Y) < Tol);
 % the index of the difference vector that has absolute value < Tolerance

Then X(indY) and X(indY+1) will give you the entries of X that produces these less-than-tolerance differences. If the latter is bigger than the former, then X starts to increase (approximation), vice versa.

MATLAB gives:

  >> X(indY)
     ans =
         0.99997
               1
        -0.99992
              -1
  >> X(indY+1)
     ans =
               1
         0.99992
              -1
        -0.99997

The criteria for choosing Tol is more like an a priori guess. First you have the length of the interval I set being $2\pi$, there is $616$ grid points, which has a magnitude of 1e2. The true derivative of $\sin x$ is $\cos x$, which has absolute value less than $1$, so the all the numerical differences should be less than or equal to $h = 2\pi/615$. Now I already know sine, when near its extrema, increases or decreases like a linear function (imagine Taylor expansion, or small angle approximation), and it takes about 100 grids to make it the maximum (knowing a priori for sine). Therefore the absolute value of the difference being less than the magnitude of the 1e-2 times the grid size would suffice to say that, X almost remains the same. It is problem dependent.

If you are trying to find the entries with numerical difference being zero, you might end up with nothing because the differences may be non-zero for all entries because they are double, and assuming these 616 entries are approximation to something.

I suggest you might wanna try to set a tolerance Tol: for example

 X = sin(linspace(0,2*pi,616)'); 
 % this gives you an X being the grid approximation to sine
 Y = diff(X);
 Tol = 1e-2*2*pi/616;
 indY = find(abs(Y) < Tol);
 % the index of the difference vector that has absolute value < Tolerance

Then X(indY) and X(indY+1) will give you the entries of X that produces these less-than-tolerance differences. If the latter is bigger than the former, then X starts to increase (approximation), vice versa.

If you are trying to find the entries with numerical difference being zero, you might end up with nothing because the differences may be non-zero for all entries because they are double, and assuming these 616 entries are approximation to something.

I suggest you might wanna try to set a tolerance Tol: for example

 X = sin(linspace(0,2*pi,616)'); 
 % this gives you an X being the grid approximation to sine
 Y = diff(X);
 Tol = 1e-2*2*pi/615;
 indY = find(abs(Y) < Tol);
 % the index of the difference vector that has absolute value < Tolerance

Then X(indY) and X(indY+1) will give you the entries of X that produces these less-than-tolerance differences. If the latter is bigger than the former, then X starts to increase (approximation), vice versa.

MATLAB gives:

  >> X(indY)
     ans =
         0.99997
               1
        -0.99992
              -1
  >> X(indY+1)
     ans =
               1
         0.99992
              -1
        -0.99997

The criteria for choosing Tol is more like an a priori guess. First you have the length of the interval I set being $2\pi$, there is $616$ grid points, which has a magnitude of 1e2. The true derivative of $\sin x$ is $\cos x$, which has absolute value less than $1$, so the all the numerical differences should be less than or equal to $h = 2\pi/615$. Now I already know sine, when near its extrema, increases or decreases like a linear function (imagine Taylor expansion, or small angle approximation), and it takes about 100 grids to make it the maximum (knowing a priori for sine). Therefore the absolute value of the difference being less than the magnitude of the 1e-2 times the grid size would suffice to say that, X almost remains the same. It is problem dependent.

Source Link
Shuhao Cao
  • 2.6k
  • 17
  • 30

If you are trying to find the entries with numerical difference being zero, you might end up with nothing because the differences may be non-zero for all entries because they are double, and assuming these 616 entries are approximation to something.

I suggest you might wanna try to set a tolerance Tol: for example

 X = sin(linspace(0,2*pi,616)'); 
 % this gives you an X being the grid approximation to sine
 Y = diff(X);
 Tol = 1e-2*2*pi/616;
 indY = find(abs(Y) < Tol);
 % the index of the difference vector that has absolute value < Tolerance

Then X(indY) and X(indY+1) will give you the entries of X that produces these less-than-tolerance differences. If the latter is bigger than the former, then X starts to increase (approximation), vice versa.