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

조회 수: 2 (최근 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 CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by