Newton Method and Bisection Method - Matlab Scripts

Posted Apr 27, 2009 by djmcv90 / comments 0 comments / Print / Font Size Decrease font size Increase font size

Here are some iteration methods ive used at uni

For more information about the inputs see the other matlab script on the secant method.

Here are 2 scripts on Newtons method and the Bisection method.

Newtons methods is based on using the derivative at a point to help calculate a closer value and this continues to find a closer and closer value.

The bisection method, as the name suggests, halves the distance between 2 points continuously until the point in between is accurate enough.

The bisection method is quite slow compared to newtons method as it only halves the distance, yet newtons method uses the gradient to approximate quickly with fewer iterations.

Newtons Method

function x = newton_method(f_str,df_str,x0,n)
% in the form f(x) = 0

f = inline(f_str);
df = inline(df_str);
disp('Number of Iterations =')
disp(0)

x = x0;
xn = 0;

disp('Xn =')
disp(x)
disp('f(x) =')
disp(f(x))
disp('f''(x) =')
disp(df(x))

for i = 1:n
    xn = x - (f(x)/df(x));
    if x == xn
        disp('This is the Highest Accuracy Achievable')
        i = i-1;
        break
    end
    x = xn;
disp('Number of Iterations =')
disp(i)
disp('Xn =')
disp(x)
disp('f(x) =')
disp(f(x))
disp('f''(x) =')
disp(df(x))
end
disp('Number of Iterations Done:')
disp(i)
end

Bisection Method

function x = bisection(f_str,x0,x1,n)
% in the form f(x) = 0

f = inline(f_str);
a = x0;
b = x1;
disp('Number of Iterations =')
disp(0)
disp('a =')
disp(a)
disp('b =')
disp(b)

x = (a+b)/2;
disp('x =')
disp(x)
disp('f(a)=')
disp(f(a))
disp('f(b)=')
disp(f(b))
disp('f(x)=')
disp(f(x))

for i = 1:(n)
    if (f(x) > 0 && f(b) > 0) || (f(x) < 0 && f(b) < 0)
        b = x;
    elseif (f(x) > 0 && f(a) > 0) || (f(x) < 0 && f(a) < 0)
        a = x;
    else disp('This is the Highest Accuracy Achievable')
        i = i-1;
        break
    end
    disp('Number of Iterations =')
    disp(i)
        disp('a =')
        disp(a)
        disp('b =')
        disp(b)
   
    x = (a+b)/2;
    disp('x =')
    disp(x)
    disp('f(a)=')
    disp(f(a))
    disp('f(b)=')
    disp(f(b))
    disp('f(x)=')
    disp(f(x))
end
disp('Number of Iterations Done:')
disp(i)
end

Other Iterative and Mathematical Method using Matlab and also other Mathematical Examples:

>>> Romberg Numerical Integration - Matlab Script

>>> Simpson's Rule and Trapezoidal Rule of Numerical Integration - Matlab Scripts

>>> Secant Method of finding Roots - Matlab Script

>>> Lagrange Method and Newton Divided Difference Method - Matlab Scripts

Other Articles I have writen include:

>>> How to Build a Computer

>>> Restore to Factory Settings with Recovery Partition

>>> how to fix usbhub.sys blue screen error crashes

Rate this Article:

Be the first to rate me.


* You must be logged in order to leave comments, please login or join us.

Comments

No comments yet.



Bookmark and Share
Sign up for our email newsletter
Name:
Email: