Plotting a variable that changes in a loop iteration vs the iteration number

11 vues (au cours des 30 derniers jours)
I am trying to plot the change in u (diff_u) agaist the iteration to prove that diff_u converges. What is the best way to do this?
Below is my code, with z being the iteration counter:
%% Parameters
rho = 1.225; % kg/m^3 - Air sea level standard day
U = 10; % m/s - freestream
mu = 1.789e-5; % kg/(m*s) - Air sea level standard day
nu = mu/rho; % m^2/s
L = 0.25; % m
H = 0.05; % m
% nx and ny defines matrix grid, dx and dy for step size
nx = 50;
ny = 10;
dx = .005; % 50 grid points in x-dir - .005m spacing
dy = .005; % 10 grid points in y-dir - .005m spacing
% Re based on length
Re_L = U*L/nu;
% Filling mesh with 0 to begin
v = zeros(ny,nx); % All initial v are 0
u = ones(ny,nx)*U; % Filling the entire mesh with all u = 10
% Boundary Conditions
% Goes (j,i) each segment, fills mesh with known values
u(:,1) = U; % Inlet
v(:,1) = 0; % Inlet
u(1,:) = 0; % No Slip wall
v(1,:) = 0; % No Slip wall
u(ny,:) = U; % Free stream (top of mesh)
%% Solving
% Guess values of alpha
alphau = 0.001; %guess, will need to figure out best value for this
alphav = 0.1;
% Differences
diff_u=[];
diff_v=[];
diff_u_max=[];
diff_v_max=[];
diff_both=[];
% Tolerance and convergence for main while loop
tol = .001;
converge = false;
z = 0; % Starting the index for iterations
while z < 20 % This is number of iterations it performs
z = z + 1;
for j = 2:1:ny-1
for i = 2:1:nx-1
u_i = u(j,i+1);
u(j,i+1) = (2*dx/u(j,i)) * (((mu/rho)*((u(j-1,i) + 2*u(j,i) + u(j+1,i))/(dy^2))) - (v(j,i)*((u(j-1,i) - u(j+1,i))/(2*dy)))) + u(j,i-1);
change_u = u_i - u(j,i+1);
u(j,i+1) = u_i + (alphau * change_u);
v_j = v(j+1,i);
v(j,i+1) = v(j-1,i+1) - dy/2/dx*(u(j,i+1)-u(j,i)+u(j-1,i+1)-u(j-1,i));
change_v = v_j - v(j,i+1);
v(j,i+1)= v_j + (alphav * change_v);
diff_u = [diff_u change_u];
diff_v = [diff_v change_v];
end
end
diff_U = max(abs(diff_u))
diff_V = max(abs(diff_v))
diff_u_max = [diff_u_max diff_U];
diff_v_max = [diff_v_max diff_V];
diff = [diff_U diff_V];
max_diff = max(diff);
%if max_diff < tol
% z = 0; % I dont think this is right. Want max_diff < tol then iterations = 0 so it stops iterating
%end
% WANT TO PLOT THE CHANGE IN u TO SHOW IT CONVERGES
end

Réponse acceptée

Geoff Hayes
Geoff Hayes le 4 Mai 2020
Colin - outside of the while loop, just do
plot(diff_u);
But is that what you really want to plot? Your comment
%if max_diff < tol
% z = 0; % I dont think this is right. Want max_diff < tol then iterations = 0 so it stops iterating
%end
where
diff = [diff_U diff_V];
max_diff = max(diff);
also includes a v. Do you want to plot diff_v instead? Or max_diff? Also, if you want to exit the while loop, just do
if abs(max_diff) < tol
break;
end
  1 commentaire
Colin Thiele
Colin Thiele le 4 Mai 2020
Thanks Geoff!
I actually will need 2 difference lines plotted. One that shows the differnce in v and one for difference in u to show they are both converging throughout the iteration. I figured I would plot diff_u and diff_v on the same plot, since max_diff takes into account both differnces (of u and v) and combines them. When the iteration finsihed max_diff is just one number.
Also thank you for the help with the if statment at the end of the while loop!
while
% Eqns and such
if abs(max_diff) < tol
break
end
z = z + 1;
end
figure
hold on
plot(diff_u)
plot(diff_v)
hold off
xlabel('Iterations');
ylabel('Chnage in u and v');
title('Convergence of u and v Throughout Iterations');
legend('u difference', 'v difference');

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Physics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by