MATLAB Answers

0

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

Asked by Matthew Worker on 11 Nov 2017
Latest activity Edited by Rena Berman on 21 Mar 2018
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!

  0 Comments

Sign in to comment.

1 Answer

Answer by Mischa Kim
on 11 Nov 2017
Edited by Rena Berman on 21 Mar 2018
 Accepted Answer

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

Sine it and itc both start at zero, and both get incremented once for each while loop iteration, why do you think they should be different?
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.
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.