correction for loop in bisection method

조회 수: 17 (최근 30일)
Anupan Netyanun
Anupan Netyanun 2015년 6월 17일
답변: Tamima Nisat 2022년 1월 10일
Hi all, I tried to work on bisection method. However, that is an error at the loop. I guess that there might be a problem with inline code at the loop. Please help me correct them. Thank you,
A=input('Enter A: ');
B=input('Enter B: ');
e=0;
func = inline('exp(x)-2cos(x)' , ' X');
while e<0.0001
for i=1:10
C=(A+B)/2
FC=func(C);
FB=func(B);
if(FC*FB>0)
B=C;
else
A=C;
end
e=(abs((FB-FC)/FB))*100
end
end

답변 (2개)

Mischa Kim
Mischa Kim 2015년 6월 17일
편집: Mischa Kim 2015년 6월 17일
Anupan, check out
A = input('Enter A: ');
B = input('Enter B: ');
func = @(x) exp(x)-2*cos(x); % use instead anonymous functions
e = 1; % set e > 0.0001 to start search
while e>0.0001 % one loop is sufficient -> remove for loop
C = (A+B)/2;
FC = func(C);
FB = func(B);
if(FC*FB>0)
B = C;
else
A = C;
end
e = (abs((FB-FC)/FB))*100
end

Tamima  Nisat
Tamima Nisat 2022년 1월 10일
f = @(x)(2*x^2-5*x+3)
xl=0;
xu=10;
error=0.001;
while(f(xl)*f(xu)>0)
xl=input('New input for lower ');
xu=input('New input for upper ');
end
while(abs(xu-xl)>error)
xc=(xu+xl)/2;
if(f(xl)*f(xc)<0)
xu=xc;
else
xl=xc;
end
end
fprintf('result=%f',xc)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by