How do I plot the point SOR solution along the vertical central line x=0.5 and compare it with the analytical solution?
The analytical solution is U(x,y) = sin(pi*x)cosh(pi*y)/(pi*sin(pi)).
% …… code
% calculate improved estimate using the SOR method
unew = u;
for j = 2 : Ny + 1
for i = 2 : Nx – 1
% POINT SOR METHOD
unew(j, i) = (1-w)*(u(j, i)) + w * ((dy^2)*(unew(j, i-1) + u(j, i+1)) + (dx^2)*(unew(j-1, i) + u(j+1, i))) / (2*(dy^2+dx^2));
end
end
% Calculate L2 error between two successive steps
err = sqrt(sum(sum((unew – u).^2)));
% Update U and iteration count
u = unew;
k = k + 1;
% output current solution
fprintf(‘ %4i ‘, k);
fprintf(‘|%10.6f ‘, u(2:Ny+1, 2:Nx-1)’);
fprintf(‘|%10.6f n’, err);
end
% Ensure SOR solution is stored in the array u
SOR_solution = u;
fprintf(‘%sn’, hline)
cpu_time = toc;
fprintf(‘nCPU time for this method is %1.4f s.n’, cpu_time)
% Analytical solution
u_analytical = @(x, y) sin(pi*x).*cosh(pi*y)./(pi*sinh(pi));
% Plot SOR solution along the vertical central line x=0.5
x_index = find(x(1, 🙂 == 0.5);
figure; % Create a new figure
hold on; % Enable overlaying plots
plot(y(:, x_index), u(:, x_index), ‘b’, ‘DisplayName’, ‘SOR Solution’);
plot(y(:, x_index), u_analytical(0.5, y(:, x_index)), ‘r’, ‘DisplayName’, ‘Analytical Solution’);
xlabel(‘y’);
ylabel(‘U(x,y)’);
legend(‘Location’, ‘best’);
title(‘Comparison of SOR Solution and Analytical Solution at x = 0.5 (w = 1.85)’);
grid on;
hold off; % Release the overlaying plot modeThe analytical solution is U(x,y) = sin(pi*x)cosh(pi*y)/(pi*sin(pi)).
% …… code
% calculate improved estimate using the SOR method
unew = u;
for j = 2 : Ny + 1
for i = 2 : Nx – 1
% POINT SOR METHOD
unew(j, i) = (1-w)*(u(j, i)) + w * ((dy^2)*(unew(j, i-1) + u(j, i+1)) + (dx^2)*(unew(j-1, i) + u(j+1, i))) / (2*(dy^2+dx^2));
end
end
% Calculate L2 error between two successive steps
err = sqrt(sum(sum((unew – u).^2)));
% Update U and iteration count
u = unew;
k = k + 1;
% output current solution
fprintf(‘ %4i ‘, k);
fprintf(‘|%10.6f ‘, u(2:Ny+1, 2:Nx-1)’);
fprintf(‘|%10.6f n’, err);
end
% Ensure SOR solution is stored in the array u
SOR_solution = u;
fprintf(‘%sn’, hline)
cpu_time = toc;
fprintf(‘nCPU time for this method is %1.4f s.n’, cpu_time)
% Analytical solution
u_analytical = @(x, y) sin(pi*x).*cosh(pi*y)./(pi*sinh(pi));
% Plot SOR solution along the vertical central line x=0.5
x_index = find(x(1, 🙂 == 0.5);
figure; % Create a new figure
hold on; % Enable overlaying plots
plot(y(:, x_index), u(:, x_index), ‘b’, ‘DisplayName’, ‘SOR Solution’);
plot(y(:, x_index), u_analytical(0.5, y(:, x_index)), ‘r’, ‘DisplayName’, ‘Analytical Solution’);
xlabel(‘y’);
ylabel(‘U(x,y)’);
legend(‘Location’, ‘best’);
title(‘Comparison of SOR Solution and Analytical Solution at x = 0.5 (w = 1.85)’);
grid on;
hold off; % Release the overlaying plot mode The analytical solution is U(x,y) = sin(pi*x)cosh(pi*y)/(pi*sin(pi)).
% …… code
% calculate improved estimate using the SOR method
unew = u;
for j = 2 : Ny + 1
for i = 2 : Nx – 1
% POINT SOR METHOD
unew(j, i) = (1-w)*(u(j, i)) + w * ((dy^2)*(unew(j, i-1) + u(j, i+1)) + (dx^2)*(unew(j-1, i) + u(j+1, i))) / (2*(dy^2+dx^2));
end
end
% Calculate L2 error between two successive steps
err = sqrt(sum(sum((unew – u).^2)));
% Update U and iteration count
u = unew;
k = k + 1;
% output current solution
fprintf(‘ %4i ‘, k);
fprintf(‘|%10.6f ‘, u(2:Ny+1, 2:Nx-1)’);
fprintf(‘|%10.6f n’, err);
end
% Ensure SOR solution is stored in the array u
SOR_solution = u;
fprintf(‘%sn’, hline)
cpu_time = toc;
fprintf(‘nCPU time for this method is %1.4f s.n’, cpu_time)
% Analytical solution
u_analytical = @(x, y) sin(pi*x).*cosh(pi*y)./(pi*sinh(pi));
% Plot SOR solution along the vertical central line x=0.5
x_index = find(x(1, 🙂 == 0.5);
figure; % Create a new figure
hold on; % Enable overlaying plots
plot(y(:, x_index), u(:, x_index), ‘b’, ‘DisplayName’, ‘SOR Solution’);
plot(y(:, x_index), u_analytical(0.5, y(:, x_index)), ‘r’, ‘DisplayName’, ‘Analytical Solution’);
xlabel(‘y’);
ylabel(‘U(x,y)’);
legend(‘Location’, ‘best’);
title(‘Comparison of SOR Solution and Analytical Solution at x = 0.5 (w = 1.85)’);
grid on;
hold off; % Release the overlaying plot mode plot numerical solution with analytical solution. MATLAB Answers — New Questions