why is my for loop not working?
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
KIPROTICH KOSGEY
hace alrededor de 24 horas
Movida: Image Analyst
hace alrededor de 18 horas
y=@(x) x^2-4;
x1=input('Enter the value of x1:');
x2=input('Enter the value of x2:');
x3=input('Enter the value of x3:');
for i=1:100
L1=(x2-x3)/(x3-x1);
xf=x3+L1*(x3-x1);
y1=abs(y(x1));
y2= abs(y(x2));
y3=abs(y(x3));
yf=abs(y(xf));
ynext=sort([y1 y2 y3 yf]);
if y1==max(ynext)
x1=xf;
elseif y2==max(ynext)
x2=xf;
else
x3=xf;
end
if y(xf)<10^-10
break
end
end
fprintf('the root: %f\n the number of iterations: %d\n',xf,i)
0 comentarios
Respuestas (1)
dpb
hace alrededor de 6 horas
Movida: Image Analyst
hace alrededor de 5 horas
Probably because you're testing floating point values for exact equality -- see isapprox and ismembertol
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!