Infinite loop in False postion method
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
R Abhinandan
am 27 Okt. 2020
Kommentiert: Cris LaPierre
am 27 Okt. 2020
Here is my code for false position method and im getting a infinite loop, i want it to stop at tol=10^-4, can someone help?
a=0.5;
b=1;
tol=0.0001;
counter=0;
f =@(x) exp(x)-3*x^2;
while abs(b-a)>tol
c= b - (f(b)* (b-a)/(f(b)-f(a)));
if f(a)*f(c)<0
b=c;
end
if f(a)*f(c)>0
a=c;
end
counter=counter+1;
er=abs(a-b);
errorcounter(counter)=er;
end
figure
hold on
plot(errorcounter)
title('Convergence of False Position')
ylabel('Error')
xlabel('Iterations')
axis ([0 5 0 9e-04])
1 Kommentar
Cris LaPierre
am 27 Okt. 2020
btw, hold on is unnecessary here. When used, it should always be paired with hold off.
Akzeptierte Antwort
Cris LaPierre
am 27 Okt. 2020
I don't understand what your approach is, but by converting your while loop to a for loop, I can see that your error stops decreasing at 0.09. Here's a simplified version of your code.
a=0.5;
b=1;
f =@(x) exp(x)-3*x^2;
% while abs(b-a)>tol
for loop = 1:10
c= b - (f(b)* (b-a)/(f(b)-f(a)));
if f(a)*f(c)<0
b=c;
elseif f(a)*f(c)>0
a=c;
end
er=abs(a-b)
end
2 Kommentare
Cris LaPierre
am 27 Okt. 2020
Bearbeitet: Cris LaPierre
am 27 Okt. 2020
I understand. Check your algorithm. You have not implemented it correctly.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!