Error in graph - Finite difference code
Ältere Kommentare anzeigen
I'm a research scholar and I've started coding in MATLAB recently. I'm trying to solve a system of differential equations using finite difference method. The paper I'm working on is by Sharma et al. (I've given the link to the paper here Sharma et al. (2019)). In the paper, they have solved the finite difference equations using Thomas algorithm. But, I'm trying to solve the same finite difference equations without using Thomas algorithm. I've attached my code for your reference. There are no errors shown in the code. However, I'm unable to recreate the graphs given in the paper. May I know where I've gone wrong?
Nr=21; Nz=161; %number of values for radius r, z
R=1; L=16; %max values along each axis
dr=R/(Nr-1); dz=L/(Nz-1); %deltas
r=(0:Nr-1)*dr; z=(0:Nz-1)*dz; %vectors for r, z
u=zeros(Nr,Nz); w=zeros(Nr,Nz);
p=zeros(Nr,Nz); theta=zeros(Nr,Nz);
sigma=zeros(Nr,Nz); %allocate arrays
R=1; M=1; delta=0.6; epsilon=0.005; %constants
alpha=0; n=2; beta=1; Df=1; Sc=0.5; Sr=0.2; %constants
eta=(delta*(n^(n/(n-1))))/(n-1);
%alpha=a/b
h=(1+(epsilon*beta))*(1-eta*((beta-alpha)-(beta-alpha)^n));
%w(:,1)=w*ones(Nr,1); %w distribution at z=0
%w(1,:)=w*ones(1,Nz); %w distribution at r=0
for j=1:Nz-1
for i=2:h
u(i+1,j)=(1/dz*(r(i)))*(((u(i,j))*(r(i))*dz)-((u(i,j))*(dr)*(dz))+((w(i,j))*(r(i))*(dr))-((w(i,j+1))*(r(i))*(dr)));
%(u(i+1,j)-u(i,j))/(dr)+(u(i,j)/r(i,j))+(w(i,j+1)-w(i,j))/(dz)==0;
p(i+1,j)=p(i,j);
p(i,j+1)=p(i,j)+(dz/r(i))*((w(i+1,j)-w(i,j))/dr)+((dz)/(dr)^2)*(w(i+1,j)-2*w(i,j)+w(i-1,j))-(M^2)*dz*w(i,j);
((1+(4/3*R))*(((theta(i+1,j)-theta(i,j))/(dr*(r(i))))+((theta(i+1,j)-2*theta(i,j)+(theta(i-1,j)))/((dr)^2))))+(Df*(((sigma(i+1,j)-sigma(i,j))/(dr*(r(i))))+((sigma(i+1,j)-2*sigma(i,j)+sigma(i-1,j))/((dr)^2))))==0;
((1/Sc)*(((sigma(i+1,j)-sigma(i,j))/(dr*(r(i))))+((sigma(i+1,j)-2*sigma(i,j)+sigma(i-1,j))/((dr)^2))))+((Sr)*(((theta(i+1,j)-theta(i,j))/(dr*(r(i))))+((theta(i+1,j)-2*theta(i,j)+theta(i-1,j))/((dr)^2))))==0;
w(1,j)=w(2,j); theta(1,j)=theta(2,j); sigma(1,j)=sigma(2,j); %boundary condition at r=0
w(Nr,j)=0; theta(Nr,j)=0; sigma(Nr,j)=0; %boundary condition at r=R
%w(i,1)=0; theta(i,1)=0; sigma(i,1)=0;
%w(i,Nz)=0; theta(i,Nz)=0; sigma(i,Nz)=0;
end
end
%plot(r,[w(10,:);w(15,:);w(20,:)])
plot(r,theta)
17 Kommentare
Subha S
am 6 Okt. 2023
Torsten
am 6 Okt. 2023
Do you understand which variables are solved for in the article ? I see w, theta, p and sigma as discretized variables, but only two equations on page 330.
Ok, now I see that the discretized algebraic equations refer to equations (10), (11) and (12) of the paper.
But I cannot believe that P along the z-direction is considered constant. If this were the case, there would be no flow neither in r nor in z direction. Maybe you mean that dP/dz is considered constant.
Although your equations are linear in the unknowns, it will make a lot of effort to collect terms such that your system reads A*[w ;sigma;theta] = b and that you could solve for the unknown vector [w; sigma; theta]. So I suggest that you just implement the system as written in the paper (f(w,sigma,theta) = 0) and use "fsolve" to solve for the unknowns. The (nonlinear) solver "fsolve" should converge in one step to the solution because your system is linear.
Or if you have problems with the discretization, simply use "bvp4c" or "bvp5c" for the solution of stationary boundary value problems.
Equations (11) and (12) are in principle a homogenous system of equations in the unknowns 1/r * d/dr (r*dtheta/dr) and 1/r * d/dr(r*dsigma/dr). The solution is
1/r * d/dr (r*dtheta/dr) = 0
1/r * d/dr (r*dsigma/dr) = 0
So with the boundary conditions given, it's hard for me to understand why not theta = sigma = 0 for all r follows.
Subha S
am 9 Okt. 2023
Torsten
am 9 Okt. 2023
But if theta and sigma are zeros for all r, then how did they obtain non-zero graphs for those variables?
Good question - I don't know.
Subha S
am 10 Okt. 2023
Which equations do you use ?
For the first equation, I get
d^2w/dr^2 + 1/r * dw/dr - M^2*w = dPdz,
thus
dy(1) = y(2);
dy(2) = -1/r * y(2) + M^2 * y(1) + dPdz
I cannot recover that from your code.
Note that you don't need to care about r = 0 in the equations setting if you exclude r = 0 in the mesh definition:
r=linspace(eps,R,10);
Subha S
am 12 Okt. 2023
As I already wrote, the equations (2) and (3) from the paper (and thus yours in the bvp code) must be wrong.
I showed you that they reduce to
1/r * d/dr (r*dtheta/dr) = 0
1/r * d/dr (r*dsigma/dr) = 0
and that they give theta = sigma = 0 (together with the boundary conditions given).
bvp4c doesn't work for combinations of second-order derivatives. You explicitly have to solve for the second-order derivatives and then reduce the system to a first-order system.
For your equations, this means that they can only be solved in the form
1/r * d/dr (r*dtheta/dr) = 0
1/r * d/dr (r*dsigma/dr) = 0
not as
a * 1/r * d/dr (r*dtheta/dr) + b * 1/r * d/dr (r*dsigma/dr) = 0
c * 1/r * d/dr (r*dtheta/dr) + d * 1/r * d/dr (r*dsigma/dr) = 0
R=1.5;
P_0=10;
k=1;
eps=1.0000e-04;
r=linspace(eps,R,1000);
solinit = bvpinit(r,[0,0]);
options = bvpset('RelTol', 10e-4,'AbsTol',10e-7);
sol = bvp4c(@(r,y)soret(r,y,P_0),@soret_BC,solinit,options);
y=deval(sol,r);
plot(r,y(1,:))
function dydx = soret(r,y,P_0) % Details ODE to be solved
dydx = zeros(2,1);
dydx(1) = y(2);
dydx(2) = -P_0 - 1/r * y(2) + y(1) ; % This equation is invalid at r = 0, so taken as r+eps
end
function res = soret_BC(ya,yb) % Details boundary conditions
res=[ya(2);yb(1)];
end
R=1.5;
Q=1;
D_f=1;
Sc=0.5;
Sr=0.2;
k=1;
eps=1.0000e-04;
r=linspace(eps,R,100);
solinit = bvpinit(r,[0,0,1,1]);
options = bvpset('RelTol', 1e-8,'AbsTol',1e-8);
sol = bvp4c(@(r,y)soret(r,y),@soret_BC,solinit,options);
y=deval(sol,r);
plot(r,y(2,:))
function dydx = soret(r,y) % Details ODE to be solved
dydx = zeros(4,1);
dydx(1) = y(3); dydx(2)=y(4);
dydx(3) = -y(3)/r;
dydx(4) = -y(4)/r;
% This equation is invalid at r = 0
end
function res = soret_BC(ya,yb) % Details boundary conditions
res=[ya(3); ya(4); yb(1); yb(2)];
end
Antworten (0)
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




