Can't get a proper solution with modified secant method

조회 수: 13 (최근 30일)
Ishmum Monjur Nilock
Ishmum Monjur Nilock 2020년 8월 16일
답변: Peter 2023년 3월 9일
Hi all, I have built a code for modified secant method to find the root of the function f(x)= which is given below. The output of the code should give an answer of about 0.5671 but instead it is giving 56.71.... something answer. HOw to solve this problem? Code is mentioned below:
clear all
close all
clc
n=100;
f=@(x) exp(-x)-x;
dx=0.01;
x0=1;
x(1)=x0;
i=0;
err=0.0048;
for i=1:n
x(i+1)=x(i)-((f(x(i)*dx)/(f(x(i)+dx)-f(x(i)))));
if abs((x(i+1)-x(i))/x(i+1))*100<err
roots=x(i);
break;
end
end
disp(roots)

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 16일
x(i+1)=x(i)-((f(x(i)*dx)/(f(x(i)+dx)-f(x(i)))));
Notice that you have f(x(i)*dx) and dx is 0.01 . When you find the root of f(x(i)/100) == 0, then x(i) would be 100 times the root of f(x)
Meanwhile in the denominator you have f(x(i)+dx) with +dx not *dx . It seems to me you need to be consistent

추가 답변 (1개)

Peter
Peter 2023년 3월 9일
clear all
close all
clc
n=100;
f=@(x) exp(-x)-x;
dx=0.01;
x0=1;
x(1)=x0;
i=0;
err=0.0048;
for i=1:n
x(i+1)=x(i)-((f(x(i)*dx)/(f(x(i)+dx)-f(x(i)))));
if abs((x(i+1)-x(i))/x(i+1))*100<err
roots=x(i);
break;
end
end
disp(roots)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by