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

조회 수: 7 (최근 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에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by