Error in graph - Finite difference code

20 visualizaciones (últimos 30 días)
Subha S
Subha S el 2 de Oct. de 2023
Editada: Torsten el 12 de Oct. de 2023
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 comentarios
Subha S
Subha S el 12 de Oct. de 2023
Thank you Sir for pointing out the error. I've changed the equations for theta and sigma. However, I'm not obtaining a smooth curve, there is a discontinuity that arises due to the problem itself. How to rectify the discontinuity? The same thing happens with w graph as well. Please suggest on how to proceed. I'm attaching my code for both w and theta, sigma.
R=1.5;
P_0=10;
k=1;
eps=1.0000e-06;
r=linspace(-R,R,1000);
solinit = bvpinit(linspace(-R,R,10),[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+eps))*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
The code for theta and sigma are given below
R=1.5;
Function definitions in a script must appear at the end of the file.
Move all statements after the "soret_BC" function definition to before the first local function definition.
Q=1;
D_f=1;
Sc=0.5;
Sr=0.2;
k=1;
eps=1.0000e-06;
r=linspace(-R,R,100);
solinit = bvpinit(linspace(-R,R,100),[0,0,1,1]);
options = bvpset('RelTol', 10e-4,'AbsTol',10e-7);
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+eps));
dydx(4) = -(y(4))/((r+eps));
% 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
Torsten
Torsten el 12 de Oct. de 2023
Editada: Torsten el 12 de Oct. de 2023
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

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by