I'm Trying to run iterations until convergence.

조회 수: 44 (최근 30일)
Cole Zajack
Cole Zajack 2020년 9월 23일
댓글: Dana 2020년 9월 23일
Hello,
So baically I have a problem where I am guessing an initial weight (Wi). and then I have to plug in (Wi) into an Equation that looks like this. W6=Wi*(0.95)*(0.97)*...
Then I want to take the answer for W6 and use it in this equation (Y=W6/Wi). Then take the answer for Y plug it into another equation F=(1-Y)*1.06.
Lastly from there, take all these answers I just found and plug into Wtogw=(25000)/(1-F-0.457).
Now. Whatever answer I get for Wtogw, I want to use that as my new Wi and redo the process unti the answer I get for Wtogw is equal to Wi.
I have posted what I have so far. The whole first part of code is just setting variables which have known values. Please tell me what I am doing wrong/missing for the "while" loop.

답변 (1개)

Dana
Dana 2020년 9월 23일
편집: Dana 2020년 9월 23일
Well, you haven't actually told us what the problem you're encountering is, but right away I can see several problems. First, your while loop condition is i<=maxIter, but you haven't defined i anywhere. As a result, MATLAB is using the built-in i, i.e., sqrt(-1). In this case, when checking whether i<=maxIter, MATLAB only considers the real part of i, which is 0, and therefore (i<=maxIter) evaluates to true.
Furthermore, since i isn't changing inside the loop, it will always be the case that i<=maxIter, and therefore your loop will never end.
If you want to do this loop a set number of times, you should use a for loop, not a while:
for i = 1:maxIter
<stuff>
end
  댓글 수: 7
Cole Zajack
Cole Zajack 2020년 9월 23일
Do you know why I am still getting an error for maxIter?
Dana
Dana 2020년 9월 23일
MATLAB interprets the statement i<=maxIter as, "Check whether i is less than or equal to maxIter; return true if it is, return false if it's not."
That's not what you want to do here. Rather, you simply want to define maxIter, just as you did originally:
maxIter = 100;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by