Results 1 to 5 of 5

Math Help - Halleys metyhod matlab help

  1. #1
    Junior Member
    Joined
    May 2009
    Posts
    45

    Halleys metyhod matlab help

    This is my code, to find the f(x)=0 positive root, using halleys method

    2 m files

    1. function file
    function [y,dydx,ddyddxx]=h(x)
    % The function is expressed, and the 1st and 2nd derivatives are noted
    y=exp(cos(x)^2)-x^2;
    dydx=-sin(2*x)*exp(cos(x)^2)-2*x;
    ddyddxx=2*exp(cos(x)^2)-4*cos(x)^4*exp(cos(x)^2)-2;

    end

    2. Halleys method
    clear all
    % clears all the stored variable
    format long
    %formats the outputs to 10 significant figures
    x(1) = 0;
    %left bound
    x(2) = 1000;
    %right bound
    eps = 1e-10;
    count = 2;
    while abs(x(count-1)-x(count)) > eps
    % calculating the derivatives
    [y,dydx,ddyddxx]= h(x(count));
    x(count+1) = x(count) - ((y*dydx)/(((dydx)^2)-(ddyddxx*y)/2));
    fprintf('The root is approximately %1.10f\n',x(count+1));
    count = count + 1;
    end
    fprintf('The solution of exp(cos(x)^2)-x^2 is approximately %1.10f\nThis algorithm took %u iterations to complete\n',x(count),count-2)

    OK my problem is my initial conditions, as all i have done is put halleys method into newtons method. I need to "using an initial condition value of x1=1000, apply this iterative procedure to f(x) untill |f(xn)|<10^-10 or the number of iterations is 1000, also my program needs to oputput the number of iterations and the root to 10 signif figures. my code has found x to 10 signifs not f(xn) apparently i need to make it like a controlled bisection, and have an || and && statements somewhere... i litterally have no idea how to do this... please can someone help me?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Junior Member
    Joined
    May 2009
    Posts
    45
    the conrolled bisection i have give is:

    clear all
    % clears all the stored variable
    format long
    xa = pi/2;
    xb = pi;
    xc(1) = pi/2;
    xc(2) = pi;
    eps = 1e-10;
    delta = 1e-10;
    count = 2;
    M = 100;
    while ((abs(xc(count)-xc(count-1)) > eps) || (abs(f(xc(count)) - f(xc(count-1)))) > delta) && (count < M+2)
    count = count + 1;
    % claculating the midpoint
    xc(count) = (xa+xb)/2;
    % determining whether the midpoint lies between [xa,xc] ot [xc,xb].
    % Then changing the values of xa and xb according.
    if f(xa)*f(xc(count)) < 0;
    xb = xc(count);
    else
    xa = xc(count);
    end
    fprintf('The root lies between %1.10f and %1.10f\n',xa,xb);
    end
    if (count == M+2)
    fprintf('This series did not \n');
    else
    fprintf('This series converged in %u steps to %f\n',count-2, xc(count));
    end

    I dont know what to change in my code... I am very very new to matlab, im not a good coder, i just try to work from example...
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Assuming your code for Halley's method is correct:

    Code:
    clear all       % clears all the stored variable
    format long  %formats the outputs to 10 significant figures
    
    x=zeros(1,1000);
    x(1) = 1;
    [y,dydx,ddyddxx]= h(x(1));
    
    eps = 1e-10;
    count = 1;
    
    while (abs(y) > eps) && (count<=1000)
    
       % calculating the derivatives
       [y,dydx,ddyddxx]= h(x(count));
    
       x(count+1) = x(count) - ((y*dydx)/(((dydx)^2)-(ddyddxx*y)/2));
    
       fprintf('The root is approximately %1.10f\n',x(count+1));
       count = count + 1;
    
    end
    
    fprintf('The solution of exp(cos(x)^2)-x^2 is approximately %1.10f\nThis algorithm took %u iterations to complete\n',x(count),count-2)
    Note the initial guess should be near the root.

    CB
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Junior Member
    Joined
    May 2009
    Posts
    45
    Is it possibly like this? if i use M is the max values, it donest go past 1000 iterarations, if i take out the first part of the while loop. Idk im confused


    clear all
    % clears all the stored variable
    format long
    %formats the outputs to 10 significant figures
    x(2) = 1000;
    eps = 1e-10;
    count = 2;
    M = 1000;
    %Maximum nuber of iterations
    delta = 1e-10;
    while abs(x(count-1)-x(count)) > eps || (abs(h(x(count)) - h(x(count-1))) > delta) && count<M;
    % calculating the derivatives
    [y,dydx,ddyddxx]= h(x(count));
    x(count+1) = x(count) - ((y*dydx)/(((dydx)^2)-(ddyddxx*y)/2));
    fprintf('The root is approximately %1.10f\n',x(count+1));
    count = count + 1;
    end
    fprintf('The solution of exp(cos(x)^2)-x^2 is approximately %1.10f\nThis algorithm took %u iterations to complete\n',x(count),count-2)
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    May 2009
    Posts
    45
    ahh i see thanks heaps
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. ode 45 (matlab)
    Posted in the Math Software Forum
    Replies: 3
    Last Post: October 8th 2010, 12:30 PM
  2. Matlab Help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 17th 2010, 08:13 AM
  3. i need help in matlab
    Posted in the Math Software Forum
    Replies: 0
    Last Post: March 2nd 2010, 07:49 PM
  4. help with matlab please
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 7th 2009, 10:51 PM
  5. MATLAB
    Posted in the Math Software Forum
    Replies: 4
    Last Post: September 25th 2008, 01:47 PM

Search Tags


/mathhelpforum @mathhelpforum