MATLAB Answers

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

13 ビュー (過去 30 日間)
Matthew Worker
Matthew Worker 2017 年 11 月 11 日
Edited: Rena Berman 2018 年 3 月 21 日
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 件のコメント

サインイン to comment.

採用された回答

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 件のコメント

表示 6 件の古いコメント
Image Analyst
Image Analyst 2017 年 11 月 11 日
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?
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

サインイン to comment.

More Answers (0)

サインイン してこの質問に回答します。


Translated by