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
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.
Walter Roberson <robe...@hushmail.com> wrote in message <m6QGk.7996$t76....@newsfe06.iad>...
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
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.
> 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