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

3 ビュー (過去 30 日間)
James T
James T 2017 年 9 月 15 日
編集済み: James Tursa 2017 年 9 月 18 日
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 件のコメント
Andrei Bobrov
Andrei Bobrov 2017 年 9 月 15 日
編集済み: Andrei Bobrov 2017 年 9 月 15 日
Please read here.
James T
James T 2017 年 9 月 16 日
編集済み: James T 2017 年 9 月 16 日
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;

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

回答 (1 件)

James Tursa
James Tursa 2017 年 9 月 15 日
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 件のコメント
James T
James T 2017 年 9 月 16 日
編集済み: James T 2017 年 9 月 16 日
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 2017 年 9 月 18 日
編集済み: James Tursa 2017 年 9 月 18 日
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.

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by