correction for loop in bisection method
조회 수: 17 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
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
댓글 수: 0
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)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!