Tuesday, May 2, 2017

False Position method code implementation in Matlab


False Position method code implementation in Matlab

Problem of False position method:
Find the root of the following function where function = 5 * x^4 - 2.7 * x^2 - 2*x + 0.5 
User can take upper limit, lower limit and exact integral.


Code Implementation:



%Function
f = @(x) 5 * x^4 - 2.7 * x^2 - 2*x + 0.5;
l = input('Enter Lower Limit : ');
u = input('Enter Upper Limit : ');
previous_approximation = 0;

f_l = feval(f, l);
f_u = feval(f, u);
root = u -((f_u * (l-u))/ (f_l - f_u));
given_absoulte_error = input('Enter absolute error : ');
absolute_error = abs((root - previous_approximation) / root ) * 100;
previous_approximation = root;

while(absolute_error > given_absoulte_error)
    f_l = feval(f, l);
    f_u = feval(f, u);
    f_root = feval(f, root);
    
    if(f_l * f_root > 0)
        l = root;
    else
        u = root;
    end;
    
    f_l = feval(f, l);
    f_u = feval(f, u);
    
    
    root = u -((f_u * (l-u))/ (f_l - f_u));
    absolute_error = abs((root - previous_approximation) / root ) * 100;
    previous_approximation = root;
    
    fprintf('Root %f Error %f\n', root, absolute_error);
    
end;

disp('-------------------------------');
fprintf('Final Root = %f and absolute error = %f', root, absolute_error);

2 comments:

  1. I feel a lot more people need to read this, very good info!
    https://victoriaparlett.blogspot.com/

    ReplyDelete
  2. Tu blog me inspira constantemente con sus mensajes alentadores. Gracias por tu sabidurĂ­a y aliento. Contador De Clicks Online te permite realizar un seguimiento de tu progreso de clic y desafiarte a ti mismo para mejorar tu CPS. Es una gran herramienta para los jugadores que quieren mejorar.

    ReplyDelete