finding root using false position method

166 visualizaciones (últimos 30 días)
Ayda
Ayda el 22 de Nov. de 2011
Comentada: Walter Roberson el 7 de Feb. de 2024
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 comentario
Samanta
Samanta el 7 de Feb. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 22 de Nov. de 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 comentarios
Ayda
Ayda el 22 de Nov. de 2011
thanxs
Fangjun Jiang
Fangjun Jiang el 22 de Nov. de 2011
A better approach is to check whether f(x0)*f(x1)<0 right after the input().

Iniciar sesión para comentar.

Más respuestas (4)

Mahesha MG
Mahesha MG el 14 de Dic. de 2012

Polash Roy
Polash Roy el 10 de Abr. de 2021
Editada: Walter Roberson el 7 de Feb. de 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 el 3 de Dic. de 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 comentario
Walter Roberson
Walter Roberson el 3 de Dic. de 2021
Editada: Walter Roberson el 3 de Dic. de 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));

Iniciar sesión para comentar.


Samanta
Samanta el 7 de Feb. de 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 comentario
Walter Roberson
Walter Roberson el 7 de Feb. de 2024
This solution does not explain how to use False Position Method, so it is not clear how it answers the question?

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by