Filter löschen
Filter löschen

Solving a system of nonlinear, discretized partial derivatives (mass balances) using FSOLVE

5 Ansichten (letzte 30 Tage)
Dear MATLAB community,
I have a few questions regarding the topic that I hope some of you could help me on. The problem to solve is as follows:
For my research, I want to numerically analyze a system of stationary, 2D mass balances (electrochemistry) which is made up of partial derivatives and boundary conditions. I have by now found no built-in MATLAB commands are suitable (the format of the equations are not suitable for the PDE-tool, and pdepe is for transient systems only). Now I intend to discretize all equations using the command gradient, and solve the resulting system of algebraic, nonlinear equations using FSOLVE. Overall, 12 unknown variables (arrays) are to be solved by a system of 21 equations & constraints. I have embedded the relevant pieces of code.
My questions are:
- Whenever I want to run the m.file for testing, the following error shows up: Undefined function or variable 'x'. Why? I've seen numerous examples on MATLAB where multiple variables are defined as a = x(1), b = x(2) after which they are used in an equation F. This is the code:
function EQNS = nernst_planck_ae(x)
% Define independent variables in terms of x
c = x(1);
d = x(2);
cTm = x(3);
etc. until x(12).
- In essence, I want the solver to give arrays as an output (the variable concentration along the x-axis amongst others). However, when giving the equations, which are also array valued, the following error introduces itself: In an assignment A(:) = B, the number of elements in A and B must be the same. I understand this, but how can I work around this? The relevant code of line is:
EQNS(4) = - J_ch - D_m * cTm(end) .* gradient(phi_iem)
- Although the command gradient allows me to take the central difference of the derivative for any array/matrix, I also want to implement some boundary constraints for the solver. More specifically, I want for f(x), df/fx at x = 0 to be 0. How do I implement such a constraint in the equations? Is the following line of code in the correct format?:
% Neumman conditions for the dcdx & J_ions
dcdx = gradient(c);
dddx = gradient(d);
EQNS(10) = dcdx(1) == 0;
EQNS(11) = dcdx(end) == -J_ions/(2*D_s);
EQNS(12) = dddx(1) == 0;
EQNS(13) = dddx(end) == -J_ions/(2*D_s);
The EQNS are objective functions for fsolve.
I realize this is quite some text, though I tried to ask as concise as possible without letting out relevant information. I can upload the entire script if that helps. Also, I can reference the academic paper if needed.
In any case, thanks a lot for the effort in advance,
Jair

Antworten (1)

Torsten
Torsten am 11 Apr. 2017
First question:
From what you post we can not tell the reason for the error message. Maybe your call to "fsolve" will tell us more.
Second question:
EQN(4) is a scalar whereas - J_ch - D_m * cTm(end) .* gradient(phi_iem) seems to be a vector. So setting them equal is not possible.
You will have to reserve one equation for each component of the vector.
Third question:
EQNS(10) = dcdx(1);
EQNS(11) = dcdx(end) - (-J_ions/(2*D_s));
EQNS(12) = dddx(1) ;
EQNS(13) = dddx(end) - (-J_ions/(2*D_s));
Best wishes
Torsten.
  6 Kommentare
Torsten
Torsten am 12 Apr. 2017
count1 = length(array1)
for i=1:count1
EQNS(8+i)=array1(i);
end
count2 = length(array2)
for i=1:count2
EQNS(8+count1+i)=array2(i)
end
...
Best wishes
Torsten.
Jair Dan
Jair Dan am 13 Apr. 2017
Thanks that helped a lot, I've got the system running (at least). Now I just have to keep finetuning it.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by