필터 지우기
필터 지우기

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

댓글을 달려면 로그인하십시오.

카테고리

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