Why won't my code run? Matlab just says its busy when i run it?

when i put this in the command window I matlab just keeps saying its busy and doesn't give me any results. bisection(@(x)(cos(x)-x),0,1,1e-6)

1 comentario

function [xs, fs, n] = bisection(f, a, b, tol)
% This function is for finding a single isolated root of f over the interval [a,b]
% When do you get an error
fa = feval(f, a);
fb = feval(f,b);
if sign(fa) * sign(fb) == 1
xs = nan;
fs = nan;
n = nan;
fprintf('error, check your inputss...');
return
elseif a >= b;
xs = nan;
fs = nan;
n = nan;
fprintf('error, check your inputss...');
return
end
n=0; % initialize itteration
x0 = (a + b)/2;
f0 = feval(f, x0);
xf = x0;
while (abs(x0 - xf)) >= tol*(1+abs(x0))||abs(f0)>=tol;
xf = x0;
x0 = (a + b)/2;
n = n + 1;
if (fa) * (f0) < 0
a = x0;
else
b = x0;
end
end
xs = xo;
fs = f0;
% Printing final results
fprintf('\n Bisection Method For Functions W/ Multiple Roots \n Homework 3 Problem 1')
fprintf('\n\n xs = %f produces \n fs = %f \n n = %i iterations\n', xs, fs, n-1);
end
%bisection(@(x)(cos(x)-x),0,1,1e-6)

Iniciar sesión para comentar.

Respuestas (2)

Hi,
I did not try the code, but I would strongly recommend to have some limit on the number of iterations, something like
it = 0;
itMax = 25;
while it <= itMax && (abs(x0 - xf)) >= tol*(1+abs(x0))||abs(f0)>=tol
% do what you want to do
it = it + 1;
end
% test no of iterations:
if it > itMax
warning('Maximum number of iterations reached. Result may be wrong or inaccurate.')
end
For debugging use the debugger: put a breakpoint inside your loop and go step by step an watch the variables to see what's happening.
Titus

2 comentarios

Sorry, I did not see that you already count iterations. Use your variable n instead of "my" variable it...
An excellent suggestion that I make a lot. NEVER have a while statement without a failsafe - a check on the iteration count so that you don't get into an infinite loop in case your main condition is never attained.

Iniciar sesión para comentar.

James Tursa
James Tursa el 17 de Sept. de 2015
Editada: James Tursa el 19 de Sept. de 2015
The f(0) looks like a typo in this line:
if sign(f(a)) * sign(f(0)) < 0
As for the rest of the code, nobody is going to type this in by hand to test it. We cannot copy code from a picture. In the future please post code as text highlighted with the { } code button ... do not post code as a picture.
EDIT: 9/18/2015
Try this (changes noted with <--)
function [xs, fs, n] = bisection(f, a, b, tol)
% This function is for finding a single isolated root of f over the interval [a,b]
% When do you get an error
fa = feval(f, a);
fb = feval(f,b);
if sign(fa) * sign(fb) == 1
xs = nan;
fs = nan;
n = nan;
fprintf('error, check your inputss...');
return
elseif a >= b;
xs = nan;
fs = nan;
n = nan;
fprintf('error, check your inputss...');
return
end
n=0; % initialize itteration
x0 = (a + b)/2;
f0 = feval(f, x0);
xf = x0;
while (abs(x0 - xf)) >= tol*(1+abs(x0))||abs(f0)>=tol;
xf = x0;
x0 = (a + b)/2;
f0 = feval(f, x0); % <-- added this line
n = n + 1;
if (fa) * (f0) > 0 % <-- changed < to >
a = x0;
fa = f0; % <-- added this line
else
b = x0;
end
end
xs = x0; % <-- changed xo to x0
fs = f0;
% Printing final results
fprintf('\n Bisection Method For Functions W/ Multiple Roots \n Homework 3 Problem 1')
fprintf('\n\n xs = %f produces \n fs = %f \n n = %i iterations\n', xs, fs, n-1);
end
%bisection(@(x)(cos(x)-x),0,1,1e-6)

2 comentarios

me
me el 17 de Sept. de 2015
i posted my updated code... its still saying busy
Use the debugger.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

me
el 16 de Sept. de 2015

Comentada:

el 19 de Sept. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by