How to include average of a variable in the equation setup of PDEPE
3 次查看(过去 30 天)
显示 更早的评论
Hi everyone!
I am using PDEPE to solve a set of partial differential equations over space (x) and time. My equation is , where is the average of all the values of G from 0 to x. G is also a variable in this set of partial differential equations. How can I add this G_avg function?
Edit: I wont be able to do it this way - One way I know is to run the solver from [t,t+Δt], obtain G(x) for that value of time, feed back the G_avg and then run the solver again. Can I do it without this method?
Thanks!
0 个评论
回答(1 个)
Nipun
2024-6-6
Hi Jairaj,
I understand that you want to solve a PDE using "pdepe" in MATLAB where the equation involves an average function "G_avg". Here's a possible approach to include "G_avg" without iterating through time steps manually:
function sol = solvePDE()
% Define the PDE coefficients
m = 0; % Symmetry parameter for pdepe (0 for slab, 1 for cylindrical, 2 for spherical)
% Time and space discretization
x = linspace(0, 1, 100); % Spatial domain
t = linspace(0, 10, 50); % Time domain
% Solve the PDE
sol = pdepe(m, @pdefun, @icfun, @bcfun, x, t);
% Plot results
surf(x, t, sol(:,:,1));
title('Solution of the PDE');
xlabel('Space x');
ylabel('Time t');
zlabel('F(x,t)');
end
function [c, f, s] = pdefun(x, t, u, dudx)
% Calculate G_avg(x)
global G_values G_avg C;
G_avg = trapz(G_values(1:find(x==G_values,1)))/x; % Approximate integral
% Define PDE
c = 1;
f = dudx;
s = G_avg - C;
end
function u0 = icfun(x)
% Initial condition
u0 = 0;
end
function [pl, ql, pr, qr] = bcfun(xl, ul, xr, ur, t)
% Boundary conditions
pl = ul;
ql = 0;
pr = ur - 1;
qr = 0;
end
In this approach, "G_avg" is calculated within the "pdefun" function. This allows you to avoid iterating through time steps manually. Ensure you define "G_values" and "C" in your workspace before running the solver.
Hope this helps.
Regards,
Nipun
1 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 PDE Solvers 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!