finding root using false position method

350 次查看(过去 30 天)
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 个评论
Samanta
Samanta 2024-2-7
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

请先登录,再进行评论。

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-11-22
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().

更多回答(4 个)

Mahesha MG
Mahesha MG 2012-12-14

Polash Roy
Polash Roy 2021-4-10
编辑:Walter Roberson 2024-2-7
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 2021-12-3
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 个评论
Walter Roberson
Walter Roberson 2021-12-3
编辑:Walter Roberson 2021-12-3
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));

请先登录,再进行评论。


Samanta
Samanta 2024-2-7
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 个评论
Walter Roberson
Walter Roberson 2024-2-7
This solution does not explain how to use False Position Method, so it is not clear how it answers the question?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by