Why am I getting error using assignin and syms while solving pde using pdepe
이전 댓글 표시
So I am trying to solve the non dimensionalized form of an equation which describes pore diffusion resistance combined with surface kinetics. So the equation is a partial differential equation and its boundary condition is a system of coupled ODEs. I have attached the file regarding the equation above. Refer to the non dimensional section for the equations. Here is the code that I have written which uses pdepe and dsolve.
function coupled_pde_ode()
% Define parameters
epsilon_p = input('Porosity (epsilon_p): ');
De = input('Effective diffusivity (De, in m^2/s): ');
R = input('Radius of the pellet (R, in m): ');
kf = input('Mass transfer coefficient (kf, in m/s): ');
kR = input('Reaction rate constant (kR, in 1/s): ');
C0 = input('Initial concentration in pellet pores (C0, arbitrary units): ');
mp = input('Weight of catalyst beads (mp, in kg): ');
rho_p = input('Overall density of catalyst pellets (rho_p, in kg/m^3): ');
V = input('Volume of reactor (V, in m^3): ');
% Derived non-dimensional parameters
phi = (R / 3) * sqrt(kR / De); % Thiele modulus
Bi = kf * R / De; % Biot number
alpha = (3 * epsilon_p * mp) / (V * rho_p); % Coupling parameter
tau_end = 10; % End time in dimensionless units
% Non-dimensionalized spatial and temporal discretization
rbar = linspace(0, 1, 50); % Non-dimensional radius (0 <= rbar <= 1)
tau = linspace(0, tau_end, 100); % Non-dimensional time
% Solve PDE for Cpbar using pdepe
m = 2; % Spherical symmetry
sol_pde = pdepe(m, ...
@(rbar, tau, Cpbar, dCpbar_drbar) pde_eqn(rbar, tau, Cpbar, dCpbar_drbar, epsilon_p, phi), ...
@initial_condition, ...
@(xl, CpbarL, xr, CpbarR, tau) boundary_conditions(xl, CpbarL, xr, CpbarR, tau, Bi, phi), ...
rbar, tau);
% Extract Cpbar at rbar = 1
Cpbar_surface = sol_pde(:, end); % Concentration at rbar = 1 over time
% Solve coupled ODE for Cbbar using dsolve
syms Cbbar(tau)
dCbbar_dtau = -alpha * (Bi * (Cbbar - Cpbar_surface') - 3 * phi^2 * Cpbar_surface');
Cbbar_sol = dsolve(dCbbar_dtau, Cbbar(0) == 1);
% Evaluate Cbbar over time (not plotted)
tau_values = tau; % Time grid
Cbbar_values = double(subs(Cbbar_sol, tau, tau_values));
% Plot results: Concentration vs. Radius
figure;
for i = 1:10:length(tau)
plot(rbar, sol_pde(i, :), 'DisplayName', sprintf('\\tau = %.1f', tau(i)));
hold on;
end
xlabel('\bar{r} (Dimensionless Radius)');
ylabel('\bar{C}_p (Dimensionless Pore Concentration)');
legend;
title('Pore Concentration Profiles (\bar{C}_p) vs. Radius');
grid on;
hold off;
% Nested functions
function [c, f, s] = pde_eqn(rbar, tau, Cpbar, dCpbar_drbar, epsilon_p, phi)
% Non-dimensional PDE coefficients
c = epsilon_p; % Time-dependent term coefficient
f = dCpbar_drbar; % Diffusion term coefficient
s = -9 * phi^2 * Cpbar; % Source term (reaction rate)
end
function Cpbar0 = initial_condition(rbar)
% Non-dimensional initial condition
Cpbar0 = ones(size(rbar)); % Initial concentration equals C0 (normalized to 1)
end
function [pl, ql, pr, qr] = boundary_conditions(xl, CpbarL, xr, CpbarR, tau, Bi, phi)
% Boundary conditions for the non-dimensional problem
% At rbar = 0 (center of the sphere), Neumann BC: dCpbar/drbar = 0
pl = 0;
ql = 1;
% At rbar = 1 (outer surface of the sphere), flux term
pr = Bi * (1 - CpbarR) - 3 * phi^2 * CpbarR;
qr = 1;
end
end
Here are the values I entered
Porosity (epsilon_p):
0.5
Effective diffusivity (De, in m^2/s):
1.5e-10
Radius of the pellet (R, in m):
1e-3
Mass transfer coefficient (kf, in m/s):
2.8e-3
Reaction rate constant (kR, in 1/s):
5e-12
Initial concentration in pellet pores (C0, arbitrary units):
1
Weight of catalyst beads (mp, in kg):
0.1e-3
Overall density of catalyst pellets (rho_p, in kg/m^3):
850
Volume of reactor (V, in m^3):
0.1
Here is the error message I'm receiving
Error using assignin
Attempt to add "Cbbar" to a static workspace.
Error in syms (line 331)
assignin('caller', name, xsym);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in Testing (line 35)
syms Cbbar(tau)
채택된 답변
추가 답변 (1개)
tau is a vector of values in your code:
tau = linspace(0, tau_end, 100); % Non-dimensional time
You can't use it as a symbolic variable afterwards without redefining it as symbolic:
syms tau Cbbar(tau)
But note that you now have overwritten the original tau.
Further, replace
dCbbar_dtau = -alpha * (Bi * (Cbbar - Cpbar_surface') - 3 * phi^2 * Cpbar_surface');
by
dCbbar_dtau = diff(Cbbar,tau) == -alpha * (Bi * (Cbbar - Cpbar_surface') - 3 * phi^2 * Cpbar_surface');
to define your differential equation.
But this will only work for Cpbar_surface being a single value, not a vector of values that depend on time.
Thus you won't succeed to integrate
dCbbar_dtau = -alpha * (Bi * (Cbbar - Cpbar_surface') - 3 * phi^2 * Cpbar_surface');
with a time-dependent function Cpbar_surface(t) (in this case a vector of values) symbolically.
Use pde1dm if you want to solve a PDE with a coupled ODE:
Or discretize your PDE in space, add the ODE and solve the complete system of ordinary differential equations using "ode15s". Look up "method-of-lines" for more details.
카테고리
도움말 센터 및 File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

