Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bisection method

15 views
Skip to first unread message

Artie Ohlrich

unread,
Oct 7, 2008, 4:22:01 PM10/7/08
to
I was asked to use the bisection method in matlab to find the real root of 1.2=x*log(x) correct to six decimal places. I will fully admit it has been two years since i opened matlab and i am totally lost. i tried to use the following script and got tons of errors. any help would be appreciated.

function root = bisect(f,a,b,tol)
f=x*log(x)
a=2;
b=5;
tol=10^-6;
fa = feval(f,a);
fb = feval(f,b);
if (fa*fb > 0)
fprintf('Not valid imput\n');
return
end
if (tol<=0)
fprintf('tol must be +\n')
return
end
%LOOP
while 1
fprintf('%18.14f %18.14f %18.14f %18.14f\n',a,b,fa,fb)
if (abs(b-a) <=tol )
root = (a+b)/2;
return
end
c= (a+b)/2; fc= feval(f,c);
if (c == a | c == b)
fprintf('Percision Correct\n');
root = c;
return
end
if (fa*fc>0)
a=c;
fa=fc;
else
b = c;
fb = fc;
end
end

Artie Ohlrich

unread,
Oct 7, 2008, 4:50:04 PM10/7/08
to
Well if u wont help go **** urselves. i figured i could come here for help. apparently not

Artie Ohlrich

unread,
Oct 7, 2008, 4:58:02 PM10/7/08
to
Alright, sorry, im just really ticked, this is really hard for me and i dont remember how to do anything. Apologies everyone. If u can help thanks, ur saving the hairs on my head and my fingernails a lot of pain and abuse.

Walter Roberson

unread,
Oct 7, 2008, 5:01:42 PM10/7/08
to
Artie Ohlrich wrote:
> Well if u wont help go **** urselves. i figured i could come here for help. apparently not

I don't know what brought that remark on?

You posted to Mathwork's interface to a Usenet newsgroup. Newsgroups propagation is not
immediate: it takes time for messages to reach around the world.

Examining the timestamps on your message, I see that although your message was posted while
I was actively checking for new postings, your message did not actually arrive at my
news server until I went downstairs for a brief meeting. Your follow-up was posted
about 28 minutes after your original message, and happened to arrive just before I came
back up again.

We're volunteers here, and sometimes we have to work on our jobs.

Artie Ohlrich

unread,
Oct 7, 2008, 5:08:01 PM10/7/08
to
your correct. i am a highly agitated college student who is being forced to use a program i was never taught how to use to find a number i can find on my ti-89 in .5 seconds. I am sorry i got so upset. I ask for forgiveness. if i cant figure it out well its my own fault. sorry again all

Walter Roberson <robe...@hushmail.com> wrote in message <m6QGk.7996$t76....@newsfe06.iad>...

Jos

unread,
Oct 7, 2008, 5:28:01 PM10/7/08
to
"Artie Ohlrich" <ohlr...@osu.edu> wrote in message <gcggd9$j0d$1...@fred.mathworks.com>...

> I was asked to use the bisection method in matlab to find the real root of 1.2=x*log(x) correct to six decimal places. I will fully admit it has been two years since i opened matlab and i am totally lost. i tried to use the following script and got tons of errors. any help would be appreciated.
>

Artie, I choose to ignore your follow-up posts, but you need to bear in mind that, in principle, nobody reading this newsgroup is really interested in your problem! If you seek assistance by asking well-defined questions, you have more chance that somebody is kind (sic!) enough to help you out. The last time I checked you did not pay my salary.

Tha't being said, I suggest, first of all you (re-)read the Getting Started section of the manual, and try the (numerical) examples.

> function root = bisect(f,a,b,tol)
> f=x*log(x)
> a=2;
> b=5;

Why have a function that accepts three user-defined parameters, but that immediately overwrites these parameters? Also note that x is not defined. Probably you want to use an anonymous function like this:

f = @(x) x .* log(x)

for which a value can be evaluated, as in f(1.34)

> tol=10^-6;
> fa = feval(f,a);
> fb = feval(f,b);

Use: fa = f(a)

> if (fa*fb > 0)
> fprintf('Not valid imput\n');
> return
> end
> if (tol<=0)
> fprintf('tol must be +\n')
> return
> end

help error

> %LOOP
> while 1
> fprintf('%18.14f %18.14f %18.14f %18.14f\n',a,b,fa,fb)
> if (abs(b-a) <=tol )
> root = (a+b)/2;
> return
> end
> c= (a+b)/2; fc= feval(f,c);

Again, fc = f(c)

> if (c == a | c == b)

comparing floating points like this is bound to give problems.

> fprintf('Percision Correct\n');
> root = c;
> return
> end
> if (fa*fc>0)
> a=c;
> fa=fc;
> else
> b = c;
> fb = fc;
> end
> end

There are more problems with your code ... but this is for starters. Get back here, if you're still hungry.

hth
Jos

Artie Ohlrich

unread,
Oct 7, 2008, 5:47:02 PM10/7/08
to
Alright, i scrapped and rewrote my code. I hope it is somewhat better.

function root = bisect(fn,a,b,tol)
clear all;
clc;
fa = feval(fn,a);
fb = feval(fn,b);
if fa*fb >= 0
error('Not Possible')
end
c = (a+b)/2;
i=0;
while abs(a-b) > 2*tol
i=i+1;
fc = feval(fn,c);
if fa*fc < 0 % The root is to the left of c


b = c;
fb = fc;

c = (a+b)/2;
elseif fc*fb < 0 % The root is to the right of c
a = c;
fa = fc;
c = (a+b)/2;
else % We landed on the root
done = 1;
end
end
Where a is lower bound, b is upper bound and tol is the tolerance.

Artie Ohlrich

unread,
Oct 7, 2008, 6:16:20 PM10/7/08
to
Got it, thank you for the help. Apologize again, will do my best to format further questions in a specific and concise manner.

Adam

unread,
Oct 9, 2008, 9:53:12 AM10/9/08
to
"Artie Ohlrich" <ohlr...@osu.edu> wrote

> function root = bisect(fn,a,b,tol)
> clear all;
> clc;

The "clear all" isn't necessary. actually it's terrible because all it does is clear your inputs from memory. I'm surprised your code works.

You use functions because they create their own set of variables. They're blind to any that had been created before. It's called scope:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-38052.html

putting the clc inside is bad programming practice. It would really only make sense if, for example, you had a function displayResults() that's only purpose was to output to the screen.

~Adam

0 new messages