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

2 views (last 30 days)

Show older comments

Aijalon Marsh
on 4 Oct 2023

Commented: Walter Roberson
on 5 Oct 2023

% 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 Comments

Torsten
on 5 Oct 2023

### Accepted Answer

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;

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)

##### 0 Comments

### More Answers (1)

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 =

-2.511543944632718558814850406746e+339

-7.0154934357487854586830872470039e+1020

-1.664611335641028352527305925413e+680

-2.511543944632718558814850406746e+339

-7.0154934357487854586830872470039e+1020

##### 2 Comments

Walter Roberson
on 5 Oct 2023

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()

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!