Professional Documents
Culture Documents
سند (11) hgg
سند (11) hgg
c = 2;
lx = 2;
ly = 2;
dx = 0.1;
dy = 0.1;
dt = 0.01;
tmax = 10;
% Define grid
x = 0:dx:lx;
y = 0:dy:ly;
t = 0:dt:tmax;
% Correct step
u_corr_x_halfstep(:, :) = (u(:, :) + u_pred_x_halfstep(:, :)) / 2 - c * dt / dx / 2 *
(circshift(u_pred_y_halfstep(:, :), [0, 1]) - circshift(u_pred_y_halfstep(:, :), [0, -1]));
u_corr_y_halfstep(:, :) =(u_corr_x_halfstep(:,:) + u_pred_y_halfstep(:,:)) / 2 - c * dt / dy / 2 *
(circshift(u_pred_x_halfstep(:, :), [1, 0]) - circshift(u_pred_x_halfstep(:, :), [-1, 0]));
% Update u and ut
u(:, :) = u_corr_y_halfstep(:, :);
ut(:, :) = (u_corr_y_halfstep(:, :) - u(:, :)) / dt;
% Apply boundary conditions
u(1, :) = 0;
u(end, :) = 0;
u(:, end) = 0;
u(:, 1) = f(y) .* sin(5 * t(n+1));
end
% Plot results
[X,Y] = meshgrid(x,y);
surf(X,Y,u');
xlabel('x');
ylabel('y');
zlabel('u(x,y,t)');
title('2D Wave Equation Solution using McCormack Scheme');
fy = y.(y-ly);
u_exact = 0.5sin(50pi/4)sin(pi/2)sin(pi/2)fy(2);
unumerical = u(round(1/dx),round(1/dy),end);