Azzera filtri
Azzera filtri

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

7 visualizzazioni (ultimi 30 giorni)
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 Commento
me
me il 17 Set 2015
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)

Accedi per commentare.

Risposte (2)

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

Accedi per commentare.


James Tursa
James Tursa il 17 Set 2015
Modificato: James Tursa il 19 Set 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)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by