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
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.
Aijalon Marsh
2023 年 10 月 4 日
Torsten
2023 年 10 月 4 日
Can you imagine how big sinh(m * pi * x / L) will become for m = 1000 ? I'd say: Inf.
Aijalon Marsh
2023 年 10 月 4 日
Torsten
2023 年 10 月 5 日
I doubt that the infinite series you use to approximate T is correct ; the "sinh" term must be wrong.
採用された回答
その他の回答 (1 件)
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 件のコメント
Aijalon Marsh
2023 年 10 月 4 日
Walter Roberson
2023 年 10 月 5 日
Three possibiltiies:
- 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
- plot 1:n-1; OR
- subtract 1 from n after the loop before the plot()
カテゴリ
ヘルプ センター および 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!