How can I loop my code until the loop is satisfied and gives the correct number of iterations.
이전 댓글 표시
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
matquest
2020년 4월 10일
Is it possible you have a typo in your deltay update? You have deltay = (Ymax - Ymax)/n; which is always 0.
Minenhle Ndlangisa
2020년 4월 10일
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 ?
Minenhle Ndlangisa
2020년 4월 10일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!