Results 1 to 2 of 2

Math Help - mathlab- root finding

  1. #1
    Newbie
    Joined
    Oct 2009
    Posts
    1

    mathlab- root finding

    Hey im trying to find the secant method.
    I am trying to modify my newton's code, but am not getting anywhere.
    Here is the code I am using for newton's.

    >> format long
    >> c=1
    c =
    1
    >> n=0;
    >> x=1.5;
    >> f=@(x) ((x)^4-c);
    >> fder=@(x) (4*(x)^3);
    >> while n<100
    newx=x-(f(x)/fder(x));
    n=n+1
    x=newx
    end

    The secant formula is xn+1=xn-(xn-xn-1)*f(xn)/f(xn)-f(xn-1).
    Thus I need two points. Can someone please write out a nice matlab code that I can use (with say two points being 1.5, and 1.3)?

    thanks
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    Maybe something like this would work for you:

    Code:
    function [x err iter]  = mhfSecant(f,x1,x2)
    maxerr = 1e-6;
    maxiter = 100;
    iter = 0;
    while abs(x1-x2) > maxerr && iter < maxiter
        xnew = x2-(x2-x1)/(f(x2)-f(x1))*f(x2);
        x1 = x2;
        x2 = xnew;
        iter = iter +1;
    end
    x = (x1+x2)/2;
    err = f(x);
    Code:
    EDU>> f=@(x) ((x)^4-1);
    EDU>> [x e i] = mhfSecant(f,1.5,1.3)
    
    x =
    
        1.0000
    
    
    e =
    
      6.9803e-009
    
    
    i =
    
         7
    
    EDU>>
    Im not very familiar with this method so I cant guarantee this is the right solution.


    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. check my errors,i can't run mathlab...
    Posted in the Math Software Forum
    Replies: 5
    Last Post: March 11th 2010, 08:22 AM
  2. Transcendental Equation-Mathlab
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: May 20th 2009, 01:23 AM
  3. Mathlab code
    Posted in the Math Software Forum
    Replies: 1
    Last Post: March 26th 2009, 04:56 AM
  4. Mathlab help Bisection Method
    Posted in the Math Software Forum
    Replies: 4
    Last Post: June 16th 2008, 06:05 AM
  5. Actualization of eqaution in MathLab
    Posted in the Advanced Math Topics Forum
    Replies: 2
    Last Post: May 8th 2005, 08:55 PM

Search Tags


/mathhelpforum @mathhelpforum