How do you loop code for all gridsizes in order to obtain iterations for each of them
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Jayden Cavanagh
am 5 Jun. 2021
Beantwortet: Walter Roberson
am 5 Jun. 2021
I am trying to run everything within the first for loop for each ndiv so that at the end I can obtain 8 different iterations that line up with the different values within the ndiv array. How do I set up my code so that it loops through all of the ndiv (Gridsizes) values and gives me 8 different values of k that can be set up into an array format
Code:
a=25;
b=a;
ndiv=[4,8,16,32,128,256,512];
for nx=ndiv(1)
nz=nx;
x = linspace(0, a, nx);
z = linspace(0, b, nz);
[X, Z] = meshgrid(x,z);
Tnp1 = zeros(nx, nz);
Tnp1(:,1) = 20; % Left boundary
Tnp1(:,end) = 20; % Right boundary
Tnp1(1,:) = 20+380*sin((x*pi)/25)+205*sin((x*5*pi)/25); % Bottom boundary
Tnp1(end,:) = 20; % Top boundary
err = 1;
tol = 1e-8;
k=0;
while err > tol
Tn = Tnp1;
k=k+1;
for i = 2:nx-1
for j = 2:nz-1
Tnp1(i,j) = (1/4)*(Tn(i+1,j)+Tn(i-1,j)+Tn(i,j+1)+Tn(i,j-1));
end
end
err = max(abs(Tnp1(:) - Tn(:)));
end
T = Tnp1;
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 5 Jun. 2021
ndiv=[4,8,16,32,128,256,512];
for nx=ndiv(1)
replace that with something like
ndiv=[4,8,16,32,128,256,512];
for nidx = 1:length(ndiv)
nx = ndiv(nidx)
stuff goes here
all_k(nidx) = k;
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
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!