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

8 次查看(过去 30 天)
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!

采纳的回答

Mischa Kim
Mischa Kim 2017-11-11
编辑:Rena Berman 2018-3-21
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 个评论
Mischa Kim
Mischa Kim 2017-11-11
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 2017-11-11
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by