Professional Documents
Culture Documents
x0=-6
x1=6
Tolerance=0.001
%
==================================================
===================
% ****** To find root of given equation by false position method ******
% ************************ By Mahesha MG ******************************
% Date: 28/11/2012
%
==================================================
===================
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
The false-position method in Matlab is quite straight-forward. Assume a file f.m with
contents
function y = f(x)
y = x^3 - 2;
exists. Then:
>> format long
>> eps_abs = 1e-5;
>> eps_step = 1e-5;
>> a = 0.0;
>> b = 2.0;
>> step_size = Inf;
>> while (step_size >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >=
eps_abs ) )
c = (f(a)*b - f(b)*a)/(f(a) - f(b));
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
step_size = b - c;
b = c;
else
step_size = c - a;
a = c;
end
end
>> [a b]
ans = 1.259915864579067 2
>> abs(f(a))
ans =
0.0000246934256663
>> abs(f(b))
ans =
if ( f(a) == 0 )
r = a;
return;
elseif ( f(b) == 0 )
r = b;
return;
elseif ( f(a) * f(b) > 0 )
error( 'f(a) and f(b) do not have opposite signs' );
end
c_old = Inf;
for k = 1:N
if ( f(c) == 0 )
r = c;
return;
elseif ( f(c)*f(a) < 0 )
b = c;
else
a = c;
end
|f(a)| < |f(b)| and |f(a)| < eps_abs and return 'a', or
c_old = c;
end
%-----------------------for i=1:100
xr = xu-( f(xu)*(xl-xu) )/( f(xl)-f(xu) ); %compute xr
if f(xr)==0
return
elseif f(xr)*f(xl)<0 %root is left of xr
xu=xr;
else %root is right of xr
xl=xr;
end
end
x_false_position = xr
clc
= 0.001)
(default = 50)
a sign change
terminate
xrold = xr;
xr = xu - func(xu)*(xl - xu)/(func(xl) - func(xu));
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
test = func(xl)*func(xr);
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
36;
end
if ea <= es || iter >= maxit, break, end
end
root = xr;
ea = 0;
36;
end
if ea <= es || iter >= maxit, break, end
end
root = xr;