How do I display individual values from an if statement in a messgae?

8 views (last 30 days)
Ok bear with me.
I am trying to write a script that gives the root to a function using the bisection method. However it is not the traditional bisection using step halving, it is using a weighted version so it converges quicker. I have it working, however I need to get it to display specific values for my output. One of these is the final interval that the root falls into. Here is the part of my script that calculates each individual upper or lower interval value:
if fa*fc<0
b=c;
else
a=c;
end
What I want to do it have a message at the end of my command window that states:
"The final interval is 'a' and 'b'" but with a and b being replaced by the final interval, i.e. the final values of a and b that the root falls between. I can get it to print every value of a and b individually for every iteration, but not the single values for the final interval which is what I want.
I am reluctant to send my whole script as I suspect it isn't written 'perfectly' but I want to make it more efficient on my own, I just cannot work out how to do this part.
My script starts with :
function [a,b,it,itc] = WB(f,a,b,w,tol)
and it is the [a,b] from the left hand side of the equals that are the final interval values.
Thank you in advance!

Accepted Answer

Mischa Kim
Mischa Kim on 11 Nov 2017
Edited: Rena Berman on 21 Mar 2018
Hi Matthew, I assume you are using a loop to do the iterations, most likely a while loop. If you say you can print only all the interval information this suggests you do the printing inside the loop. How about simply moving the print command right after the end command of the loop? Something like
fprintf('Final interval: [%f, %f]\n',a,b)
Without access to the entire code it is challenging to impossible to point you in the right direction.
  9 Comments
Mischa Kim
Mischa Kim on 11 Nov 2017
Thanks for sharing. In your code you are updating it and itc simultaneously
it = it+1;
itc = itc+1;
so the values of both variables always will be the same. In other words, you need to move the second command (updating itc) to the proper place in your code. I do not know what itc is supposed to count, so I cannot help you there.
Also, this thread has become somewhat long at this point. If your first couple of questions have been addressed successfully, please accept the answer and post a new question. This also helps other users find answers for their questions. Thanks.
Matthew Worker
Matthew Worker on 11 Nov 2017
Image Analyst due to the last comment please go to this link to continue with this question:
https://uk.mathworks.com/matlabcentral/answers/366444-how-do-i-count-how-many-iterations-my-root-is-in-a-smaller-interval

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!