finding root using false position method

346 ビュー (過去 30 日間)
Ayda
Ayda 2011 年 11 月 22 日
コメント済み: Walter Roberson 2024 年 2 月 7 日
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().
  2 件のコメント
Ayda
Ayda 2011 年 11 月 22 日
thanxs
Fangjun Jiang
Fangjun Jiang 2011 年 11 月 22 日
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 Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by