3 views (last 30 days)

Show older comments

Hello,

I was wondering if someone would be able to help me. I wrote this code for the bisection method:

Z=[0.126938541068352,0.182653647329121,0.209649626651350,0.284893739230327,0.132107983917289,0.0637564618035612];

K=[4.23297333098727;1.43244396950094;1.92283427118344;0.505474641398215;0.638916861313215;0.183249967140241];

a = 0;

b = 1;

accuracy = 0.00000001;

if RRE(a,Z,K) == 0; %Checks if lower bound is the root. a is already the root.

Ps = a;

elseif RRE(b,Z,K) == 0; %Checks if the upper bound is the root b is already the root.

Ps = b;

elseif RRE(a,Z,K)*RRE(b,Z,K) > 0;

error('RRE(a) and RRE(b) do not have opposite signs. Try a different bound or check if your Z and K values are correct.'); %custom error code

end

while (b-a > accuracy || (abs(RRE(a,Z,K)) > accuracy && abs(RRE(b,Z,K)) > accuracy)); %repeat the bisection method until both f(a) and f(b) are less than 'accuracy' or until the difference between a and b are less than 'accuracy'

c = (a+b)/2; %Midpoint Method

%c = a - (((b-a)*RRE(a,Z,K))/(RRE(b,Z,K)-RRE(a,Z,K))); %False-Position Method

if RRE(c,Z,K) == 0;

Ps = c;

break;

elseif RRE(a,Z,K)*RRE(c,Z,K) < 0; %If f(a)*f(c) is a negative # then make the next range from [a c]. To do this you make c the new b

b = c;

else

a = c; %If f(a)*f(c) is a posituve # then make the next range from [c b]. To do this you make c the new a value

end

end

if RRE(c,Z,K) == 0;

Ps = c;

elseif RRE(a,Z,K)<RRE(b,Z,K); %Choose the x-value that brings f(psi) closest to 0.

Ps = a;

else

Ps = b;

end

However, when I run this code my function RRE is calculating a value AND printing it out on the command window. I've checked all my semi-colons and all the lines are supressed. I'm stuck. I have no idea why my function RRE is printing to the command window.

Please help.

-Justin

John D'Errico
on 23 Feb 2015

You did not give us the function RRE. The problem is most likely in there.

By the way, I want to really compliment you on the use of internal comments. They make your code far more debuggable. So a good job there. I also like that you have error checking in the code.

You might consider using more expressive variable names though. They also make code more readable, more debuggable.

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

Start Hunting!