MATLAB Answers

How can I loop my code until the loop is satisfied and gives the correct number of iterations.

2 ビュー (過去 30 日間)
Minenhle Ndlangisa
Minenhle Ndlangisa 2020 年 4 月 10 日
コメント済み: James Browne 2020 年 4 月 11 日
Good Day.
I am writing a code and it is supposed to calculate the surface area for a very large n, and n is achieved by increasing it by 1 until the difference in the total area calculated from one iteration to the next is less than 10^-4. My code gives me an infinite recursion error.
n = 1;
Ymin = 0;
Ymax = 40;
deltaY = (Ymax - Ymin)/n;
SurfaceArea = [];
count = 1;
Temp2 = 0;
Temp1 = 0;
area1 = [];
I initialised my variables to use for my code
for count = Ymin:deltaY:Ymax
A = 2*pi*(1+sqrt(0.25+((44.56-count)/0.16)))*((1 + (1/(28.5184 - 0.64*count))^2)^0.5)*count;
area1 = [area1 A];
end
Temp2 = sum(a)
diff = Temp2 - Temp1
area2 = [];
while diff > 0.0001
area2 = [];
n = n + 1;
deltaY = (Ymax - Ymin)/n;
for count = Ymin:deltaY:Ymax
A = 2*pi*(1+sqrt(0.25+((44.56-count)/0.16)))*((1 + (1/(28.5184 - 0.64*count))^2)^0.5)*count;
area2 = [area2 A];
end
end
Temp2 =sum(area2)
diff = Temp2 - Temp1;
Logically thinking about this, I feel like it should work however I am struggling to undersstand why I am getting an error
% Your code could not run to completion. Check for errors in the code such as infinite recursion.

  4 件のコメント

表示 1 件の古いコメント
Minenhle Ndlangisa
Minenhle Ndlangisa 2020 年 4 月 10 日
No. I just updated it and I am still getting the same error.
Walter Roberson
Walter Roberson 2020 年 4 月 10 日
Why are you subtracting Ymax from Ymax? The result is always going to be 0. Wouldn't it make more sense to use Ymax minus Ymin ?

サインインしてコメントする。

採用された回答

James Browne
James Browne 2020 年 4 月 10 日
You are getting the error because the script never breaks out of the while loop. This is because the comparitor in the while satement "while diff > 0.0001" is dependant on the variable "diff" which is never altered inside of the while loop, so here is what happens...
After the script runs the code that is inside the while loop, it checks the comparitor statement "diff > 0.0001", if it evaluates to true, the while loop executes the code inside of it again and then checks the comparitor again and so on until the comparitor evaluates to false, at which time the script exits the while loop and continues on to the next line. Since "diff" never gets alterd inside the while loop, every time the while loop checks the comparitor, it will decide to continue, hence the infinite recursion error.
In general, you need to be careful when setting up a while loop so that the variable(s) in the comparitor are being changed from within the while loop or that there is some other exiting mechanism inside the while loop or you will get this problem.
It is an easy mistake to make though, I still do it sometimes myself.
Hope that helps =)

  3 件のコメント

Minenhle Ndlangisa
Minenhle Ndlangisa 2020 年 4 月 10 日
Please could you elaborate further on what you mean by altering the diff inside the while loop. Thank you
Walter Roberson
Walter Roberson 2020 年 4 月 10 日
n is achieved by increasing it by 1 until the difference in the total area calculated from one iteration to the next is less than 10^-4
So each iteration you should be recording the current area, and comparing it to the area of the previous iteration, and computing the absolute difference (your diff). And after you have checked the difference, make the current area into the old area:
diff = abs(A - Temp1);
Temp1 = A; %next time through, Temp1 will hold the area from this cycle and will act as "old" area.
James Browne
James Browne 2020 年 4 月 11 日
It may also be worth mentioning that, in general, once the script gets to any loop structure, it just executes the code inside the loop over and over but nothing beyond the loop, until the exit condition is satisfied and then it exits the loop moves on to the next line. In the case of a while loop, you have to define the exit condition...
In your case, the exit condition is when "diff" is greater than 0.001. But since the varibale "diff" is not altered inside of the while loop, the exit condition cannot ever be achieved because MATLAB is only looking at the code inside the while loop, until the exti condition is satisfied. Since the variable "diff" is not changed inside of the while loop, if the script were to run, MATLAB would get stuck inside of the while loop forever.
When you give MATLAB the command to run a script, one of the things that it does is looks at while loops and checks to see if some conditions exist which might make the program get stuck inside the while loop (such as none of the exit condition varibales being altered inside of the loop).

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by