I have perfomed the MATLAB method in MATLAB as follows:

%Implementation of the secant method.

function c = secant2(x0, x1,eps)

format long e

fx0 = f(x0);                   
fx1 = f(x1);                   
if abs(fx1) < abs(fx0),         %c is the current best approx to a root.
  c = x1;  fc = fx1;
  c = x0;  fc = fx0;
fprintf('initial guesses: x0=%d, x1=%d, fx0=%d, fx1=%d\n',x0,x1,fx0,fx1)
if abs(fc) <= eps             % check to see if initial guess satisfies
  return;                       % convergence criterion.                      

while abs(fc) > eps,
  fpc = (fx1-fx0)/(x1-x0);      % this is the secant approx to f'.

  if fpc==0,               %% if fprime is 0, abort.
    error('fprime is 0')   %% the error function prints message and exits

  x0 = x1;  fx0 = fx1;             %% save previous iterate
  x1 = x1 - fx1/fpc;               %% secant step
  fx1 = f(x1);
  if abs(fx1) < abs(fx0),          %% store best approx to root in c.
    c = x1;  fc = fx1;
    c = x0;  fc = fx0;
  fprintf('   x0=%d, x1=%d, fx0=%d, fx1=%d\n',x0,x1,fx0,fx1)

function fx = f(x)
    fx = x-tan(x);

The thing is that this is supposed no to converge but is gives

initial guesses: x0=1, x1=2, fx0=-5.574077e-01, fx1=4.185040e+00
   x0=2, x1=1.117536e+00, fx0=4.185040e+00, fx1=-9.355037e-01
   x0=1.117536e+00, x1=1.278759e+00, fx0=-9.355037e-01, fx1=-2.047554e+00
   x0=1.278759e+00, x1=9.819084e-01, fx0=-2.047554e+00, fx1=-5.152181e-01
   x0=9.819084e-01, x1=8.820982e-01, fx0=-5.152181e-01, fx1=-3.327476e-01
   x0=8.820982e-01, x1=7.000875e-01, fx0=-3.327476e-01, fx1=-1.423505e-01
   x0=7.000875e-01, x1=5.640071e-01, fx0=-1.423505e-01, fx1=-6.853868e-02
   x0=5.640071e-01, x1=4.376483e-01, fx0=-6.853868e-02, fx1=-3.026248e-02
   x0=4.376483e-01, x1=3.377447e-01, fx0=-3.026248e-02, fx1=-1.345669e-02
   x0=3.377447e-01, x1=2.577501e-01, fx0=-1.345669e-02, fx1=-5.863760e-03
   x0=2.577501e-01, x1=1.959730e-01, fx0=-5.863760e-03, fx1=-2.547959e-03
   x0=1.959730e-01, x1=1.485017e-01, fx0=-2.547959e-03, fx1=-1.101340e-03
   x0=1.485017e-01, x1=1.123608e-01, fx0=-1.101340e-03, fx1=-4.752504e-04
   x0=1.123608e-01, x1=8.492712e-02, fx0=-4.752504e-04, fx1=-2.047731e-04
   x0=8.492712e-02, x1=6.415760e-02, fx0=-2.047731e-04, fx1=-8.817364e-05
   x0=6.415760e-02, x1=4.845148e-02, fx0=-8.817364e-05, fx1=-3.794967e-05
   x0=4.845148e-02, x1=3.658380e-02, fx0=-3.794967e-05, fx1=-1.632969e-05
   x0=3.658380e-02, x1=2.762008e-02, fx0=-1.632969e-05, fx1=-7.025644e-06
   x0=2.762008e-02, x1=2.085142e-02, fx0=-7.025644e-06, fx1=-3.022464e-06
   x0=2.085142e-02, x1=1.574097e-02, fx0=-3.022464e-06, fx1=-1.300219e-06
   x0=1.574097e-02, x1=1.188281e-02, fx0=-1.300219e-06, fx1=-5.593207e-07

ans =


So what did I performed wrong (I have seen here http://www.wolframalpha.com/input/?i=x-tan%28x%29&lk=4&num=1 That the roots are not in the interval [1,2] ) ?

Thanks in advance.

  • $\begingroup$ Your function gets flatter as it approaches one. Maybe, your method performs poorly there. Try to set tolerance smaller maybe? $\endgroup$ – Rubi Shnol Sep 21 '15 at 11:24
  • $\begingroup$ Let me try that :), give a second :) $\endgroup$ – user162343 Sep 21 '15 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.