Clear Filters
Clear Filters

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

2 views (last 30 days)
% 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);
% Check for accuracy
max_diff = max(abs(T - T_old));
if max_diff < accuracy
accuracy_met = true;
n = n + 1;
% 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
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)');
Aijalon Marsh
Aijalon Marsh on 4 Oct 2023
Yea but by making it smaller it'll cause an error on the line with the plot
Torsten on 5 Oct 2023
I doubt that the infinite series you use to approximate T is correct ; the "sinh" term must be wrong.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 5 Oct 2023
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;
% 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);
% 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);
fprintf('All done after %d iterations.\n', loopCounter)

More Answers (1)

Walter Roberson
Walter Roberson on 4 Oct 2023
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 =
Aijalon Marsh
Aijalon Marsh on 4 Oct 2023
So if 1:n is too long should use a smaller number?
Walter Roberson
Walter Roberson on 5 Oct 2023
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()

Sign in to comment.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!