Coupled Ion Exchange - Electrodialysis model using partial differential equations

Dear All,
I am trying to model a coupled Ion Exchange - Electrodialysis model. This is similar to the adsorption model. The extra term in this model is that of transport of ions due to the electric field. The first snapshot I have attached shows the original model. The second snapshot shows the rearranged form of the model to match the adsorption model (without axial dispersion). In this second snapshot you can see the extra term containing 'phi', the potential gradient.
In the adsorption model, the 2 independent variables are time and one direction. The 2 dependent variables are bulk concentration and solid phase concentration. In this model, the 3 independent variables are time and 2 directions. 1 direction (x) is the direction the ions move under the influence of the electric field. The second direction (z) is the direction that the bulk fluid moves in. The 3 dependent variables are the bulk concentration, solid phase concentration and the electric potential.
The sample code for the adsorption model is given below (Taken from https://uk.mathworks.com/matlabcentral/answers/385756-adsorption-modelling-solving-pde-axial-dispersion-model. Coded by Ashley Gratton, Torsten). Please let me know how I can change this for my model which has 3 independent and 3 dependent variables.
Thank you in advance.
function main
% Initialisation
close all
clear all
clc
%System Set-up %
%Define Variables
D = 3*10^-8; % Axial Dispersion coefficient
v = 1*10^-3; % Superficial velocity
epsilon = 0.4; % Voidage fraction
k = 3*10^-5; % Mass Transfer Coefficient
b = 2.5*10^-5; % Langmuir parameter
qs = 2*10^4; % Saturation capacity
cFeed = 10; % Feed concentration
L = 1; % Column length
t0 = 0; % Initial Time
tf = 1000; % Final time
dt = 0.5; % Time step
z = [0:0.01:L]; % Mesh generation
t = [t0:dt:tf];% Time vector
n = numel(z); % Size of mesh grid
%Initial Conditions / Vector Creation
c0 = zeros(n,1);
c0(1) = cFeed;
q0 = zeros(n,1); % t = 0, q = 0 for all z, this makes sense to me
y0 = [c0 ; q0]; % Appends conditions together
%ODE15S Solver
[T, Y] = ode15s(@(t,y) MyFun(t,y,z,n),t,y0);
function DyDt=MyFun(~, y, z, n)
% Defining Constants
D = 3*10^-8; % Axial Dispersion coefficient
v = 1*10^-3; % Superficial velocity
epsilon = 0.4; % Voidage fraction
k = 3*10^-5; % Mass Transfer Coefficient
b = 2.5*10^-5; % Langmuir parameter
qs = 20000; % Saturation capacity
% Variables being allocated zero vectors
c = zeros(n,1);
q = zeros(n,1);
DcDt = zeros(n,1);
DqDt = zeros(n,1);
DyDt = zeros(2*n,1);
zhalf = zeros(n-1,1);
DcDz = zeros(n,1);
D2cDz2 = zeros(n,1);
c = y(1:n);
q = y(n+1:2*n);
% Interior mesh points
zhalf(1:n-1)=(z(1:n-1)+z(2:n))/2;
for i=2:n-1
DcDz(i) = ((z(i)-z(i-1))/(z(i+1)-z(i))*(c(i+1)-c(i))+(z(i+1)-z(i))/(z(i)-z(i-1))*(c(i)-c(i-1)))/(z(i+1)-z(i-1));
D2cDz2(i) = (zhalf(i)*(c(i+1)-c(i))/(z(i+1)-z(i))-zhalf(i-1)*(c(i)-c(i-1))/(z(i)-z(i-1)))/(zhalf(i)-zhalf(i-1));
end
% Calculate DcDz and D2cDz2 at z=L for boundary condition dc/dz = 0
DcDz(n) = 0;
D2cDz2(n) = -1.0/(z(n)-zhalf(n-1))*(c(n)-c(n-1))/(z(n)-z(n-1));
% Set time derivatives at z=0
% DcDt = 0 since c=cFeed remains constant over time and is already set as initial condition
% Standard setting for q
DqDt(1) = k*( ((qs*b*c(1))/(1 + b*c(1))) - q(1) );
DcDt(1) = 0.0;
% Set time derivatives in remaining points
for i=2:n
%Equation: dq/dt = k(q*-q) where q* = qs * (b*c)/(1+b*c)
DqDt(i) = k*( ((qs*b*c(i))/(1 + b*c(i))) - q(i) );
%Equation: dc/dt = D * d2c/dz2 - v*dc/dz - ((1-e)/(e))*dq/dt
DcDt(i) = D*D2cDz2(i) - v*DcDz(i) - ((1-epsilon)/(epsilon))*DqDt(i);
end
% Concatenate vector of time derivatives
DyDt = [DcDt;DqDt];
end

34 Comments

Hi
I had a go at editing the code given above. I know there are many mistakes in this. The main one I see is the discretization in the x direction. My system contains an x direction and I don't know how to discretize in the x direction for my problem. Please let me know how I can get this code to run and if it is even possible to use this code for my problem. I have added 'EDIT' comments where I have changed the code.
Thanks in advance.
% Initialisation
close all
clear all
clc
%System Set-up %
%Define Variables
a = 0.4;
delta = 0.0001;
D = 3*10^-8; % Axial Dispersion coefficient
v = 1*10^-3; % Superficial velocity
epsilon = 0.4; % Voidage fraction
K = 3*10^-5; % Mass Transfer Coefficient
u = 0.1;
Itot = 4;
F = 96485.3329;
cHstar = 2*10^-5;
qH = 2*10^-4;
cFeed = 10; % Feed concentration
L = 1; % Column length
W = 1; % EDIT: Column Width
t0 = 0; % Initial Time
tf = 1000; % Final time
dt = 0.5; % Time step
z = [0:0.05:L]; % Mesh generation
x = [0:0.05:W]; % EDIT: Mesh generation in x-direction
t = [t0:dt:tf];% Time vector
n = numel(z); % Size of mesh grid in z direction
m = numel(x); % EDIT: Size of mess grid in x direction
%Initial Conditions / Vector Creation
c0 = zeros(n,1);
c0(1) = cFeed;
q0 = zeros(m,1); % t = 0, q = 0 for all z, this makes sense to me. EDIT: zeros(m,1) instead of zeros (n,1)
phi0 = zeros(m,1); % EDIT: Electric field vector initial conditions. zeros(m,1) instead of zeros (n,1)
J0 = zeros(m,1); % EDIT: Solid flux vector initial conditions. J = u.q.DphiDt. zeros(m,1) instead of zeros (n,1)
y0 = [c0 ; q0 ; phi0 ; J0]; % Appends conditions together
%ODE15S Solver
[T, Y] = ode15s(@(t,y) EDIfun(t,y,z,x,n),t,y0);
plot(T,Y);
function DyDt=EDIfun(~, y, z, x, n)
% Defining Constants
a = 0.4;
delta = 0.0001;
D = 3*10^-8; % Axial Dispersion coefficient
v = 1*10^-3; % Superficial velocity
epsilon = 0.4; % Voidage fraction
K = 3*10^-5; % Mass Transfer Coefficient
u = 0.1;
Itot = 4;
F = 96485.3329;
% Variables being allocated zero vectors
c = zeros(n,1);
q = zeros(m,1); % EDIT: zeros(m,1) instead of zeros (n,1)
phi = zeros(m,1); % EDIT: Electric field vector
J = zeros(m,1); % EDIT: Solid phase flux vector
DcDt = zeros(n,1);
DqDt = zeros(m,1); % EDIT: zeros(m,1) instead of zeros (n,1)
DphiDt = zeros(m,1); % EDIT: Electric field change with time vector
DJDt = zeros(m,1); % EDIT: Solid phase flux with time vector
DyDt = zeros(4*n,1); % EDIT: DyDt has 4 parts instead of 2
%zhalf = zeros(n-1,1); % EDIT: No need for this line because there is no
%D2CDz2.
DcDz = zeros(n,1);
DphiDx = zeros(m,1); % EDIT: added DphiDx vector
DJDx = zeros(m,1); % EDIT: added DJDx vector
c = y(1:n);
q = y(n+1:2*n);
phi = y(2*n+1:3*n); %EDIT: added phi to y vector
J = y(3*n+1:4*n); %EDIT: added J to y vector
% Interior mesh points
for i=2:n-1
DcDz(i) = ((z(i)-z(i-1))/(z(i+1)-z(i))*(c(i+1)-c(i))+(z(i+1)-z(i))/(z(i)-z(i-1))*(c(i)-c(i-1)))/(z(i+1)-z(i-1));
end
%EDIT: added another for loop to discretize in the x direction. I dont know
%how to discretize in the x direction.
J(1) = u*q(1)*DphiDx(1);
DphiDx(1) = Itot/(F*(1-epsilon)*W*L*u*q(1));
DcDz(n) = 0;
DJDx(m) = 0;
DphiDx(m) = 0;
J(m) = 0;
cstar(1) = (q(1)*cHstar)/(K*qH);
for i = 2:m-1
J(i) = u*q(i)*DphiDx(1);
DJDx(i) = (J(i)-J(i-1))/m;
DphiDx(i) = Itot/(F*(1-epsilon)*W*L*u*q(i));
end
% Set time derivatives at z=0
% DcDt = 0 since c=cFeed remains constant over time and is already set as initial condition
% Standard setting for q
% EDIT: changed the DqDt equation
DqDt(1) = a*D*((c(1)-cstar(1))/delta) - DJDx(1);
DcDt(1) = 0.0;
% Set time derivatives in remaining points
for i=2:n
DqDt(i) = (a*K*(c(i)-cstar(i))/delta) - DJDx;
DcDt(i) = - v*DcDz(i) - (((1-epsilon)/(epsilon))*(DqDt(i)+DJDx(i)));
end
% Concatenate vector of time derivatives
DyDt = [DcDt;DqDt;DphiDt;DJDt]; %EDIT: concatenated for all 4 dependent variables
end
When looking at your model, the following questions arose:
c_i^b only depends on z ?
c_i^hat depends on z and x ?
d_phi_dx is only a single number (thus not dependent on x or z) ?
Boundary conditions for c_i^b at z = 0 ?
Boundary conditions for c_i^hat at x = 0 ?
How large is "n" in d_phi_dx ?
Hi Torsten. Thank you for your comment. According to my understanding of the model:
Yes ci^b (bulk concentration) changes only with z direction.
ci^hat (solid phase concentration) changes only with x direction (please let me know if your understanding of the model suggests otherwise).
I was given this relationship of dphidx from literature. I understand that it is dependent on the x direction.
At z = 0 i imagine the boundary condition is that ci^b = ciFeed as in the adsorption model I have used above. I am assuming just any number for that at the moment. Lets say 10.
At x = 0 ci^hat should be equal to 0.
For now I am ignoring the sigma(i =1 to n). n is actually supposed to be the number of ions. But at the moment I am trying to model for just a single ion. So assume n = 1. If I am successful, I will expand the model to multiple ions. You can also assume "zi" in that equation to be equal to 1. That is the charge on the ions.
ci^hat (solid phase concentration) changes only with x direction (please let me know if your understanding of the model suggests otherwise).
Since c_i^hat depends on c_i^b, c_i^hat must depend on z and x.
I was given this relationship of dphidx from literature. I understand that it is dependent on the x direction.
In your notation, I don't see a dependence on x for dphidx. It is a single number since all the space-dependent contributions are integrated out.
Thank you for your comment.
I understand what you are saying. Thank you for clearing that out for me.
Does this mean that there is missing information in the model? How can I proceed with modelling this system?
How can I proceed with modelling this system?
First by clarifying the questions - maybe by contacting the authors.
Thank you for your comment.
If ci^hat does depend on ci^b, what needs to be added or changed? Similarly if dphidx is a single number then what needs to be added or changed? Is the given information not suffient to carry out the modelling?
Your model is too much different from the one you want to take as reference. If I were you, I'd start programming your model from scratch.
If z_i (i=1,...,Z) is a discretization for the interval [0;1] and x_j (j=1,...,X) is a disretization for the interval [0;d], you will have to solve for c_k^b at z=z_i (i=1,...,Z) and c_k^hat at z=z_i and x=x_j(i=1,...,Z;j=1,...,X).
Discretize dc_k^b/dz at z=z_i and dc_k^hat/dx with a first-order upwind scheme.
The integral in the computation of dphidx as a single value can be evaluated using the trapezoidal rule, e.g.
This gives Z + X*Z ordinary differential equations that can be solved using ODE15S.
Good luck.
Thank you for this.
I will have a go and get back to you if I run into problems.
Is there any source I can look at to code the first order upwind scheme in MATLAB?
dc_k^b/dz at z=z_i is approximately
(c_k^b(i)-x_k^b(i-1))/(z(i)-z(i-1))
and
dc_k^hat/dx at z=z_i and x=x_j is approximately
(c_k^hat(i,j)-x_k^hat(i,j-1))/(x(j)-x(j-1))
Here I assumed that "nu" and "u_k^hat*dphidx" are positive in your model.
Thanks! I will have a go and let you know if I get stuck. I appreciate all your help.
Hi,
In your previous comment could you please clarify what is:
1) x_k^b(i-1)
2) x_k^hat(i,j-1)
Thanks.
Should read
c_k^b(i-1)
and
c_k^hat(i,j-1)
thus the solution variables at z_(i-1) resp. (z_i,x_(j-1)).
Thank you.
I am trying to write my data file and using the methodology that you used in the adsorption model where you concatenate the c and q vectors into one y matrix. Because c is a (n,1) vector and q is a (n,m) matrix, they cannot be concatenated into a y matrix. How do I proceed with this? Please see my code below.
Thanks in advance.
%% Defining constants
W = 1; %Width of column
L = 1; %Length of column
a = 0.4; %Surface area to volume ratio
delta = 0.0001; %Film thickness
D = 3*10^-8; %Film diffusion coefficient
v = 1*10^-3; %Velocity of fluid
epsilon = 0.4; %Bed porosity
K = 3*10^-5; %Equilibrium constant between solid phase and interface
u = 0.1; %Mobility
Itot = 4; %Total current
F = 96485.3329; %Faraday constant
cHstar = 2*10^-5; %Interfacial concentration of counter ion (hydrogen)
qH = 2*10^-4; %Solid phase concentration of counter ion (hydrogen)
cFeed = 0.1; %Feed concentration
%% Initializing Vectors for Independent variables
t0 = 0; %Initial time
tf = 100; %Final time
dt = 0.5; %Time step
t = [0:dt:tf]; %Time vector
i = [0:0.1:L]; %Mesh generation for z direction
j = [0:0.1:W]; %Mesh generation for x direction
n = numel(i); %Number of elements in i
m = numel(j); %Number of elements in j
%% Initialization of Dependent variables
c0 = zeros(n,1) ; %Initial bulk concentration vector
c0(1) = cFeed; %Initial bulk concentration at z = 0
q0 = zeros(n,m); %Initial solid phase concentration matrix. Dependent on x and z directions.
phi0 = zeros(n,m); %Initial electric potential matrix. Dependent on x and z directions.
J0 = zeros(n,m); %Initial Solid phase flux matrix. Dependent on x and z directions.
y0 = [c0;q0]; %Initial y vector appending together c0 and q0
y0 = [c0;reshape(q0,n*m,1)];
But I strongly recommend to check the open questions first before programming because the complete code might change if the model has to be modified.
Thank you.
Sorry what do you mean by open questions?
I will think about modifying the model to make it steady state later on. In that case it would be only algebraic equations. I have to do this because I want to optimise it on GAMS, which does not deal with differential equations well.
With open questions I mean especially the dphidx-term. I doubt that it is constant for all positions.
Thanks.
I will look at those once I am able to run this model and get a better understanding. So far I have this code. The data file followed by the function file. Please let me know what I am doing wrong. I have pasted my code below, followed by the error that I am getting. Thanks in advance.
Data file:
%% Defining constants
W = 1; %Width of column
L = 1; %Length of column
a = 0.4; %Surface area to volume ratio
delta = 0.0001; %Film thickness
D = 3*10^-8; %Film diffusion coefficient
v = 1*10^-3; %Velocity of fluid
epsilon = 0.4; %Bed porosity
K = 3*10^-5; %Equilibrium constant between solid phase and interface
u = 0.1; %Mobility
Itot = 4; %Total current
F = 96485.3329; %Faraday constant
cHstar = 2*10^-5; %Interfacial concentration of counter ion (hydrogen)
qH = 2*10^-4; %Solid phase concentration of counter ion (hydrogen)
cFeed = 0.1; %Feed concentration
%% Initializing Vectors for Independent variables
t0 = 0; %Initial time
tf = 100; %Final time
dt = 0.5; %Time step
t = [0:dt:tf]; %Time vector
i = [0:0.1:L]; %Mesh generation for z direction
j = [0:0.1:W]; %Mesh generation for x direction
n = numel(i); %Number of elements in i
m = numel(j); %Number of elements in j
%% Initialization of Dependent variables
c0 = zeros(n,1) ; %Initial bulk concentration vector
c0(1) = cFeed; %Initial bulk concentration at z = 0
q0 = zeros(n,m); %Initial solid phase concentration matrix. Dependent on x and z directions.
phi0 = zeros(n,m); %Initial electric potential matrix. Dependent on x and z directions.
J0 = zeros(n,m); %Initial Solid phase flux matrix. Dependent on x and z directions.
y0 = [c0;reshape(q0,n*m,1)]; %Initial y vector appending together c0 and q0
[T,Y] = ode15s(@(t,y)EDIfun2(t,y,i,j,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed),t,y0);
Function file:
function DyDt=EDIfun2(t,y,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed);
%% Variables being allocated zero vectors
c = zeros(n,1); %Bulk concentration vector
q = zeros(n,m); %Solid phase concentration matrix. Dependent on x and z directions.
phi = zeros(n,m); %Potential matrix. Dependent on x and z directions.
J = zeros(n,m); %Solid phase flux matrix. Dependent on x and z directions.
cstar = zeros(n,m); %Cstar variable representing
DcDt = zeros(n,1); %Time differential vector of bulk concentration
DqDt = zeros(n,m); %Time differential matrix of solid phase concentration. Dependent on x and z directions.
DyDt = zeros(n+n*m,1); %Time differential vector of concatenated c and q
%% Discretizing in x and z directions
DcDz = zeros(n,1); %Initial vector for bulk concentration change with z direction
DqDx = zeros(n,m); %Initial matrix for solid phase concentration change in x and z directions
DJDx = zeros(n,m); %Initializing matrix for solid phase flux change in x and z directions
DphiDx = zeros(n,m); %Initializing matrix for potential gradient change in x and z directions
c = y(1:n); %Initial vector for bulk concentration allocated to y vector
q = y(n+1:n+n*m); %Initial vector for solid phase concetration allocated to y vector
DcDz(1) = 0; %Initial bulk concentration differential
DqDx(1,1) = 0; %Initial solid phase concentration differential
J(1,1) = 0; %Initial solid phase flux
DphiDx(1,1) = 0; %Initial potential gradient
for i = 2:n-1
DcDz(i) = (c(i)-c(i-1))/(z(i)-z(i-1)); %Discretization of bulk concentration with z direction
for j = 2:m-1
DqDx(i,j) = (q(i,j)-q(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase concentration with x and z direction
J(i,j) = u*q(i,j)*DphiDx(i,j); %Equation for solid phase flux
DJDx(i,j) = (J(i,j)-J(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase flux with x and z direction
DphiDx(i,j) = Itot/(F*(1-epsilon)*W*L*u*q(i,j)); %Equation for DphiDx
cstar(i,j) = (q(i,j)*cHstar)/(K*qH); %Equation for cstar
end
end
DcDz(n) = 0; %Final bulk concentration differential
DqDx(n,m) = 0; %Final solid phase concentration differential
DJDx(n,m) = 0; %Final solid phase flux
DphiDx(n,m) = 0; %Final potential gradient
%% Differential equation with time
DcDt(1) = 0; %Initial bulk concentration differential with time
DqDt(1,1) = 0; %Initial solid phase concentration differential with time
for i = 2:n
for j = 2:m
DcDt(i) = -v*DcDz(i) - (((1-epsilon)/epsilon)*(DqDt(i) + DJDx(i,j)));
DqDt(i,j) = a*D*(q(i,j)-(cstar(i,j))) - DJDx;
end
end
DyDt = [DcDt;reshape(DqDt,n*m,1)];
end
Error:
Error using EDIfun2
Too many input arguments.
Error in EDIdatafile2>@(t,y)EDIfun2(t,y,i,j,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed)
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in EDIdatafile2 (line 38)
[T,Y] = ode15s(@(t,y)EDIfun2(t,y,i,j,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed),t,y0);
Count the number of variables in the call list to EDIfun2 and in the receive list. They do not coincide.
Thanks for the comment.
My initial mistake in the function file:
function DyDt=EDIfun2(t,y,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed);
Change I made:
function DyDt=EDIfun2(t,y,i,j,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed);
New error:
Error in EDIdatafile2>@(t,y)EDIfun2(t,y,i,j,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed)
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in EDIdatafile2 (line 38)
[T,Y] = ode15s(@(t,y)EDIfun2(t,y,i,j,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed),t,y0);
Within EDIfun2, you use i and j as loop indices and z and x are the corresponding arrays of mesh points.
According to your parameter list, you had to use i and j as arrays of mesh points and x and z are undefined.
Thanks for the comment.
I changed the instances of i and j to z and x respectively and left i and j only in the loops.
New data file:
%% Defining constants
W = 1; %Width of column
L = 1; %Length of column
a = 0.4; %Surface area to volume ratio
delta = 0.0001; %Film thickness
D = 3*10^-8; %Film diffusion coefficient
v = 1*10^-3; %Velocity of fluid
epsilon = 0.4; %Bed porosity
K = 3*10^-5; %Equilibrium constant between solid phase and interface
u = 0.1; %Mobility
Itot = 4; %Total current
F = 96485.3329; %Faraday constant
cHstar = 2*10^-5; %Interfacial concentration of counter ion (hydrogen)
qH = 2*10^-4; %Solid phase concentration of counter ion (hydrogen)
cFeed = 0.1; %Feed concentration
%% Initializing Vectors for Independent variables
t0 = 0; %Initial time
tf = 100; %Final time
dt = 0.5; %Time step
t = [0:dt:tf]; %Time vector
z = [0:0.1:L]; %Mesh generation for z direction
x = [0:0.1:W]; %Mesh generation for x direction
n = numel(z); %Number of elements in z
m = numel(x); %Number of elements in x
%% Initialization of Dependent variables
c0 = zeros(n,1) ; %Initial bulk concentration vector
c0(1) = cFeed; %Initial bulk concentration at z = 0
q0 = zeros(n,m); %Initial solid phase concentration matrix. Dependent on x and z directions.
phi0 = zeros(n,m); %Initial electric potential matrix. Dependent on x and z directions.
J0 = zeros(n,m); %Initial Solid phase flux matrix. Dependent on x and z directions.
y0 = [c0;reshape(q0,n*m,1)]; %Initial y vector appending together c0 and q0
[T,Y] = ode15s(@(t,y)EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed),t,y0);
New function file:
function DyDt=EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed);
%% Variables being allocated zero vectors
c = zeros(n,1); %Bulk concentration vector
q = zeros(n,m); %Solid phase concentration matrix. Dependent on x and z directions.
phi = zeros(n,m); %Potential matrix. Dependent on x and z directions.
J = zeros(n,m); %Solid phase flux matrix. Dependent on x and z directions.
cstar = zeros(n,m); %Cstar variable representing
DcDt = zeros(n,1); %Time differential vector of bulk concentration
DqDt = zeros(n,m); %Time differential matrix of solid phase concentration. Dependent on x and z directions.
DyDt = zeros(n+n*m,1); %Time differential vector of concatenated c and q
%% Discretizing in x and z directions
DcDz = zeros(n,1); %Initial vector for bulk concentration change with z direction
DqDx = zeros(n,m); %Initial matrix for solid phase concentration change in x and z directions
DJDx = zeros(n,m); %Initializing matrix for solid phase flux change in x and z directions
DphiDx = zeros(n,m); %Initializing matrix for potential gradient change in x and z directions
c = y(1:n); %Initial vector for bulk concentration allocated to y vector
q = y(n+1:n+n*m); %Initial vector for solid phase concetration allocated to y vector
DcDz(1) = 0; %Initial bulk concentration differential
DqDx(1,1) = 0; %Initial solid phase concentration differential
J(1,1) = 0; %Initial solid phase flux
DphiDx(1,1) = 0; %Initial potential gradient
for i = 2:n-1
DcDz(i) = (c(i)-c(i-1))/(z(i)-z(i-1)); %Discretization of bulk concentration with z direction
for j = 2:m-1
DqDx(i,j) = (q(i,j)-q(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase concentration with x and z direction
J(i,j) = u*q(i,j)*DphiDx(i,j); %Equation for solid phase flux
DJDx(i,j) = (J(i,j)-J(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase flux with x and z direction
DphiDx(i,j) = Itot/(F*(1-epsilon)*W*L*u*q(i,j)); %Equation for DphiDx
cstar(i,j) = (q(i,j)*cHstar)/(K*qH); %Equation for cstar
end
end
DcDz(n) = 0; %Final bulk concentration differential
DqDx(n,m) = 0; %Final solid phase concentration differential
DJDx(n,m) = 0; %Final solid phase flux
DphiDx(n,m) = 0; %Final potential gradient
%% Differential equation with time
DcDt(1) = 0; %Initial bulk concentration differential with time
DqDt(1,1) = 0; %Initial solid phase concentration differential with time
for i = 2:n
for j = 2:m
DcDt(i) = -v*DcDz(i) - (((1-epsilon)/epsilon)*(DqDt(i) + DJDx(i,j)));
DqDt(i,j) = a*D*(q(i,j)-(cstar(i,j))) - DJDx;
end
end
DyDt = [DcDt;reshape(DqDt,n*m,1)];
end
New error:
Index in position 2 exceeds array bounds (must not exceed 1).
Error in EDIfun2 (line 28)
DqDx(i,j) = (q(i,j)-q(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase concentration with x and z direction
Error in EDIdatafile2>@(t,y)EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed)
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in EDIdatafile2 (line 38)
[T,Y] = ode15s(@(t,y)EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed),t,y0);
I don't understand why you can't identify such obvious programming errors on your own.
In EDIfun2, you set
q = y(n+1:n+n*m);
and then use q in
DqDx(i,j) = (q(i,j)-q(i,j-1))/(x(j)-x(j-1));
Don't you see the problem ?
Please don't immediately post every single error - start thinking and use MATLAB's debugger.
Thanks for your comment.
I have worked on the code and managed to get a solution.
In my Y matrix, all the values that represent 'q' are 0 for all time. The q in my code is 'c^hat' in the model snapshot. When I looked at my model snapshot again ('EDI final model') and looked at the 'Jelec' term, it contains the solid phase concentration 'c^hat'. When substituing the 'DphiDx' term into the 'Jelec' equation, the 'c^hat' term cancels out, leaving 'Jelec' as a constant and so 'DJDx' = 0. Is this a correct analysis?
Data file:
%% Defining constants
W = 1; %Width of column
L = 1; %Length of column
a = 0.4; %Surface area to volume ratio
delta = 0.0001; %Film thickness
D = 3*10^-8; %Film diffusion coefficient
v = 1*10^-3; %Velocity of fluid
epsilon = 0.4; %Bed porosity
K = 3*10^-5; %Equilibrium constant between solid phase and interface
u = 0.1; %Mobility
Itot = 4; %Total current
F = 96485.3329; %Faraday constant
cHstar = 2*10^-5; %Interfacial concentration of counter ion (hydrogen)
qH = 2*10^-4; %Solid phase concentration of counter ion (hydrogen)
cFeed = 0.1; %Feed concentration
%% Initializing Vectors for Independent variables
t0 = 0; %Initial time
tf = 100; %Final time
dt = 0.5; %Time step
t = [0:dt:tf]; %Time vector
z = [0:0.1:L]; %Mesh generation for z direction
x = [0:0.1:W]; %Mesh generation for x direction
n = numel(z); %Number of elements in z
m = numel(x); %Number of elements in x
%% Initialization of Dependent variables
c0 = zeros(n,1) ; %Initial bulk concentration vector
c0(1) = cFeed; %Initial bulk concentration at z = 0
q0 = zeros(n,m); %Initial solid phase concentration matrix. Dependent on x and z directions.
phi0 = zeros(n,m); %Initial electric potential matrix. Dependent on x and z directions.
J0 = zeros(n,m); %Initial Solid phase flux matrix. Dependent on x and z directions.
y0 = [c0;reshape(q0,n*m,1)]; %Initial y vector appending together c0 and q0
[T,Y] = ode15s(@(t,y)EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed),t,y0);
Function file:
function DyDt=EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed);
%% Variables being allocated zero vectors
c = zeros(n,1); %Bulk concentration vector
q = zeros(n,m); %Solid phase concentration matrix. Dependent on x and z directions.
phi = zeros(n,m); %Potential matrix. Dependent on x and z directions.
J = zeros(n,m); %Solid phase flux matrix. Dependent on x and z directions.
cstar = zeros(n,m); %Cstar variable representing
DcDt = zeros(n,1); %Time differential vector of bulk concentration
DqDt = zeros(n,m); %Time differential matrix of solid phase concentration. Dependent on x and z directions.
DyDt = zeros(n+n*m,1); %Time differential vector of concatenated c and q
%% Discretizing in x and z directions
DcDz = zeros(n,1); %Initial vector for bulk concentration change with z direction
DqDx = zeros(n,m); %Initial matrix for solid phase concentration change in x and z directions
DJDx = zeros(n,m); %Initializing matrix for solid phase flux change in x and z directions
DphiDx = zeros(n,m); %Initializing matrix for potential gradient change in x and z directions
c = y(1:n); %Initial vector for bulk concentration allocated to y vector
q = reshape(y(n+1:n+n*m),n,m); %Initial vector for solid phase concetration allocated to y vector
DcDz(1) = 0; %Initial bulk concentration differential
DqDx(1,1) = 0; %Initial solid phase concentration differential
J(1,1) = 0; %Initial solid phase flux
DphiDx(1,1) = 0; %Initial potential gradient
for i = 2:n-1
DcDz(i) = (c(i)-c(i-1))/(z(i)-z(i-1)); %Discretization of bulk concentration with z direction
for j = 2:m-1
DqDx(i,j) = (q(i,j)-q(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase concentration with x and z direction
J(i,j) = u*q(i,j)*DphiDx(i,j); %Equation for solid phase flux
DJDx(i,j) = (J(i,j)-J(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase flux with x and z direction
DphiDx(i,j) = Itot/(F*(1-epsilon)*W*L*u*q(i,j)); %Equation for DphiDx
cstar(i,j) = (q(i,j)*cHstar)/(K*qH); %Equation for cstar
end
end
DcDz(n) = 0; %Final bulk concentration differential
DqDx(n,m) = 0; %Final solid phase concentration differential
DJDx(n,m) = 0; %Final solid phase flux
DphiDx(n,m) = 0; %Final potential gradient
%% Differential equation with time
DcDt(1) = 0; %Initial bulk concentration differential with time
DqDt(1,1) = 0; %Initial solid phase concentration differential with time
for i = 2:n
for j = 2:m
DcDt(i) = -v*DcDz(i) - (((1-epsilon)/epsilon)*(DqDt(i) + DJDx(i,j)));
DqDt(i,j) = a*D*(q(i,j)-(cstar(i,j))) - DJDx(i,j);
end
end
DyDt = [DcDt;reshape(DqDt,n*m,1)];
end
Update:
I changed the order of equations in the first 'for' loop which was the mistake. But now there is another error which I am unable to interpret.
Data file:
%% Defining constants
W = 0.5; %Width of column
L = 1; %Length of column
a = 2; %Surface area to volume ratio
delta = 0.1; %Film thickness
D = 3*10^-3; %Film diffusion coefficient
v = 0.1; %Velocity of fluid
epsilon = 0.4; %Bed porosity
K = 0.2; %Equilibrium constant between solid phase and interface
u = 0.1; %Mobility
Itot = 4; %Total current
F = 96485.3329; %Faraday constant
cHstar = 2*10^-1; %Interfacial concentration of counter ion (hydrogen)
qH = 2*10^-1; %Solid phase concentration of counter ion (hydrogen)
cFeed = 0.0001; %Feed concentration
%% Initializing Vectors for Independent variables
t0 = 0; %Initial time
tf = 10; %Final time
dt = 0.01; %Time step
t = [0:dt:tf]; %Time vector
z = [0:0.1:L]; %Mesh generation for z direction
x = [0:0.1:W]; %Mesh generation for x direction
n = numel(z); %Number of elements in z
m = numel(x); %Number of elements in x
%% Initialization of Dependent variables
c0 = zeros(n,1) ; %Initial bulk concentration vector
c0(1) = cFeed; %Initial bulk concentration at z = 0
q0 = zeros(n,m); %Initial solid phase concentration matrix. Dependent on x and z directions.
phi0 = zeros(n,m); %Initial electric potential matrix. Dependent on x and z directions.
J0 = zeros(n,m); %Initial Solid phase flux matrix. Dependent on x and z directions.
y0 = [c0;reshape(q0,n*m,1)]; %Initial y vector appending together c0 and q0
[T,Y] = ode15s(@(t,y)EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed),t,y0);
plot(T,Y);
Function file:
function DyDt=EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed);
%% Variables being allocated zero vectors
c = zeros(n,1); %Bulk concentration vector
q = zeros(n,m); %Solid phase concentration matrix. Dependent on x and z directions.
phi = zeros(n,m); %Potential matrix. Dependent on x and z directions.
J = zeros(n,m); %Solid phase flux matrix. Dependent on x and z directions.
cstar = zeros(n,m); %Cstar variable representing
DcDt = zeros(n,1); %Time differential vector of bulk concentration
DqDt = zeros(n,m); %Time differential matrix of solid phase concentration. Dependent on x and z directions.
DyDt = zeros(n+n*m,1); %Time differential vector of concatenated c and q
%% Discretizing in x and z directions
DcDz = zeros(n,1); %Initial vector for bulk concentration change with z direction
DqDx = zeros(n,m); %Initial matrix for solid phase concentration change in x and z directions
DJDx = zeros(n,m); %Initializing matrix for solid phase flux change in x and z directions
DphiDx = zeros(n,m); %Initializing matrix for potential gradient change in x and z directions
c = y(1:n); %Initial vector for bulk concentration allocated to y vector
q = reshape(y(n+1:n+n*m),n,m); %Initial vector for solid phase concetration allocated to y vector
DcDz(1) = 0; %Initial bulk concentration differential
DqDx(1,1) = 0; %Initial solid phase concentration differential
J(1,1) = 0; %Initial solid phase flux
DphiDx(1,1) = 0; %Initial potential gradient
for i = 2:n-1
for j = 2:m-1
DcDz(i) = (c(i)-c(i-1))/(z(i)-z(i-1)); %Discretization of bulk concentration with z direction
DqDx(i,j) = (q(i,j)-q(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase concentration with x and z direction
DphiDx(i,j) = Itot/(F*(1-epsilon)*W*L*u*q(i,j)); %Equation for DphiDx
J(i,j) = u*q(i,j)*DphiDx(i,j); %Equation for solid phase flux
DJDx(i,j) = (J(i,j)-J(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase flux with x and z direction
cstar(i,j) = (q(i,j)*cHstar)/(K*qH); %Equation for cstar
end
end
DcDz(n) = 0; %Final bulk concentration differential
DqDx(n,m) = 0; %Final solid phase concentration differential
DJDx(n,m) = 0; %Final solid phase flux
DphiDx(n,m) = 0; %Final potential gradient
%% Differential equation with time
DcDt(1) = 1; %Initial bulk concentration differential with time
DqDt(1,1) = 1; %Initial solid phase concentration differential with time
for i = 2:n
for j = 2:m
DcDt(i) = -v*DcDz(i) + (((1-epsilon)/epsilon)*(DqDt(i,j) + DJDx(i,j)));
DqDt(i,j) = a*D*(q(i,j)-(cstar(i,j))) - DJDx(i,j);
end
end
DyDt = [DcDt;reshape(DqDt,n*m,1)];
end
Error:
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.
> In ode15s (line 589)
In EDIdatafile2 (line 38)
Warning: Failure at t=0.000000e+00. Unable to meet integration tolerances without reducing the step size below the smallest value
allowed (7.905050e-323) at time t.
> In ode15s (line 668)
In EDIdatafile2 (line 38)
q=0 at t=0, and you divide by q when calculating DphiDx.
Again: Print Dydt in EDIfun2 until it contains reasonable values and use the debugger.
Note that according to your model equations, DphiDx does not depend on x and z . In your code from above, it does because you replace the integrand by its value in (z(i),x(j)).
Thanks for your comment.
q=0 at t=0, and you divide by q when calculating DphiDx.
I was able to correct the error by initializing q0 as 'ones(n,m)' instead of 'zeros(n,m)' in my data file.
Again: Print Dydt in EDIfun2 until it contains reasonable values and use the debugger.
I printed this in the function file and got a series of '0's and and '-0.0015's in the command window. I will look more into this now. Thank you.
Note that according to your model equations, DphiDx does not depend on x and z . In your code from above, it does because you replace the integrand by its value in (z(i),x(j)).
Yes I realize that. I will change it. However, if I don't change this, the DphiDx matrix would just be a matrix of '0's right? I mean it would not affect my final solution would it?
Thanks.
Why should DphiDx be a matrix of 0's ? If you start with q=1, it is a single number different from 0. And if it is a single number that does not depend on z and x, it will affect DJdx.
Note further that you use DqDt(i,j) in your loop before you define it.
Thanks for the comment.
Why should DphiDx be a matrix of 0's ?
Sorry I meant a matrix of constants, since it is not dependent on z and x.
If you start with q=1, it is a single number different from 0. And if it is a single number that does not depend on z and x, it will affect DJdx.
If I have q = 0 then I cant divide it in the DphiDx equation. What would you suggest for this problem?
Note further that you use DqDt(i,j) in your loop before you define it.
I defined DqDt and DcDt in the function file at the top. Here is the section of code which shows where i defined it:
function DyDt=EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed);
%% Variables being allocated zero vectors
c = zeros(n,1); %Bulk concentration vector
q = zeros(n,m); %Solid phase concentration matrix. Dependent on x and z directions.
phi = zeros(n,m); %Potential matrix. Dependent on x and z directions.
J = zeros(n,m); %Solid phase flux matrix. Dependent on x and z directions.
cstar = zeros(n,m); %Cstar variable representing
DcDt = zeros(n,1); %Time differential vector of bulk concentration
DqDt = zeros(n,m); %Time differential matrix of solid phase concentration. Dependent on x and z directions.
DyDt = zeros(n+n*m,1); %Time differential vector of concatenated c and q
I defined DqDt and DcDt in the function file at the top. Here is the section of code which shows where i defined it:
This is only an initialization of the arrays.
What I mean is
for i = 2:n
for j = 2:m
DcDt(i) = -v*DcDz(i) + (((1-epsilon)/epsilon)*(DqDt(i,j) + DJDx(i,j)));
DqDt(i,j) = a*D*(q(i,j)-(cstar(i,j))) - DJDx(i,j);
end
end
You use DqDt(i,j) in the calculation of DcDt(i) before you define it as
DqDt(i,j) = a*D*(q(i,j)-(cstar(i,j))) - DJDx(i,j);
Ah yes makes sense thank you.
Any ideas on this part:
If I have q = 0 then I cant divide it in the DphiDx equation. What would you suggest for this problem?
Thanks.
If I have q = 0 then I cant divide it in the DphiDx equation. What would you suggest for this problem?
I don't know about the physical background of your problem.
So I can't tell what makes sense here as initial condition for q.
Thanks for your comment.
I have spent some time analyzing my solution. The variation in the z direction seems fine as I am getting a reasonable result for DcDz. However, the x direction is giving strange results. For example DJDx, DphiDx. I have printed DJDx and DphiDx so that it shows their values when you run the code.
Could this be because of the way I have discretized DJDx and DphiDx? I used the upwind method that you provided in one of your earlier comments.
Data file:
%% Defining constants
W = 0.1; %Width of column
L = 1; %Length of column
a = 2; %Surface area to volume ratio
delta = 0.00001; %Film thickness
D = 3*10^-1; %Film diffusion coefficient
v = 0.1; %Velocity of fluid
epsilon = 0.9; %Bed porosity
K = 0.001; %Equilibrium constant between solid phase and interface
u = 2; %Mobility
Itot = 5; %Total current
F = 96485.3329; %Faraday constant
cHstar = 1; %Interfacial concentration of counter ion (hydrogen)
qH = 2; %Solid phase concentration of counter ion (hydrogen)
cFeed = 0.01; %Feed concentration
%% Initializing Vectors for Independent variables
t0 = 0; %Initial time
tf = 10; %Final time
dt = 0.1; %Time step
t = [0:dt:tf]; %Time vector
z = [0:0.1:L]; %Mesh generation for z direction
x = [0:0.01:W]; %Mesh generation for x direction
n = numel(z); %Number of elements in z
m = numel(x); %Number of elements in x
%% Initialization of Dependent variables
c0 = zeros(n,1) ; %Initial bulk concentration vector
c0(1) = cFeed; %Initial bulk concentration at z = 0
q0 = 0.00000001*ones(n,m); %Initial solid phase concentration matrix. Dependent on x and z directions.
phi0 = zeros(n,m); %Initial electric potential matrix. Dependent on x and z directions.
J0 = zeros(n,m); %Initial Solid phase flux matrix. Dependent on x and z directions.
y0 = [c0;reshape(q0,n*m,1)]; %Initial y vector appending together c0 and q0
[T,Y] = ode15s(@(t,y)EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed),t,y0);
%plot(z,Yc(:,:));
%Yc = Y(:,[1:n]);
%Yq = Y(:,[n+1:n+n*m]);
%Yqr = reshape(Yq(2,:),n,m);
Function file:
function DyDt=EDIfun2(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed);
%% Variables being allocated zero vectors
c = zeros(n,1); %Bulk concentration vector
q = zeros(n,m); %Solid phase concentration matrix. Dependent on x and z directions.
phi = zeros(n,m); %Potential matrix. Dependent on x and z directions.
J = zeros(n,m); %Solid phase flux matrix. Dependent on x and z directions.
cstar = zeros(n,m); %Cstar variable representing
DcDt = zeros(n,1); %Time differential vector of bulk concentration
DqDt = zeros(n,m); %Time differential matrix of solid phase concentration. Dependent on x and z directions.
DyDt = zeros(n+n*m,1); %Time differential vector of concatenated c and q
%% Discretizing in x and z directions
DcDz = zeros(n,1); %Initial vector for bulk concentration change with z direction
DqDx = zeros(n,m); %Initial matrix for solid phase concentration change in x and z directions
DJDx = zeros(n,m); %Initializing matrix for solid phase flux change in x and z directions
DphiDx = zeros(n,m); %Initializing matrix for potential gradient change in x and z directions
c = y(1:n); %Initial vector for bulk concentration allocated to y vector
q = reshape(y(n+1:n+n*m),n,m); %Initial vector for solid phase concetration allocated to y vector
DcDz(1) = 0; %Initial bulk concentration differential
DqDx(1,1) = 0; %Initial solid phase concentration differential
J(1,1) = 0; %Initial solid phase flux
DphiDx(1,1) = 0; %Initial potential gradient
for i = 2:n-1
for j = 2:m-1
DcDz(i) = (c(i)-c(i-1))/(z(i)-z(i-1)); %Discretization of bulk concentration with z direction
DqDx(i,j) = (q(i,j)-q(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase concentration with x and z direction
DphiDx(i,j) = Itot/(F*(1-epsilon)*W*L*u*q(i,j)); %Equation for DphiDx
J(i,j) = u*q(i,j)*DphiDx(i,j); %Equation for solid phase flux
DJDx(i,j) = (J(i,j)-J(i,j-1))/(x(j)-x(j-1)); %Discretization of solid phase flux with x and z direction
cstar(i,j) = (q(i,j)*cHstar)/(K*qH); %Equation for cstar
end
end
DcDz(n) = (c(n)-c(n-1))/(z(n)-z(n-1)); %Final bulk concentration differential
DqDx(n,m) = (q(n,m)-q(n,m-1))/(x(m)-x(m-1)); %Final solid phase concentration differential
DJDx(n,m) = (J(n,m)-J(n,m-1))/(x(m)-x(m-1)); %Final solid phase flux
DphiDx(n,m) = Itot/(F*(1-epsilon)*W*L*u*q(n,m)); %Final potential gradient
%% Differential equation with time
DcDt(1) = 0; %Initial bulk concentration differential with time
DqDt(1,1) = 0; %Initial solid phase concentration differential with time
for i = 2:n
for j = 2:m
DqDt(i,j) = a*D*(q(i,j)-(cstar(i,j))) - DJDx(i,j);
DcDt(i) = -v*DcDz(i) + (((1-epsilon)/epsilon)*(DqDt(i,j) + DJDx(i,j)));
end
end
DyDt = [DcDt;reshape(DqDt,n*m,1)]
DJDx
DphiDx
end
Hi,
I changed my code to have q vary only in the x direction because I did not see any variation of q in the z direction with the previous code.
But my problem remains. I have tried to debug it but I just don't understand why DcDz is reasonable in the z direction but DqDx is giving a strange result. My code is given below. I think the problem is in the discretization of DqDx. I have pasted my code below and the graph of DcDz versus z as well as DqDx versus x.
Code:
function main
clear all
%% Defining constants
W = 0.1; %Width of column
L = 1; %Length of column
a = 2; %Surface area to volume ratio
delta = 0.00001; %Film thickness
D = 3*10^-10; %Film diffusion coefficient
v = 0.1; %Velocity of fluid
epsilon = 0.4; %Bed porosity
K = 10; %Equilibrium constant between solid phase and interface
u = 0.01; %Mobility
Itot = 1; %Total current
F = 96485.3329; %Faraday constant
cHstar = 3; %Interfacial concentration of counter ion (hydrogen)
qH = 2; %Solid phase concentration of counter ion (hydrogen)
cFeed = 0.01; %Feed concentration
%% Initializing Vectors for Independent variables
t0 = 0; %Initial time
tf = 10; %Final time
dt = .1; %Time step
t = [0:dt:tf]; %Time vector
z = [0:0.1:L]; %Mesh generation for z direction
x = [0:0.01:W]; %Mesh generation for x direction
n = numel(z); %Number of elements in z
m = numel(x); %Number of elements in x
%% Initialization of Dependent variables
c0 = zeros(n,1) ; %Initial bulk concentration vector
c0(1) = cFeed; %Initial bulk concentration at z = 0
q0 = 0.00001*ones(m,1); %Initial solid phase concentration matrix. Dependent on x and z directions.
J0 = zeros(m,1); %Initial Solid phase flux matrix. Dependent on x and z directions.
y0 = [c0;q0]; %Initial y vector appending together c0 and q0
[t,y] = ode15s(@(t,y)EDIfun3(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed),t,y0);
%Yc = Y(:,[1:n]);
%plot(z,Y(:,[1:11]));
%Yq = Y(:,[n+1:n+n*m]);
%Yqr = reshape(Yq(2,:),n,m);
function DyDt=EDIfun3(t,y,z,x,n,m,W,L,a,delta,D,v,epsilon,K,u,Itot,F,cHstar,qH,cFeed);
%% Variables being allocated zero vectors
c = zeros(n,1); %Bulk concentration vector
q = zeros(m,1); %Solid phase concentration matrix. Dependent on x and z directions.
J = zeros(m,1); %Solid phase flux matrix. Dependent on x and z directions.
cstar = zeros(m,1); %Cstar variable representing
DcDt = zeros(n,1); %Time differential vector of bulk concentration
DqDt = zeros(m,1); %Time differential matrix of solid phase concentration. Dependent on x and z directions.
DyDt = zeros(n+m,1); %Time differential vector of concatenated c and q
%% Discretizing in x and z directions
DcDz = zeros(n,1); %Initial vector for bulk concentration change with z direction
DqDx = zeros(m,1); %Initial matrix for solid phase concentration change in x and z directions
DJDx = zeros(m,1); %Initializing matrix for solid phase flux change in x and z directions
DphiDx = zeros(m,1); %Initializing matrix for potential gradient change in x and z directions
c = y(1:n); %Initial vector for bulk concentration allocated to y vector
q = y(n+1:n+m); %Initial vector for solid phase concetration allocated to y vector
DcDz(1) = 0; %Initial bulk concentration differential
DqDx(1) = 0; %Initial solid phase concentration differential
J(1) = 0; %Initial solid phase flux
DphiDx(1) = 0; %Initial potential gradient
for i = 2:n-1
DcDz(i) = (c(i)-c(i-1))/(z(i)-z(i-1)); %Discretization of bulk concentration with z direction
end
for j = 2:m-1
DqDx(j) = (q(j)-q(j-1))/(x(j)-x(j-1)); %Discretization of solid phase concentration with x and z direction
DphiDx(j) = -Itot/(F*(1-epsilon)*W*L*u*q(j)); %Equation for DphiDx
J(j) = -u*q(j)*DphiDx(j); %Equation for solid phase flux
DJDx(j) = (J(j)-J(j-1))/(x(j)-x(j-1)); %Discretization of solid phase flux with x and z direction
cstar(j) = (q(j)*cHstar)/(K*qH); %Equation for cstar
end
DcDz(n) = (c(n)-c(n-1))/(z(n)-z(n-1)); %Final bulk concentration differential
DqDx(m) = (q(m)-q(m-1))/(x(m)-x(m-1)); %Final solid phase concentration differential
DJDx(m) = (J(m)-J(m-1))/(x(m)-x(m-1)); %Final solid phase flux
DphiDx(m) = -Itot/(F*(1-epsilon)*W*L*u*q(m)); %Final potential gradient
%DphiDx(m) = 0;
%% Differential equation with time
DcDt(1) = 0; %Initial bulk concentration differential with time
DqDt(1) = 0; %Initial solid phase concentration differential with time
for i = 2:n
for j = 2:m
DqDt(j) = a*D*(q(j)-(cstar(j))) - DJDx(j);
DcDt(i) = -v*DcDz(i) + (((1-epsilon)/epsilon)*(DqDt(j) + DJDx(j)));
end
end
DyDt = [DcDt;DqDt]
DJDx
DphiDx
-v*DcDz
((1-epsilon)/epsilon)*(DqDt + DJDx)
cstar
q
DqDx
DqDt
y(:,:)
end
figure;
subplot(3,3,1)
plot(x,y(:,[12:22]));
title('Solid concentration vs x direction')
xlabel('x');
ylabel('Solid phase concentration');
subplot(3,3,2)
plot(z,y(:,[1:11]));
title('Liquid concentration vs z direction')
xlabel('z');
ylabel('Liquid phase concentration');
subplot(3,3,3)
plot(x,DqDx);
title('DqDx vs x direction')
xlabel('x');
ylabel('DqDx');
subplot(3,3,4)
plot(x,DphiDx);
title('DphiDx vs x direction')
xlabel('x');
ylabel('DphiDx');
subplot(3,3,5)
plot(t,y(:,[12:22]));
title('q vs t')
xlabel('t');
ylabel('q');
subplot(3,3,6)
plot(x,J);
title('J vs x direction')
xlabel('x');
ylabel('J');
subplot(3,3,7)
plot(x,DJDx);
title('DJDx vs x direction')
xlabel('x');
ylabel('DJDx');
subplot(3,3,8)
plot(x,cstar);
title('cstar vs x direction')
xlabel('x');
ylabel('cstar');
subplot(3,3,9)
plot(z,DcDz);
title('DcDz vs z direction')
xlabel('z');
ylabel('DcDz');
end
DcDz vs z:
DcDz vs z.PNG
DqDx vs x:
DqDx vs x.PNG

Sign in to comment.

Answers (1)

Did you have any luck solving your problem? I'm very interested in this code. I have been trying to figure out to code this model in Python for a while but I am also not getting very good results.

1 Comment

Hi,
I did not solve this problem on MATLAB. However I believe it can be solved on GAMS software because it solves all equations simultaneously, whereas MATLAB looks at each line one by one. Good luck.
Zain

Sign in to comment.

Asked:

on 9 Jul 2019

Commented:

on 17 Sep 2019

Community Treasure Hunt

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

Start Hunting!