How to solve this code error? (Diffusion_equation with pedpe)
4 views (last 30 days)
Show older comments
% define the parameters
D1=1;
D2=2;
a = 10;
%define spatial mesh and time span
x = linspace(0, a, 100);
t = linspace(0, 10, 100);
%use pdepe solver
sol = pdepe(0, @diffusion_equation, @diffusion_ini, @diffusion_boun, x, t);
% Extract the solution
u = sol(:,:,1);
%plot the results
figure;
surf(x,t, u);
xlabel('spatial coordinate (x)');
ylabel('Time(t)');
zlabel('Transport property profile (u)');
title('1D diffusion model of the T cell density');
function [c, f, s] = diffusion_equation(x, t, u, DuDx)
D = zeros(size(x)); % Initialize D as a vector of zeros
D(x <= 2/a) = D1; % Set D1 where the condition is true
D(x > 2/a) = D2; % Set D2 where the condition is false
c = 1;
f = D .* DuDx;
s = 0;
end
%define the initial condition
function u0 = diffusion_ini(x)
u0 = 10^3 * ones(size(x));
end
%define the boundary condition
function [pl,ql,pr,qr] = diffusion_boun(xl,ul,xr,ur,t)
pl = ul;
ql = 0;
pr = ur;
qr = 0;
end
this code is the diffusion equation but there is a condition of x
but there is code error. so i cannot run this code yet.
please help me
3 Comments
Dyuman Joshi
on 7 Jan 2024
"At the top of the code, we've arbitrarily substituted the values 1 and 2."
Okay, yes. I seem to have overlooked those definitions.
Accepted Answer
Dyuman Joshi
on 7 Jan 2024
Modify the function to provide D1, D2 and a as inputs and update the pdepe() call accordingly as well -
% define the parameters
D1=1;
D2=2;
a = 10;
%define spatial mesh and time span
x = linspace(0, a, 100);
t = linspace(0, 10, 100);
%use pdepe solver
%% Update the call to pdepde
sol = pdepe(0, @(x, t, u, DuDx) diffusion_equation(x, t, u, DuDx, D1, D2, a), ...
@diffusion_ini, @diffusion_boun, x, t);
% Extract the solution
u = sol(:,:,1);
%plot the results
figure;
surf(x,t, u);
xlabel('spatial coordinate (x)');
ylabel('Time(t)');
zlabel('Transport property profile (u)');
title('1D diffusion model of the T cell density');
%% Provide inputs vv vv v
function [c, f, s] = diffusion_equation(x, t, u, DuDx, D1, D2, a)
D = zeros(size(x)); % Initialize D as a vector of zeros
D(x <= 2/a) = D1; % Set D1 where the condition is true
D(x > 2/a) = D2; % Set D2 where the condition is false
c = 1;
f = D .* DuDx;
s = 0;
end
%define the initial condition
function u0 = diffusion_ini(x)
u0 = 10^3 * ones(size(x));
end
%define the boundary condition
function [pl,ql,pr,qr] = diffusion_boun(xl,ul,xr,ur,t)
pl = ul;
ql = 0;
pr = ur;
qr = 0;
end
More Answers (0)
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!