Results 1 to 8 of 8

Math Help - Secant method in MATLAB

  1. #1
    Newbie
    Joined
    Feb 2010
    Posts
    24

    Secant method in MATLAB

    I try to solve this equation with secant method in MATLAB.

    fn=40*n^1.5-875*n+35000

    my initial guess is n1=60; n2=68; I want to find root and absolute relative approximate
    error at the end of each iteration. Can you help me repair my file?
    This is my m-file:

    Code:
    clc
    clear
    n1=60;
    n2=68;
    tol=1e-3;
    err0=3;
    iter=0;
    fprintf('iteration n relative approximate error\n')
    
    while err0>=tol
      iter=iter+1;
      fn1=40*(n1).^1.5-875*(n1)+35000;
      fn2=40*(n2).^1.5-875*(n2)+35000;
      nnew=n2-fn2*((n2-n1)/(fn2-fn1));
      fnnew=40*(nnew).^1.5-875*(nnew)+35000;
      err(iter)=(abs((nnew-n2)/nnew))*100;
      fprintf('%2d %f %f\n',iter,nnew,err(iter))
    
      if nnew>n1
        n1=nnew;
      else
        n2=nnew;
      end
    
    end
    
    nnew
    iter
    Last edited by CaptainBlack; April 23rd 2010 at 11:30 AM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by chronicals View Post
    I try to solve this equation with secant method in MATLAB.

    fn=40*n^1.5-875*n+35000

    my initial guess is n1=60; n2=68; I want to find root and absolute relative approximate
    error at the end of each iteration. Can you help me repair my file?
    This is my m-file:

    Code:
    clc
    clear
    n1=60;
    n2=68;
    tol=1e-3;
    err0=3;
    iter=0;
    fprintf('iteration n relative approximate error\n')
    
    while err0>=tol
      iter=iter+1;
      fn1=40*(n1).^1.5-875*(n1)+35000;
      fn2=40*(n2).^1.5-875*(n2)+35000;
      nnew=n2-fn2*((n2-n1)/(fn2-fn1));
      fnnew=40*(nnew).^1.5-875*(nnew)+35000;
      err(iter)=(abs((nnew-n2)/nnew))*100;
      fprintf('%2d %f %f\n',iter,nnew,err(iter))
    
      if nnew>n1
        n1=nnew;
      else
        n2=nnew;
      end
    
    end
    
    nnew
    iter
    Have you tried running it? What happens? That is the least I expect when a question like this is asked.

    It appears you have an infinite loop (which you should have mentioned), as you have an infinite loop you have a problem in the termination condition for the while loop.

    Code:
    clc
    clear
    n1=60;
    n2=68;
    tol=1e-3;
    err=3;
    iter=0;
    fprintf('iteration n relative approximate error\n')
    
    while err>=tol
      iter=iter+1;
      fn1=40*(n1).^1.5-875*(n1)+35000;
      fn2=40*(n2).^1.5-875*(n2)+35000;
      nnew=n2-fn2*((n2-n1)/(fn2-fn1));
      fnnew=40*(nnew).^1.5-875*(nnew)+35000;
      err=(abs((nnew-n2)/nnew))*100;
      fprintf('%2d %f %f\n',iter,nnew,err)
    
      if nnew>n1
        n1=nnew;
      else
        n2=nnew;
      end
    
    end
    
    nnew
    iter
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Feb 2010
    Posts
    24
    I rearranged my m-file and i solved infinite loop problem but i think i have mistaken at calculating absolute relative approximate error at the end of each iteration, i think this command is wrong:

    err(iter)=(abs((nnew-n2)/nnew))*100;

    How can i fix this error calculation problem?



    My m-file:

    clc
    clear
    n1=60;
    n2=68;
    tol=1e-5;
    err0=3;
    iter=0;
    fprintf('iteration n relative approximate error\n')
    while err0>=tol
    iter=iter+1;
    fn1=40*(n1).^1.5-875*(n1)+35000;
    fn2=40*(n2).^1.5-875*(n2)+35000;
    nnew=n2-fn2*((n2-n1)/(fn2-fn1));
    fnnew=40*(nnew).^1.5-875*(nnew)+35000;
    err(iter)=(abs((nnew-n2)/nnew))*100;
    fprintf('%2d %f %f\n',iter,nnew,err(iter))
    err0=abs(fnnew);


    if nnew>n1
    n1=nnew;

    else
    n2=nnew;

    end

    end
    nnew
    iter
    Last edited by chronicals; April 24th 2010 at 05:13 AM.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by chronicals View Post
    I rearranged my m-file and i solved infinite loop problem but i think i have mistaken at calculating absolute relative approximate error at the end of each iteration, i think this command is wrong:

    err(iter)=(abs((nnew-n2)/nnew))*100;

    How can i fix this error calculation problem?
    What do you want it to be? That is the relative change as a percentage.

    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Feb 2010
    Posts
    24
    Quote Originally Posted by CaptainBlack View Post
    What do you want it to be? That is the relative change as a percentage.

    CB
    These are my results:

    iteration n relative approximate error
    1 62.759758 8.349685
    2 62.689966 8.470309
    3 62.691698 0.002762
    4 62.691697 0.000001


    nnew =
    62.6917

    iter =
    4


    how can second iterations' relative approximate error be 8.470309. I think this m-file is calculating 'relative approximate error' wrongly. Please help me fix this error command: err(iter)=(abs((nnew-n2)/nnew))*100
    Last edited by chronicals; April 24th 2010 at 06:32 AM.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by chronicals View Post
    These are my results:

    iteration n relative approximate error
    1 62.759758 8.349685
    2 62.689966 8.470309
    3 62.691698 0.002762
    4 62.691697 0.000001


    nnew =
    62.6917

    iter =
    4


    how can second iterations' relative approximate error be 8.470309. I think this m-file is calculating 'relative approximate error' wrongly. Please help me fix this error command: err(iter)=(abs((nnew-n2)/nnew))*100
    Because you are using n2 as your old value when computing the relative percentage error rather than the last estimate used, n2 is not always the last estimate.

    CB
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Feb 2010
    Posts
    24
    Quote Originally Posted by CaptainBlack View Post
    Because you are using n2 as your old value when computing the relative percentage error rather than the last estimate used, n2 is not always the last estimate.

    CB
    What should I use instead of n2, how I should change this error command?
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by chronicals View Post
    What should I use instead of n2, how I should change this error command?
    The following seems adequate:

    Code:
    clc
    clear
    n1=60;
    n2=68;
    tol=1e-3;
    err=3;
    iter=0;
    fprintf('iteration n relative approximate error\n')
    onew=68;
    
    while err>=tol
      iter=iter+1;
      fn1=40*(n1).^1.5-875*(n1)+35000;
      fn2=40*(n2).^1.5-875*(n2)+35000;
      nnew=n2-fn2*((n2-n1)/(fn2-fn1));
      fnnew=40*(nnew).^1.5-875*(nnew)+35000;
      err=(abs((nnew-onew)/nnew))*100;
      onew=nnew;
      fprintf('%2d %f %f\n',iter,nnew,err)
    
      if nnew>n1
        n1=nnew;
      else
        n2=nnew;
      end
    
    end
    
    nnew
    iter
    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. My Matlab function M-file for the Secant Method
    Posted in the Math Software Forum
    Replies: 4
    Last Post: December 3rd 2010, 06:48 AM
  2. secant method
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: May 28th 2010, 01:36 AM
  3. Secant Method + Interpolation
    Posted in the Advanced Math Topics Forum
    Replies: 2
    Last Post: October 18th 2009, 08:49 AM
  4. secant method
    Posted in the Math Software Forum
    Replies: 1
    Last Post: September 29th 2008, 11:03 PM
  5. Secant method with several variables
    Posted in the Advanced Math Topics Forum
    Replies: 0
    Last Post: April 9th 2008, 01:40 PM

Search Tags


/mathhelpforum @mathhelpforum