フィルターのクリア

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

7 ビュー (過去 30 日間)
me
me 2015 年 9 月 16 日
コメント済み: Image Analyst 2015 年 9 月 19 日
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)
  2 件のコメント
me
me 2015 年 9 月 17 日
i posted my updated code... its still saying busy
Walter Roberson
Walter Roberson 2015 年 9 月 17 日
Use the debugger.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by