correction for loop in bisection method

21 vues (au cours des 30 derniers jours)
Anupan Netyanun
Anupan Netyanun le 17 Juin 2015
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

Réponses (2)

Mischa Kim
Mischa Kim le 17 Juin 2015
Modifié(e) : Mischa Kim le 17 Juin 2015
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 le 10 Jan 2022
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)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by