I am not seeing what is wrong with my code, I've checked doc and help.

1 Ansicht (letzte 30 Tage)
James T
James T am 15 Sep. 2017
Bearbeitet: James Tursa am 18 Sep. 2017
function [ root, error_bound ] = bisection(f, a0, b0, ep, max_iterate)
%function bisection(f, a0, b0, ep, max_iterate)
%This is the bisection method for solving an equation f(x)=0.
% Input:
% f: function to find root.
% a0: Left endpoint of initial interval.
% b0: Right endpoint of initial interval.
% ep: Error tolerance.
% max_iterate: Maximum iterates in loop, in case the code has an
% infinite cycle.
% Output:
% root
% error_bound
% =================Initializing=====================
root = 0;
error_bound = 0;
% ==================================================
% ============= YOUR CODE BEGINS HERE ==============
a = a0;
b = b0;
for n = 0:max_iterate
c = (a + b) / 2;
if b - c <= ep
disp('c is the root')
break;
end
if sign(f(b)) * sign(f(c)) <= 0 %%this is where I am having an issue but I am sure this is right%%
a = c;
else
b = c;
return
end
root = c;
error_bound = b - c;
% ============== YOUR CODE ENDS HERE ===============
end
  2 Kommentare
James T
James T am 16 Sep. 2017
Bearbeitet: James T am 16 Sep. 2017
so, after reading what you showed me, is this the code I am looking for?
if sign(f(a0))*sign(f(b0)) > 0
return
end
for n = 1:max_iterate
c = (a0 + b0) / 2;
if b0 - c <= ep
break;
end
if sign(f(b0)) * sign(f(a0)) <= 0
a0 =c;
else
b0 = c;
end
end
root = c;
error_bound = b0 - c;

Melden Sie sich an, um zu kommentieren.

Antworten (1)

James Tursa
James Tursa am 15 Sep. 2017
You have an inadvertent return statement:
else
b = c;
return <-- get rid of this line
The way you have it now, the first time you enter this branch the function returns without doing any more bisection iterations.
  2 Kommentare
James T
James T am 16 Sep. 2017
Bearbeitet: James T am 16 Sep. 2017
thanks man, is this a little better?
if sign(f(a0))*sign(f(b0)) > 0
return
end
for n = 1:max_iterate
c = (a0 + b0) / 2;
if b0 - c <= ep
break;
end
if sign(f(b0)) * sign(f(a0)) <= 0
a0 =c;
else
b0 = c;
end
end
root = c;
error_bound = b0 - c;
James Tursa
James Tursa am 18 Sep. 2017
Bearbeitet: James Tursa am 18 Sep. 2017
I like the fact that you have added a check to see that the inputs have bracketed the root. However, for this case I would advise generating an error instead of returning with bogus 0 values. E.g.,
if sign(f(a0))*sign(f(b0)) > 0
error('Inputs do not bracket the root');
end
You might also think about other issues to check for. E.g., what happens if a0 > b0 on input? What happens if ep < 0 on input? Etc.
But you have changed your working middle point check for an incorrect test. I.e., you have changed this line, which correctly tests where c is:
if sign(f(b)) * sign(f(c)) <= 0
for this test, which doesn't test c at all:
if sign(f(b0)) * sign(f(a0)) <= 0
Why did you make that change? Go back to the form of the original test which was working to test which side of the root c is on.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by