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

7 次查看(过去 30 天)
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 个评论
me
me 2015-9-17
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)

请先登录,再进行评论。

回答(2 个)

Titus Edelhofer
Titus Edelhofer 2015-9-17
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 个评论
Titus Edelhofer
Titus Edelhofer 2015-9-17
Sorry, I did not see that you already count iterations. Use your variable n instead of "my" variable it...
Image Analyst
Image Analyst 2015-9-19
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.

请先登录,再进行评论。


James Tursa
James Tursa 2015-9-17
编辑:James Tursa 2015-9-19
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)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by