Plotting a variable that changes in a loop iteration vs the iteration number
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Physics 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!