False position infinite loop
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Allie
el 18 de Nov. de 2014
Respondida: My
el 21 de Dic. de 2025 a las 20:01
Hi there.
This function gets stuck in an infinite loop. Do you all have any suggestions for me?
function [R, E] = myFalsePosition(f, xL, xR, tol)
if sign (f(xL)) == sign(f(xR))
error 'you are arrested!!!'
end
yL = f(xL);
yR = f(xR);
new_x = ((xR*yL) - (xL*yR))/(yL - yR);
new_y = f(new_x);
e = abs(new_y);
E = e;
while e > tol
if f(xL)*f(new_x) > 0
xL = new_x;
yL = f(xL);
else
xR = new_x;
yR = f(xR);
end
end
new_x = ((xR*yL) - (xL*yR))/(yL - yR);
new_y = f(new_x);
R = [R new_x];
e = abs(new_y);
E = [E e];
end
0 comentarios
Respuesta aceptada
Image Analyst
el 18 de Nov. de 2014
Well you could tell us the values you used for f, xL, xR, tol when you called it. And you can use the debugger to figure out why "e" never falls below "tol". Using the debugger yourself will be your fastest course of action , rather than trying to debug it via back-and-forth Answers forum postings, which can take hours.
0 comentarios
Más respuestas (2)
per isakson
el 18 de Nov. de 2014
Editada: per isakson
el 19 de Nov. de 2014
I reformatted your function.
Neither e nor tol is changed in the while-loop. If   e > tol   is true when entering the loop it will remain true.
Possible, the end of the loop is not in the position, which you intended.
0 comentarios
My
el 21 de Dic. de 2025 a las 20:01
function p = myfalseposition(f,p0,p1,TOL,N0)
q0 = f(p0);
q1 = f(p1);
for i = 1:N0
% False Position formülü
p = p1 - q1*(p1-p0)/(q1-q0);
if abs(p-p1) < TOL
return
end
q = f(p);
if q*q1 < 0
p0 = p1;
q0 = q1;
end
p1 = p;
q1 = q;
end
disp('Method failed');
end
%Main
clear; clc;
f = @(x) cos(x) - x;
p0 = 0.5;
p1 = pi/2;
TOL = 1e-3;
N0 = 100;
p = myfalseposition(f,p0,p1,TOL,N0)
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!