second order center differencing scheme soluting to PDE
Mostrar comentarios más antiguos
I am trying to find a solution to this PDE:
u* (∂θ/∂x) = (1-a^2)[(1/r) (∂/∂r) (r *(∂θ/∂r))+(∂^2θ/∂x^2]+γ*(∂u/∂r)^2 ,0≤ x ≤ 2, a≤ r ≤ 1
u=2(1-EU*)(1-r^2+B*ln r)/M, B=(a^2-1)/ln r ,E=(a^2-0.5B)/(a^2-1) ,B*=B(1-0.5U*)/(1-EU*), M=1+a^2-B
for r=a
∂θ/∂r=0 ,0≤ x <1
∂θ/∂r=cos[4π(x-1)] ,1≤ x ≤ 1.5
∂θ/∂r=0 ,1.5< x ≤ 2
for r=1
∂θ/∂r=0 ,0≤ x <1
θ=sin[4π(x-1)] ,1≤ x ≤ 1.5
∂θ/∂r=0 ,1.5≤ x ≤<2
for x=0 ,or 2
∂θ/∂r=0
a=0.1,U*=-1,γ=0.5
grid sizes:51*101
I wrote the following code to solve this PDE, but the result is incorrect. I feel like my problem-solving approach has stalled. Could someone please help?
Thank you!
code:
% MATLAB code for solving a partial differential equation using the finite volume method,
% and solving with SOR iteration and TDMA.
% Basic parameters and grid setup
a = 0.1;
gamma = 0.5;
U_star = -1;
Nx = 101; % Number of grid points in x-direction
Nr = 51; % Number of grid points in r-direction
dx = 2 / (Nx - 1); % Grid spacing in x-direction
dr = (1 - a) / (Nr - 1); % Grid spacing in r-direction
x = linspace(0, 2, Nx);
r = linspace(a, 1, Nr);
% Initialize variables
theta = zeros(Nr, Nx);
B = (a^2 - 1) / log(r(end));
E = (a^2 - 0.5 * B) / (a^2 - 1);
B_star = B * (1 - 0.5 * U_star) / (1 - E * U_star);
M = 1 + a^2 - B;
% Set SOR parameters
omega = 1.5; % Over-relaxation factor
tol = 1e-6; % Convergence criterion
max_iter = 10000; % Maximum number of iterations
% Initialize residual and iteration count
residual = inf;
iter = 0;
% Iterative solution
display('Starting SOR iteration...');
while residual > tol && iter < max_iter
theta_old = theta;
% Update in horizontal direction using SOR
for i = 2:Nr-1
for j = 2:Nx-1
% Calculate partial derivatives in r and x directions
dtheta_dr_r = (theta(i+1, j) - theta(i-1, j)) / (2 * dr);
dtheta_dx2 = (theta(i, j+1) - 2 * theta(i, j) + theta(i, j-1)) / (dx^2);
% Discretize the governing equation
theta_new = (1-a^2) * (1/r(i) * dtheta_dr_r + dtheta_dx2) + gamma * (U_star / dr)^2;
% Update using SOR formula
theta(i, j) = (1 - omega) * theta(i, j) + omega * theta_new;
end
end
% Calculate residual
residual = max(max(abs(theta - theta_old)));
iter = iter + 1;
end
display(['SOR iteration completed, total iterations: ', num2str(iter), '.']);
% Use TDMA to solve the matrix problem
for j = 2:Nx-1
% TDMA preparation
A = zeros(Nr, Nr);
B = zeros(Nr, 1);
for i = 2:Nr-1
A(i, i-1) = -1 / dr^2;
A(i, i) = 2 / dr^2 + 2 / dx^2;
A(i, i+1) = -1 / dr^2;
B(i) = theta(i, j);
end
% Set boundary conditions
if x(j) >= 1 && x(j) <= 1.5
B(1) = cos(4 * pi * (x(j) - 1)); % Boundary condition dr=cos[4*pi*(x-1)]
else
B(1) = 0; % Boundary condition dr=0
end
A(1, 1) = 1;
A(Nr, Nr) = 1;
B(Nr) = 0; % Boundary condition dr=0
% Solve using TDMA
theta(:, j) = A\B;
end
% Plot the results
figure;
surf(x, r, theta);
xlabel('x');
ylabel('r');
zlabel('theta');
title('Numerical solution of theta');
% Handling boundary conditions
for j = 1:Nx
if x(j) >= 1 && x(j) <= 1.5
theta(1, j) = -sin(4 * pi * (x(j) - 1));
end
end
% Final result
figure;
contourf(x, r, theta);
colorbar;
xlabel('x');
ylabel('r');
title('Contour plot of theta');
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Geometry and Mesh en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

