Help with my bisection method of a certain function!
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ajit Koduri
el 27 de Sept. de 2014
Respondida: Matt J
el 27 de Sept. de 2014
Hello, I am a new user of MatLab and recently had to write up a bisection method program for the function f(x) = x + 2*cos(x).
This is what I got:
function[f] = hw3_bisection(tol,N,a,b)
f = @(x) (x + 2*cos(x));
for counter = 1:N
x = (a+b)/2;
while abs(f(x)) < tol
x = (a+b)/2;
if f(x) == 0
f = x;
end
if f(a) * f(x) > 0
a = x;
else
b = x;
end
end
end
but the problem for me is that the output is always x + 2*cos(x), and even if I say disp(x) it gives me ans = x + 2*cos(x) and the (a+b)/2 without going through the rest of the loop. How do I fix this to get the real solution?
0 comentarios
Respuesta aceptada
Matt J
el 27 de Sept. de 2014
You are using 'f' for 2 different things in your function. Use a different variable for the value returned by the function.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Startup and Shutdown 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!