CFD Grid Refinement study to measure L1 and Ling=f Norm errors and adding periodic boudaries

1 次查看(过去 30 天)
Hi,
My problem is below:
Perform a grid-refinement study with the function 𝜙(𝑥) = sin⁡(6𝜋𝑥) and domain 0 ≤ 𝑥 ≤ 1
to determine the actual order of accuracy of
𝜙𝑖+1⁄2 = − 1/8 (phi)𝑖-1 + 3/4𝜙𝑖 + 3 /8𝜙𝑖+1 with point values:
and
𝜙𝑖+1⁄2 = − 1/8 (phi)𝑖-1 + 3/4𝜙𝑖 + 3 /8𝜙𝑖+1 with Phi Cell Average (I cant find the proper figures)
and discuss your results, especially with respect to whether you attain the formal order of accuracy.
My code is below.
I am so confused and not sure if I am going down the right path.
But if I am assuming a periodic function, how can I add the boundary (i=1 and i=end) points easily in the code?
Also if anyone can enlighten me if I am doing this correctly that would be so helpful.
clear
clc
for j=1:23
Nj(j) = ((2.^j)+1)';
for i = 1:Nj(j)
xi(j,i) = ((i-1)./(Nj(j)-1));
delx(j,i) = 1/(2^j)';
end
end
% Define the parameters for the grid refinement study
f = @(xi) sin(6*pi*xi);
der = 6*pi*cos(6*pi*xi);
y = f(xi);
%x = length(xi);
count = sum(xi ~= 0, 2);
pi_half = zeros (1,length(xi));
y_half = zeros(j,i);
L = 1; % Length of the domain
N_values = Nj; % Different numbers of grid points
phi_exact = @(x) sin(6 * pi * x); % Exact solution
% Preallocate arrays to store errors
errors_points = zeros(length(Nj),1);
errors_cells = zeros(length(Nj),1);
% Loop over different grid refinements
for l = 2:j
delx(l,1) = (L./Nj(l));
x = linspace(0, L, Nj(l)); % Grid points
x_half = x(1:end-1) + delx(l,2)./2;
phi_half_exact= f(x_half);
phi_exact_values = f(x);
% Scheme (1): Point values
phi_half_points = -1/8 .* phi_exact_values(1:end-2) + ...
3/4 .* phi_exact_values((end-1)) + ...
3/8 .* phi_exact_values(end);
% Calculate error for point values
errors_points(l) = max(abs(phi_half_points - phi_half_exact(2:end)));
% Scheme (2): Cell averages
phi_cell_averages = (phi_exact_values(1:end-1) + phi_exact_values(2:end)) ./ 2;
% Compute phi_i+1/2 using the cell averages
phi_half_cells = -1/6 * phi_cell_averages(1:end-2) + ...
5/6 * phi_cell_averages(2:end-1) + ...
1/3 * phi_cell_averages(3:end);
% Calculate error for cell averages
errors_cells(l) = max(abs(phi_half_cells - phi_half_exact(2:end-1)));
end
% Calculate order of accuracy
p_points = log(errors_points(1:end-1) ./ errors_points(2:end)) ./ log(2);
p_cells = log(errors_cells(1:end-1) ./ errors_cells(2:end)) ./ log(2);
% Display results
disp('Errors for Point Values:');
disp(errors_points);
disp('Estimated Order of Accuracy for Point Values:');
disp(p_points);
disp('Errors for Cell Averages:');
disp(errors_cells);
disp('Estimated Order of Accuracy for Cell Averages:');
disp(p_cells);
% Plot the errors
figure;
loglog(N_values, errors_points, '-o', 'DisplayName', 'Point Values');
hold on;
loglog(N_values, errors_cells, '-x', 'DisplayName', 'Cell Averages');
xlabel('Number of Grid Points (N)');
ylabel('Max Error');
legend('Location', 'best');
title('Grid Refinement Study');
grid on;

回答(1 个)

Divyam
Divyam 2024-10-10
编辑:Divyam 2024-10-10
Hi @Brian,
The approach you have taken for the solution seems incorrect as the plot for the errors for half points is a straight line parallel to x-axis while the error for cell averages is downward sloping. This implies that while there is no change in error of half points, the error rate of averages is constantly decreasing with the increase in grid points which seems counter-intuitive.
The reason for this error seems to be due to the way half point arrays are defined using vectorization which does not handle the periodic boundary point indices. To resolve this issue, handle the periodic boundary points in the code by defining them using a loop and a modulo operation on indices of the half point arrays as that would wrap the indices over the periodic boundaries.
% Outer loop for grid refinements
for l = 2:j
% Operations
% Scheme (1): Point values
% Looping to fill the half points array for every point
phi_half_points = zeros(1, N-1); % N is the grid value at current index l
for i = 1:N-1
phi_half_points(i) = -1/8 * phi_values(mod(i-2, N) + 1) + ...
3/4 * phi_values(i) + ...
3/8 * phi_values(i+1);
end
% Calculate error for point values
errors_points(l) = max(abs(phi_half_points - phi_exact_half));
% Scheme (2): Cell averages
% Looping to fill half cells array for every point
phi_cell_averages = (phi_values(1:end-1) + phi_values(2:end)) / 2;
phi_half_cells = zeros(1, N-2);
for i = 1:N-2
phi_half_cells(i) = -1/6 * phi_cell_averages(mod(i-2, N-1) + 1) + ...
5/6 * phi_cell_averages(i) + ...
1/3 * phi_cell_averages(i+1);
end
% Calculate error for cell averages
errors_cells(l) = max(abs(phi_half_cells - phi_exact_half(1:end-1)));
end

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by