finding root using false position method
조회 수: 166 (최근 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
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
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
Fangjun Jiang
2011년 11월 22일
A better approach is to check whether f(x0)*f(x1)<0 right after the input().
추가 답변 (4개)
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')
댓글 수: 0
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
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
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
2024년 2월 7일
This solution does not explain how to use False Position Method, so it is not clear how it answers the question?
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!