0% found this document useful (0 votes)
610 views12 pages

False Position Matlab

The MATLAB code implements the false position method to find the root of a function. It prompts the user to input values for x0, x1, and the tolerance. It then defines an anonymous function f(x) and iterates to find a value x2 where the absolute value of f(x2) is less than the tolerance. If f(x0) and f(x2) have opposite signs, it updates x1 to x2, otherwise it updates x0 to x2. This continues until the tolerance condition is met or infinity iterations are reached.

Uploaded by

Matt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
610 views12 pages

False Position Matlab

The MATLAB code implements the false position method to find the root of a function. It prompts the user to input values for x0, x1, and the tolerance. It then defines an anonymous function f(x) and iterates to find a value x2 where the absolute value of f(x2) is less than the tolerance. If f(x0) and f(x2) have opposite signs, it updates x1 to x2, otherwise it updates x0 to x2. This continues until the tolerance condition is met or infinity iterations are reached.

Uploaded by

Matt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

clc

x0 = input('enter the value of x0 = ');


x1 = input('enter the value of x1 = ');
tolerance=input('inter the tolerance = ');
f =@(x) sin(2*pi*x)+ exp(1.2*x) + x - 2.5;
for i=0:inf
x2= x1 - (f(x1)* (x1-x0)/(f(x1)-f(x0)))
c = f(x2)
absolute_c= abs(c);
if absolute_c < tolerance
break
end
if f(x0)*c <0
x1=x2;
continue
else
x0=x2;
continue
end
end
i
ezplot(f)

x0=-6
x1=6
Tolerance=0.001

%
==================================================
===================
% ****** 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

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 =

Thus, we would choose 1.259915864579067 as our approximation to the cube-root


of 2, which has an actual value (to 16 digits) of 1.259921049894873.

An implementation of a function would be


function [ r ] = false_position( f, a, b, N, eps_step, eps_abs )
% Check that that neither end-point is a root
% and if f(a) and f(b) have the same sign, throw an exception.

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

% We will iterate N times and if a root was not


% found after N iterations, an exception will be thrown.

c_old = Inf;

for k = 1:N

% Find the false position


c = (a*f(b) + b*f(a))/(f(b) - f(a));

% Check if we found a root or whether or not


% we should continue with:
%

[a, c] if f(a) and f(c) have opposite signs, or

[c, b] if f(c) and f(b) have opposite signs.

if ( f(c) == 0 )
r = c;
return;
elseif ( f(c)*f(a) < 0 )
b = c;
else
a = c;
end

% If |b - a| < eps_step, check whether or not


%

|f(a)| < |f(b)| and |f(a)| < eps_abs and return 'a', or

|f(b)| < eps_abs and return 'b'.

if ( abs( c - c_old ) < eps_step )


if ( abs( f(a) ) < abs( f(b) ) && abs( f(a) ) < eps_abs )
r = a;
return;
elseif ( abs( f(b) ) < eps_abs )
r = b;
return;
end
end

c_old = c;
end

error( 'the method did not converge' );


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

x0 = input('enter the value of x0 = ');


x1 = input('enter the value of x1 = ');
tolerance=input('inter the tolerance = ');
f =@(x) 9*(exp(-x))*sin(2*pi*x) - 3.5; %input the desired function
%this is just a sample function
for i=0:inf
x2= x1 - (f(x1)* (x1-x0)/(f(x1)-f(x0)))
c = f(x2)
absolute_c= abs(c);
if absolute_c < tolerance
break
end
if f(x0)*c <0
x1=x2;
continue
else
x0=x2;
continue
end
end

function root = falsepos(func,xl,xu,es,maxit)


% falsepos(xl,xu,es,maxit):
% uses the false position method to find the root
% of the function func
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = (optional) stopping criterion (%) (default
% maxit = (optional) maximum allowable iterations
% output:
% root = real root
func = @(x) 9*(exp(-x))*sin(2*pi*x) - 3.5;
xl = input('enter the value of xl = ');
xu = input('enter the value of xu = ');
es = input('stopping criterion = ');
maxit = input('max iterations = ');
if func(xl)*func(xu)>0 %if guesses do not bracket
error('no bracket') %display an error message and
end
% default values
if nargin<5, maxit=50; end
if nargin<4, es=0.001; end
% false position
iter = 0;
xr = xl;
while (1)

= 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;

function root = falsepos(func,xl,xu,es,maxit)


% falsepos(xl,xu,es,maxit):
% uses the false position method to find the root
% of the function func
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = (optional) stopping criterion (%) (default = 0.001)
% maxit = (optional) maximum allowable iterations (default = 50)
% output:
% root = real root
a = input('Function = ');
func = inline (a)
xl = input('enter the value of xl = ');
xu = input('enter the value of xu = ');
es = input('stopping criterion = ');
maxit = input('max iterations = ');
if func(xl)*func(xu)>0 %if guesses do not bracket a sign change
error('no bracket') %display an error message and terminate
end
% default values
if nargin<5, maxit=50; end
if nargin<4, es=0.001; end
% false position
iter = 0;
xr = xl;
while (1)
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;

You might also like