Finite Difference Explicit Method for Fick's 2nd Law

版本 1.0.0 (2.0 KB) 作者: Roche de Guzman
Finite Difference Explicit Method (iteration) to solve for a PDE (Fick's 2nd Law of diffusion)
457.0 次下载
更新时间 2019/4/26

查看许可证

%% Finite Difference Explicit Method (Iteration) with D = diffusivity: Fick's 2nd Law of Diffusion
% by Prof. Roche C. de Guzman
clear; clc; close('all');
%% Given
xi = 0; xf = 0.6; dx = 0.04; % x range and step size = dx [m]
xL = 0; xU = 0.1; % initial value x lower and upper limits [m]
ti = 0; tf = 0.05; dt = 4e-4; % t range and step size = dt [s]
ci = 2; % initial concentration value [ng/L]
cLU = 8; % initial concentration value within x lower and upper limits [ng/L]
D = 1.5; % diffusivity or diffusion coefficient [m^2/s]
%% Calculations
% Independent variables: x and t
X = xi:dx:xf; nx = numel(X); T = ti:dt:tf; nt = numel(T); % x and t vectors and their number of elements
[x,t] = meshgrid(X,T); x = x'; t = t'; % x and t matrices
% Dependent variable: c
c = ones(nx,nt)*ci; % temporary c(x,t) matrix with rows: c(x) and columns: c(t)
% Initial values and Dirichlet boundary
I = find((X>=xL)&(X<=xU)); % index of lower and upper limits
c(I,1) = cLU; % c at t = 0 for lower and upper limits
% Iteration using Taylor's Finite Difference
for j = 1:nt-1 % t counter
for i = 1:nx-2 % x counter
c(i+1,j+1) = c(i+1,j)+((dt/(dx)^2)*D*(c(i+2,j)-2*c(i+1,j)+c(i,j))); % c(x+dx,t+dt)
% Neumann boundary (zero flux): cx(xi,t)=0 and cx(xf,t)=0 -> c(xi,t)=c(xi+dx,t) and c(xf,t)=c(xf-dx,t)
c(1,j+1) = c(2,j+1); % change c(xi) = c(xi+dx)
c(nx,j+1) = c(nx-1,j+1); % change c(xf) = c(xf-dx)
end
end

引用格式

Roche de Guzman (2024). Finite Difference Explicit Method for Fick's 2nd Law (https://www.mathworks.com/matlabcentral/fileexchange/71361-finite-difference-explicit-method-for-fick-s-2nd-law), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2019a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Boundary Conditions 的更多信息

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.0