my while statement wont work

1 Ansicht (letzte 30 Tage)
Brody Wagner
Brody Wagner am 4 Nov. 2021
Bearbeitet: Chris am 5 Nov. 2021
I am attempting to create a temperature field with one set boundary condition and a wire in one corner of my matrix. To populate the matrix i have been using while loops but only the first one is working. In need of some advice.
clc;clearvars;close all;
H=0.5; %Height in mm
W=2; %Width in mm
Nh=50; %number of elements in y axis
Nw=200; %number of elements in x axis
dx=W/Nw; %increment size in x
dy=H/Nh; %increment size in y
%discretizing
x=0:dx:W;
y=0:dy:H;
%
Err=1; %initial error
Tol=1e-3; %Error tolerance
T=zeros([50,200]);
%Boundary Conditions
Tt=20; %This is correct
T(1,1:Nw)=Tt;
C=35;
T(26:end,1)=C;
T(27:end,2)=C;
T(28:end,3)=C;
T(29:end,4)=C;
T(30:end,5)=C;
T(31:end,6)=C;
T(32:end,7)=C;
T(33:end,8)=C;
T(34:end,9)=C;
T(35:end,10)=C;
T(36:end,11)=C;
T(37:end,12)=C;
T(38:end,13)=C;
T(39:end,14)=C;
T(40:end,15)=C;
T(41:end,16)=C;
T(42:end,17)=C;
T(43:end,18)=C;
T(44:end,19)=C;
T(45:end,20)=C;
T(46:end,21)=C;
T(47:end,22)=C;
T(48:end,23)=C;
T(49:end,24)=C;
T(50:end,25)=C;
while Err>Tol
Told=T;
for ii= 2:Nh-25
for jj= 1
T(ii,jj)=(T(ii-1,jj)+(2*T(ii+1,jj))+T(ii,jj+1))/4;
end
end
Err=max(max(abs(T-Told)./T))*100;
%mesh(T),colorbar;colormap;drawnow;
end
while Err>Tol
Told=T;
for q= 50
for r= 26:Nw-1
T(q,r)=((2*T(q,r-1))+T(q-1,r)+T(q,r+1))/4;
end
end
Err=max(max(abs(T-Told)./T))*100;
%mesh(T),colorbar;colormap;drawnow;
end
% below is not working for some reason, should be the same values as the first
% while loop
while Err>Tol
Told=T;
for l= 2:Nh-1
for p= Nw
T(l,p)=((2*T(l,p-1))+T(l-1,p)+T(l+1,p))/4;
end
end
Err=max(max(abs(T-Told)./T))*100;
%mesh(T),colorbar;colormap;drawnow;
end
while Err>Tol
Told=T;
for k= 2:Nh-1
for u= 2:Nw-1
T(k,u)=(T(k+1,u)+T(k,u+1)+T(k-1,u)+T(k,u-1))/4;
end
end
Err=max(max(abs(T-Told)./T))*100;
%mesh(T),colorbar;colormap;drawnow;
end

Antworten (1)

Chris
Chris am 4 Nov. 2021
Bearbeitet: Chris am 5 Nov. 2021
It looks like you need to reset Err to 1 after a loop completes.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by