Trouble getting stuck in while loop for finite difference method problem
Show older comments
I am trying to print the values at nodes (2,2) (2,6) (6,2) and (6,6) but I believe the code is never coming out of the while loop. I am using the finite difference method. When I pause the code while it's running and open my "mesh," it is showing the correct values. Does anyone know how I can get it to print these values? Thank you.
%establishes 7x7 matrix
m = 7;
n = 7;
mesh(2:6,2:6) = input('Guess the overall internal temperature: ');
%establish boundry conditions on the matrix (the mesh)
mesh(1,:) = 0; %temp at top of matrix = 0
mesh(:,1) = 200; %temp at bottom = 200C
mesh(m,:) = 300; %left is 300
mesh(:,n) = 10; %right is 10
newMesh(m,n)=mesh(m,n);
while newMesh(2,2) - mesh(2,2) <= 1 && newMesh(6,6) - mesh(6,6) <= 1 %saying the difference between matricies must be less that .1C
for m = 2:6
for n = 2:6
newMesh(m,n) = (mesh(m+1,n)+ mesh(m-1,n) + mesh(m,n+1) + mesh(m,n-1)) / 4; %getting the temperatures of nodes above, below, and on either side of point
mesh(m,n) = newMesh(m,n); %display "mesh(m,n)" with updated values.
end
end
end
fprintf('The final temperature of the nodes are %4.2f, %4.2f, %4.2f, %4.2f', mesh(2,2), mesh(2,6), mesh(6,2), mesh(6,6));
Answers (1)
darova
on 29 Jul 2019
Define newMesh:
% newMesh(m,n)=mesh(m,n); % only (7,7) element will be nonzero
newMesh = mesh; % use this instead
Here you are saying: if difference between (2,2) and (6,6) elemnts is LESS than 1 do loop
while newMesh(2,2) - mesh(2,2) <= 1 && newMesh(6,6) - mesh(6,6) <= 1 %saying the difference between matricies must be less that .1C
After for loop mesh will always be equal to newMesh (mesh==newMesh). So while loop never repeats or repeats infinitely
mesh(m,n) = newMesh(m,n); %display "mesh(m,n)" with updated values.
Categories
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!