Numerical solution of mathematical problems and equations
1. A 1D wave equation with impact is numerically solved using MATLAB script. It uses the Euler method in time and second-order accuracy central finite difference scheme in space. It can apply to different places an impact with desired forces.
% 1D wave equation after impact
clear
clc
l = 1; % length of road, m
c = 330; % speed of sound in material, m/s
dx = 0.002; % space discretization, m
dt = 0.9*dx/c; % time discretization, s
r = c*dt/dx; % Courant Number
Im = 0.0015; % impact constant
a = 0.5*l; % distance where impact apply
b = l - a;
T = 1200; % amount of time steps
N = floor(l/dx) + 1; % number of space steps
u = zeros(N,1); % fill displacement vector
uminus = zeros(N,1);
uplus = zeros(N,1);
x = zeros(N,1);
tt = zeros(T,1);
uu = zeros(T,1);
% initial condition
x(1) = 0;
for i = 1: N-1
x(i+1) = x(i) + dx;
if x(i) <= a
u(i) = sqrt(Im*b*x(i)*(l^2 - x(i)^2 - b^2)/l)^1.5;
else
u(i) = sqrt(Im*b*(l*(x(i) - a)^3/b + (l^2 - b^2)*x(i) - x(i)^3)/l)^1.5;
endendfor i = 1: N
u(i) = 0.001*normpdf(x(i),a,0.1);
end
u(1) = 0;
u(N) = 0;
% define uminusfor i = 2:N-1
uminus(i) = u(i) + 0.5*r^2*(u(i+1) - 2*u(i) + u(i-1));
end
figure
t = 0;
for i = 1:T
tt(i) = t;
t = t + dt; % timefor j = 2:N-1
uplus(j) = 2*u(j) - uminus(j) + (r^2)*(u(j+1) - 2*u(j) + u(j-1));
end
uplus(1) = 0; % boundary condition
uplus(N) = 0; % boundary condition
uminus = u;
u = uplus;
uu(i) = u(floor(N/2));
% animation
plot(x, u );
axis([0 l -0.005 0.005]);
xlabel('length of string, m');
ylabel('amplitude of wave, m');
drawnow;
end
figure
plot(tt, uu);
xlabel('time, s');
ylabel('amplitude of wave, m');
2. The numerical solution of a 2D wave equation with impact is shown using MATLAB script. The equation is solved in a circular domain using the polar coordinates. It uses Euler's method in time and second-order accuracy central finite difference scheme in space. It can choose the position and strength of initial impact conditions.
% 2D wave equation after impact in circular
clear
clc
R = 1; % radius of circle, m
c = 6000; % wave speed in material, m/s% polar grid
Nr = 40; % number of space steps in radial direction
hr = R/Nr; % space discretization in radial direction, m
Nt = 40; % number of space steps in tangential direction, Nt must equal to Nr, for use function polarplot3d()
ht = 2*pi/Nt; % space discretization in tangential direction, rad
dt = 0.5*hr*ht/(c*sqrt(ht^2 + 1)); % time discretization, s
T = 520; % amount of time steps
tt = zeros(T,1);
uu = zeros(T,1);
rho = zeros(Nr+1,1);
theta = zeros(Nt+1,1);
rhot = zeros(Nt+1,1);
rho0 = 0.0; % position where is apply the initial condition
theta0 = pi/6; % position where is apply the initial condition
rho(1) = 0;
for i = 2 : Nr + 1
rho(i) = rho(i-1) + hr;
end
theta(1) = 0;
for i = 2 : Nt + 1
theta(i) = theta(i-1) + ht;
polar([theta(i-1) theta(i-1)], [0 1]);
hold onend% plot polar gridfor j = 1: Nr + 1
for i = 1 : Nt + 1
rhot(i) = rho(j);
end
polar(theta, rhot)
title('Polar grid');
hold onend
u = zeros(Nr + 1, Nt + 1); % define sorage of wave function
uminus = zeros(Nr + 1, Nt + 1 );
uplus = zeros(Nr + 1, Nt + 1 );
col = zeros(Nr + 1, Nt + 1 );
% set initial conditionfor i = 1 : Nr + 1
for j = 1 : Nt
l = sqrt(rho0^2 + rho(i)^2 -2*rho0*rho(i)*cos(theta(j) - theta0));
u(i,j) = -0.005*normpdf(l,0,0.8);
endend
rl = 0;
for i = 2 : Nr
rl = rl + hr;
for j = 1 : Nt
if j == Nt
term3 = (u(i,1) - 2*u(i,j) + u(i,j-1))/(rl^2*ht^2);
elseif j == 1
term3 = (u(i,j+1) - 2*u(i,j) + u(i,Nt))/(rl^2*ht^2);
else
term3 = (u(i,j+1) - 2*u(i,j) + u(i,j-1))/(rl^2*ht^2);
endend
term1 = (u(i+1,j) - 2*u(i,j) + u(i-1,j))/hr^2;
term2 = (u(i+1,j) - u(i-1,j))/(rl*hr);
uminus(i,j) = u(i,j) + 0.5*c^2*dt^2*(term1 + term2 + term3);
endend
sum = 0;
for i = 1:Nt
sum = sum + (u(2,i) - u(1,1));
end
uminus(1,1) = u(1,1) + 2*c^2*dt^2*sum/((Nt+1)*hr^2);
uminus(1,:) = uminus(1,1);
uminus(:, Nt+1) = uminus(:, 1);
figure('color','white');
col(:,:) = 12;
[x, y] = pol2cart(theta,rho);
t = 0;
for k = 1:T
tt(k) = t;
t = t + dt;
sum = 0;
for i = 1:Nt
sum = sum + + (u(2,i) - u(1,1));
end
uplus(1,1) = 2*u(1,1) - uminus(1,1) + 4*c^2*dt^2*sum/((Nt+1)*hr^2);
uplus(1,:) = uplus(1,1);
for i = 2: Nr
rl = rl + hr;
for j = 1 : Nt
if j == Nt
term3 = (u(i,1) - 2*u(i,j) + u(i,j-1))/(rl^2*ht^2);
elseif j == 1
term3 = (u(i,j+1) - 2*u(i,j) + u(i,Nt))/(rl^2*ht^2);
else
term3 = (u(i,j+1) - 2*u(i,j) + u(i,j-1))/(rl^2*ht^2);
endend
term1 = (u(i+1,j) - 2*u(i,j) + u(i-1,j))/hr^2;
term2 = (u(i+1,j) - u(i-1,j))/(rl*hr);
uplus(i,j) = 2*u(i,j) - uminus(i,j) + c^2*dt^2*(term1 + term2 + term3);
endend
uplus(Nr+1, :) = 0; % boundary conditions
uplus(:, Nt+1) = uplus(:, 1);
uminus = u;
u = uplus;
uu(k) = u(1,1);
% animation% if you want to speed up the proccess put next lines under the end of% loop, so will eliminate animation%polarplot3d(u );%axis([-1.4 1.4 -1.4 1.4 -0.005 0.005])%xlabel('x, m');%ylabel('y, m');%zlabel('amplitude of wave, m');%drawnow;end
figure
plot(tt, uu);
xlabel('time, s');
ylabel('amplitude of wave, m');
3. I present two methods for a numerical solution to Laplace’s equation in the 2D square domain: Jacobi’s iterative and the Gauss-Seidel SOR method. The suggested methods are compared with the analytical solution for accuracy. To discretize the problem is applied finite differences scheme. The number of iterations significantly drops when using the Gauss-Seidel SOR method for the same accuracy.
% Numerical solution to Laplace's Equation
clear;
clc;
N = 60; % number of space divisions in x and y
L = 1; % length of square
deltax = L/N; % space step in x direction
deltay = deltax; % space step in y direction% exact solution
n = 210; % number of sumation
u = zeros(N+1, N+1); % initialize the exact solution matrix
u(:,1) = 1; % boundary condition at left side
y = 0;
for i = 2 : N
y = y + deltay;
x = 0;
for j = 2 : N
u(i,j) = 0;
x = x + deltax;
for k = 1 : n
An = (1/sinh(-k*pi))*((-2/(k*pi))*cos(k*pi/L) + 2/(k*pi));
u(i,j) = u(i,j) + An*(sin(k*pi*y/L))*sinh(k*pi*(x - L)/L);
endendend% numerical solution
tol = 1e-04; % tolerance of solution% Jacobi's iterative method
uj = zeros(N+1, N+1); % initialize the numerical solution matrix, Jacobi method
uj(:,1) = 1; % boundary condition at left side
ujold = uj;
e = 1;
kj = 0;
while e > deltay*tol
kj = kj + 1; % number of iterationfor i = 2 : N
for j = 2 : N
uj(i,j) = 0.25*(ujold(i+1,j) + ujold(i-1,j) + ujold(i,j+1) + ujold(i,j-1)); % solution using Jcobi methodendend
e = max(max(uj - ujold));
ujold = uj;
end% Point Gauss-Seidel SOR solver
omega = 1.85; % relaxation parameter
ug = zeros(N+1, N+1); % initialize the numerical solution matrix, Gauss-Seidel SOR solver
ug(:,1) = 1; % boundary condition at left side
l = 0;
for omega = 1.95 % relaxation parameter
l = l + 1;
kg = 0;
ugold = zeros(N+1, N+1); % initial solution
ug(:,1) = 1; % boundary condition
e = 1;
while e > deltay*tol
kg = kg + 1; % number of iterationfor i = 2 : N
for j = 2 : N
ug(i,j) =(1 - omega)*ug(i, j) + omega*0.25*(ugold(i+1,j) + ug(i-1,j) + ugold(i,j+1) + ug(i,j-1)); % solution using Point Gauss-Seidel SOR methodendend
e = max(max(ug - ugold));
ugold = ug;
end
iteration(l) = kg;
om(l) = omega;
end
X = 0:deltax:1;
Y = 0:deltay:1;
e = abs(ug - u); % error
plot(X(2:N), e(N/2,2:N))
grid on
xlabel('x');
ylabel('Absolute Error')
title('Error in the numerical solution y =0.5');
figure
mesh(X, Y, ug);
xlabel('x');
ylabel('y');
zlabel('value of u');
title('Solution for N = 60');
grid on
4. The 2D Poisson equation is solved numerically. The proposed equation describes the stress function in the torsion of a prismatic rod. The solution drove the linear algorithm for Poisson's equation. This method uses the finite difference scheme, in which the average values of the neighboring four points give the solution at a given central point. The suggested algorithm is accelerated using a suitable weighting factor. This factor increases up to two as the number of nodes in the domain becomes infinite. The modified algorithm is much faster than the simple linear algorithm for the same accuracy.
% Solution of 2D Poisson's equation in square domain
clc
clear
N = 45; % mesh size
H = -0.05; % parameter
toll = 1e-06; % tollerance
h = 1/(N-1); % grid spacing
maxit = 5000; % maximum number of iteration
error_vector = zeros(N^2,1);
b = zeros(N^2,1);
b(1:N^2) = -H*h^2;
p = zeros(N^2,1); % vector where is stored the potential function, this is initial guess also
p_old = zeros(N^2,1);
p(1:N) = 0; % bottom boundary conditionn
p((N-1)*N:N^2) = 0; % top boundary condition
p(1:N:(N-1)*N+1) = 0; % left boundary condition
p(N:N:N^2) = 0; % right boundary condition% main loop of solution, according the equetion (19)for i = 1: maxit
p_old = p;
for k = 2:N - 1
for j = 2:N - 1
n = j + (k - 1)*N;
p(n) = (p_old(n+1) + p_old(n-1) + p_old(n+N) + p_old(n-N) + b(n))/4;
endendfor k = 2:N-1
for j = 2: N-1
n = j + (k - 1)*N;
error_vector(n) = (p(n) - p_old(n))/p(n);
endend
error = max(abs(error_vector));
if error <= toll
break;
endend
number_of_iteration_1 = i
x = zeros(N,1);
y = zeros(N,1);
for i = 2: N
x(i) = x(i-1) + h;
y(i) = y(i-1) + h;
end% prepare to plot
pp = zeros(N,N);
for n = 1: N*N
k = fix((n-0.1)/N) +1;
j = n-(k-1)*N;
pp(k,j) = p(n);
end
mesh(x,y,pp);
if mod(N,2) == 1 % plot a centerline section
nb=fix(N/2)*N+1;
pc=p(nb:nb+N-1);
figure;
plot(x,pc)
end% main loop of solution, according the equation (25)for i = 1: maxit
p_old = p;
for k = 2:N - 1
for j = 2:N - 1
n = j + (k - 1)*N;
p(n) = (p(n+1) + p(n-1) + p(n+N) + p(n-N) + b(n))/4;
endendfor k = 2:N-1
for j = 2: N-1
n = j + (k - 1)*N;
error_vector(n) = (p(n) - p_old(n))/p(n);
endend
error = max(abs(error_vector));
if error <= toll
break;
endend
number_of_iteration_2 = i
% solution with acceleration scheme
ksi = 1 - 2*(sin(pi/(2*N)))^2;
omega = 2/(1 + sqrt(1 - ksi*ksi)); % weighting factor
v = zeros(N^2,1);
% main loop of solution, according the equation (28)for i = 1: maxit
p_old = p;
for k = 2:N - 1
for j = 2:N - 1
n = j + (k - 1)*N;
v(n) = (p(n+1) + p(n-1) + p(n+N) + p(n-N) + b(n))/4;
endend
p(1:N*N) = omega*v(1:N*N) + (1 - omega)*p_old(1:N*N);
for k = 2:N-1
for j = 2: N-1
n = j + (k - 1)*N;
error_vector(n) = (p(n) - p_old(n))/p(n);
endend
error = max(abs(error_vector));
if error <= toll
break;
endend
number_of_iteration_3 = i
% prepare to plot
figure;
pp = zeros(N,N);
for n = 1: N*N
k = fix((n-0.1)/N) +1;
j = n-(k-1)*N;
pp(k,j) = p(n);
end
mesh(x,y,pp);
if mod(N,2) == 1 % plot a centerline section
nb=fix(N/2)*N+1;
pc=p(nb:nb+N-1);
figure;
plot(x,pc)
end% from number_of iteration, it can be view that acceleration is very fast% compare with another methods