Help with iteration counter not working or displaying wrong numbers?

Hi, I was creating some code to solve an equation using bisection and regula falsi. I wanted to see which method converges faster so I decided to make an iteration counter to count how many iterations were done for each result to meet my tolerance value (0.001). I'm not sure why the iterations number which are displayed along side my other value are the same for both methods. They also have decimal places. Is there somewhere in bisect.m or regfalsi.m where I am making a mistake or in main.m where I pull the iteration values in part B and C.
Specifically this area
function bisectioncalc = bisect(f,a,b) %Head is the num.soln
c = (a+b)/2; % calculation of midpoint
r = abs(f(c));
I=0;
while r > 0.001
if (f(a)*f(c))<0
b = c; % c values becomes a
elseif (f(b)*f(c))<0
a = c; % c values becomes b
end
c = (a+b)/2;
r = abs(f(c)); %relative error/tolerance formula
I = I + 1;
end
bisectioncalc = c; %assign c back to bisectionfalsicalc
end
I want to know whether my placment of the counter is correct and if I am calling it correctly in main.m
Thank you!

3 Comments

The counter looks fine there.
Where is your else case if neither condition is satisfied?
(If you can be certain that one of the two will be satisfied then you do not need the second test.)
In this case, at least one of two will be satisfied, thanks for letting me know! I'll remove the second test.
Is there any reason why my iteration numbers are in decimal places in the output? I feel like I'm incorrectly calling it out of the bisect/falsi functions when I call it in main.m for the fprintf display
f = @(H) v -((sqrt(19.62.*H)*tanh(sqrt(19.62.*H)*t/(8))));
%Head = fzero(f,b); % find root using fzero()
H = bisect(f,a,b);
I = bisect(f,a,b);
fprintf('\n%6.1f\t%6.1f\t%8.4f\t%6.1f',v,t,H,I)

Sign in to comment.

 Accepted Answer

f = @(H) v -((sqrt(19.62.*H)*tanh(sqrt(19.62.*H)*t/(8))));
%Head = fzero(f,b); % find root using fzero()
H = bisect(f,a,b);
I = bisect(f,a,b);
Why are you calling the same bisect() twice? Is it non-deterministic? You are not timing it, so it is not for the purposes of getting more accurate timing.

5 Comments

Oh I thought that for calling up the H (head value), I (iteration number) respectively I needed two separate bisects. Is there a better way of doing that? There are 37 possible H values which are being generated, each of them has been iterated through until the tolerance is reached. Once that's done, then I display the values using:
fprintf('\n%6.1f\t%6.1f\t%8.4f\t%6.1f',v,t,H,I)
So I'm trying to generate the interation values and then have it display using the above fprintf statement.
The problem is both my tables for the bisection method and the falsi method seem to showing the same number of iterations for each head value when the three programs are run.
Your bisect() and regfalsi() have no way of returning the interation numbers at present.
Change to
function [bisectioncalc, I] = bisect(f,a,b) %Head is the num.soln
and
[H,I] = bisect(f,a,b);
Perfect! It worked, thank you. Was the reason, I could not return the numbers initially because it was not setup as an array?
No, it was because you never tried to return I from the function.
The syntax
function [bisectioncalc, I] = bisect(f,a,b) %Head is the num.soln
does not have anything to do with arrays. The [] on the left is just syntax that does not really have to do with using [] for constructing arrays. MATLAB could have designed it as, for example,
function #bisectioncalc, #I = bisect(f,a,b) %Head is the num.soln
to invent a syntax.
Sounds good, thanks for all your help.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!