Why won't my code run even though it has no errors

% Define constants
L = 50; % Length of the plate in cm
W = 30; % Width of the plate in cm
T_top = 85; % Temperature at the top side in °C
T_sides = 25; % Temperature at the left, bottom, and right sides in °C
accuracy = 1e-2; % Desired accuracy
% Define the points where temperature needs to be calculated
points = [L/4, W/4; 3*L/4, W/4; L/2, W/2; L/4, 3*W/4; 3*L/4, 3*W/4];
% Initialize variables
T = zeros(size(points, 1), 1);
n = 1;
accuracy_met = false;
% Calculate temperature at each point using the infinite series
while ~accuracy_met
T_old = T;
for i = 1:size(points, 1)
x = points(i, 1);
y = points(i, 2);
% Calculate the temperature at the current point (x, y)
T(i) = T_sides + 4 * T_top / pi;
for m = 1:2:1000 % Considering odd terms in the series
T(i) = T(i) + (4 * T_top / (pi * m)) * sinh(m * pi * x / L) * sin(m * pi * y / W);
end
end
% Check for accuracy
max_diff = max(abs(T - T_old));
if max_diff < accuracy
accuracy_met = true;
end
n = n + 1;
end
% Display the number of terms required for the desired accuracy
fprintf('Number of terms required for accuracy of %.2f°C: %d\n', accuracy, n);
% Plot Temperature vs. Number of Terms
figure;
plot(1:n, T, 'o-');
xlabel('Number of Terms');
ylabel('Temperature (°C)');
title('Temperature vs. Number of Terms');
grid on;
% Display the solution in tabular form
results = [points, T];
disp('Point (x, y) Temperature (°C)');
disp(results);

댓글 수: 5

Dyuman Joshi
Dyuman Joshi 2023년 10월 4일
편집: Dyuman Joshi 2023년 10월 4일
"Why won't my code run even though it has no errors"
Most likely - it runs, but it keeps running and running i.e. the while loop does not terminate.
I don't understand what the purpose of variable "n" is, in your code. Maybe, it was defined to keep a check for number of iterations?
I suggest you recheck the termination condition on the basis of what you are trying to do. As you have not described that and just pasted the code, it is difficult to suggest anything without any relevant information.
Thanks
Can you imagine how big sinh(m * pi * x / L) will become for m = 1000 ? I'd say: Inf.
Yea but by making it smaller it'll cause an error on the line with the plot
I doubt that the infinite series you use to approximate T is correct ; the "sinh" term must be wrong.

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

 채택된 답변

Image Analyst
Image Analyst 2023년 10월 5일
You made the common beginner mistake of not having a failsafe in your while loop so you get an infinite loop because your exit condition ~accuracy_met never happens. You should always use a failsafe to prevent situations like that (infinite loops). Here is an example of how to use a failsafe with a while loop:
% Demonstration of how to avoid an infinite loop by setting up a failsafe.
% Set up a failsafe
maxIterations = 100; % Way more than you think it would ever need.
loopCounter = 0;
% Now loop until we obtain the required condition: a random number equals exactly 0.5.
% If that never happens, the failsafe will kick us out of the loop so we do not get an infinite loop.
r = nan; % Initialize so we can enter the loop the first time.
while (r ~= 0.5) && loopCounter < maxIterations
loopCounter = loopCounter + 1;
fprintf('Iteration #%d.\n', loopCounter)
r = rand;
end
% Alert user if we exited normally, or if the failsafe kicked us out to avoid an infinite loop.
if loopCounter < maxIterations
% Then the loop found the condition and exited early, which means normally.
fprintf('Loop exited normally after %d iterations.\n', loopCounter);
else
% Then the loop never found the condition and exited when the number of iterations
% hit the maximum number of iterations allowed, which means abnormally.
fprintf('Loop exited abnormally after iterating the maximimum number of iterations (%d) without obtaining the exit criteria.\n', maxIterations);
end
fprintf('All done after %d iterations.\n', loopCounter)

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 10월 4일
plot(1:n, T, 'o-');
Inside your loop you use n to index the location you are writing to inside the T matrix, and then you increment n=n+1. So after the loop, n will be 1 more than the index of the last entry in T, so 1:n will be one entry too long to plot against T.
This assumes that you solved your numeric problems that are producing infinities. Which you can do by switching to symbolic calculations.
K>> vpa(T(:))
ans =
-2.511543944632718558814850406746e+339
-7.0154934357487854586830872470039e+1020
-1.664611335641028352527305925413e+680
-2.511543944632718558814850406746e+339
-7.0154934357487854586830872470039e+1020

댓글 수: 2

So if 1:n is too long should use a smaller number?
Three possibiltiies:
  1. Start n at 0 and increment it just before using it to store anything. That way, after the loop, n will match the size of the data; OR
  2. plot 1:n-1; OR
  3. subtract 1 from n after the loop before the plot()

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2023년 10월 4일

댓글:

2023년 10월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by