Tuesday, November 27, 2012

MATLAB - False Position Method

false_position
% =====================================================================
% ****** To find root of given equation by false position method ******
% ************************ By Mahesha MG ******************************
% Date: 28/11/2012
% =====================================================================

display('Equation is x^2+x-2 = 0')
i=1;
while(i)
    xl=input('Enter lower value:');
    xu=input('Enter upper value: ');
    e=input('Enter accuracy: ');
    if equan(xl)*equan(xu)<0
        i=0;
    else

        warning('Enter proper range');
    end
end
if equan(xl)<0
    xn=xl;
    xp=xu;
else
    xn=xu;
    xp=xl;
end

xm=xl;
while (abs(equan(xm))>e)
   xm=(xn*equan(xp)-xp*equan(xn))/(equan(xp)-equan(xn));
    if equan(xm)<0
        xn=xm;
    else
        xp=xm;
    end
end

Root=xm

SAMPLE OUTPUT:

>> false_position
Equation is x^2+x-2 = 0
Enter lower value:-2
Enter upper value: 2
Enter accuracy: 1e-4
Warning: Enter proper range 
> In false_position at 15 
Enter lower value:-1
Enter upper value: 2
Enter accuracy: 1e-4

Root =

    1.0000

15 comments:

  1. if equan(xl)*equan(xu)<0

    Doesn't that come up with an error?

    ReplyDelete
    Replies
    1. if equan(xl)*equan(xu)<0
      this statement makes sure that at lower value and upper value of x, function will have opposite signs. this is the 'must' condition for false position method. Once this condition is satisfied, 0 is assigned to i. So the while(i) loop ends.

      Delete
    2. Dear Mr. Mahesha MG ,,,,
      I am not getting output after this programm it showing error as below.

      Error using ==> le
      Not enough input arguments.

      Error in ==> Untitled2 at 12
      e=input('Enter accuracy: ');

      Delete
    3. Hi
      create a file equan.m and copy the following lines to it & save.

      % Equation to be solved
      function[eqn]=equan(x);
      eqn=x^2+x-2;

      Now run false position. It should work. The function is defined in equan.m file and it is called in the present code. Since it is already mentioned in bisection code (see code page), I didn't mention that in this page.
      Thanks for intimating the problem.

      Delete
  2. hi is there any truncation of iteration criteria?

    ReplyDelete
  3. Equation is x^2+x-2 = 0
    Enter lower value:0
    Enter upper value: 9
    Enter accuracy: 1e-4
    ??? Undefined function or method 'equan' for input arguments of type 'double'.

    im entering these values and getting this error....

    ReplyDelete
    Replies
    1. Hi
      create a file equan.m and copy the following lines to it & save.

      % Equation to be solved
      function[eqn]=equan(x);
      eqn=x^2+x-2;

      Now run false position. It should work. The function is defined in equan.m file and it is called in the present code. Since it is already mentioned in bisection code (see code page), I didn't mention that in this page.
      Thanks for intimating the problem.

      Delete
  4. Can this method be used for another function>

    ReplyDelete
  5. Could this method be used for any other function?

    ReplyDelete
  6. Can this method be used for another function>

    ReplyDelete
  7. clc
    clear all
    syms x
    f=inline(input('enter the equation:','s'))

    i=1;
    while(i)
    xl=input('Enter lower value:');
    xu=input('Enter upper value: ');
    e=input('Enter accuracy: ');
    if f(xl)*f(xu)<0
    i=0;
    else

    display('root not located between the entered values');
    end
    end
    if f(xl)<0
    xn=xl;
    xp=xu;
    else
    xn=xu;
    xp=xl;
    end

    xm=xl;
    t=1;
    while (abs(f(xm))>e)
    xm=(xn*f(xp)-xp*f(xn))/(f(xp)-f(xn));
    fprintf('%d)\tx%d=%f\n',t,t,xm)
    t=t+1;

    if f(xm)<0
    xn=xm;
    else
    xp=xm;
    end
    end

    Root=xm


    please use this code instead to find out roots for any expression.. just enter an equation with proper syntax in the command window. e.g enter the expression like this when asked x^2+x-2 . the results will be shown in table form

    ReplyDelete
  8. Is it not the formula xn-((equan(xn)-(xp-xn)/(equan(xp)-equan(xn)) the formula in getting xm or is your formula derived from that formula? Or maybe an alternative formula. I'm confused to what formula am i going to use when i only know the formula given to us by our school proffesors. Thats all ty.

    ReplyDelete
    Replies
    1. xn-((equan(xn)*(xp-xn)/(equan(xp)-equan(xn))is right. What I used is derived from this itself. Take common denominator and simplify. You will get the formula that I used.

      Delete
  9. For getting xm, can we use the formula xn-(equan(xn)-(xp-xn)/(equan(xp)-equan(xn)))?

    ReplyDelete