finding root using false position method

166 views (last 30 days)
Good evening\morning
I try to write a code that calculate the root of a nonlinear function using False Position Method, but I get an infinite loop. I use the same loop for the Bisection Method and it's work.
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
  1 Comment
Samanta
Samanta on 7 Feb 2024
1.Use the False Position Method to find the root of the equation e^x−3x=0. Start with the initial guesses x_0=0 and x_1 =1

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
Do a plot to find out the curve. If you put the right initial value, it could solve the problem.
ezplot(f)
x0=-6
x1=6
Tolerance=0.001
It reached the end at i==590
A better approach is to check whether f(x0)*f(x1)<0 right after the input().
  2 Comments
Fangjun Jiang
Fangjun Jiang on 22 Nov 2011
A better approach is to check whether f(x0)*f(x1)<0 right after the input().

Sign in to comment.

More Answers (4)

Mahesha MG
Mahesha MG on 14 Dec 2012

Polash Roy
Polash Roy on 10 Apr 2021
Edited: Walter Roberson on 7 Feb 2024
clc
clear all
close all
f=@(r) exp((-5e-3)*r)*cos((sqrt(2000-.01*r^2)*.05))-.01;
a=100;
b=550;
for i=1:10
x0=a;
x1=b;
fprintf('\n Hence root lies between (%.4f,%.0f)',a,b)
x2(i)=x0-(x1-x0)/(f(x1)-f(x0))*f(x0);
if f(x2(i))*f(x0)>0
b=x2(i);
else
a=x2(i);
end
fprintf('\n Therefore, x2=%.4f \n Here, f(x20=%.4f',x2(i),f(x2(i)))
p=x2(i);
end
for i=1:10
eror(i)=p-x2(i);
end
Answer=p
plot(eror)
grid on;
title('Plot of error')
xlabel('iterations')
ylabel('Error')

Aman Pratap Singh
Aman Pratap Singh on 3 Dec 2021
clc
% Setting x as symbolic variable
syms x;
% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
% Finding Functional Value
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
fb = eval(subs(y,x,b));
else
a =c;
fa = eval(subs(y,x,a));
end
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c);
end
  1 Comment
Walter Roberson
Walter Roberson on 3 Dec 2021
Edited: Walter Roberson on 3 Dec 2021
You should never eval() a symbolic expression. Symblic expressions are not character vectors containing MATLAB code. Sometimes they look like MATLAB code, but they contain parts that are not MATLAB, and some of the functions have a different parameter order than MATLAB uses.
If you have fully substituted for all numeric variables, then
fa = double(subs(y,x,a));
fb = double(subs(y,x,b));

Sign in to comment.


Samanta
Samanta on 7 Feb 2024
Use the False Position Method to find the root of the equation e^x−3x=0. Start with the initial guesses x_0=0 and x_1 =1
  1 Comment
Walter Roberson
Walter Roberson on 7 Feb 2024
This solution does not explain how to use False Position Method, so it is not clear how it answers the question?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!