Unsuccessful use of anonymous function for time-varying boundary condition
5 次查看(过去 30 天)
显示 更早的评论
I have asked previously about time varying boundary conditions. Advice offered suggested that I should recast my heat transfer problem to a general PDE equation. I have now done that and am trying to apply an anonymous function approach to a time varying boundary condition as suggested in the MatLab PDE Toolbox User’s Guide. As a simple example, I have defined a problem that is basically transient cryogenic cooling on one surface of a 1D slab with a known time-varying refrigeration temperature. I include the code below. MatLab says that the anonymous function for the boundary condition that I tried to define [bcfuncN] does not exist. What am I doing wrong?
Error using pde.BoundaryCondition (line 93. The value of 'g' is invalid. Function bcfuncN does not exist.
Another issue is that for a transient anonymous boundary condition function, I presume that I should be using state.time for the time variable. To define time limits of interest should I include code something like the following:
state.time= 0:1:100
Thanks
%% Create transient thermal model
thermalmodel = createpde(1);
R1= [3,4,0,4,4,0,0,0,1,1]';
gd= [R1];
sf= 'R1';
ns = char('R1');
ns = ns';
dl = decsg(gd,sf,ns);
%% Create & plot geometry
geometryFromEdges(thermalmodel,dl);
pdegplot(thermalmodel,"EdgeLabels","on","FaceLabels","on")
xlim([-1 5])
ylim([0 1])
% axis equal
%% Generate and plot mesh
generateMesh(thermalmodel)
figure
pdemesh(thermalmodel)
title("Mesh with Quadratic Triangular Elements")
%% Apply BCs
% Edge 4 is left edge; Edge 2 is right side
hrefrig= 262
f1= 3.843e-8;
f2= -3.482e-5;
f3= 1.179e-2;
f4= -1.95;
f5= 23.69;
bcfuncN = @(location,state)hrefrig*(f1*state.time.^4 + f2*state.time.^3 + f3*state.time.^2 + f4*state.time + f5); % Anonymous function
%% Define time limits
%tlist= 0: 1: 100;
state.time= 0:1:100;
applyBoundaryCondition(thermalmodel,"neumann", ...
Edge=4,g=@bcfuncN);
applyBoundaryCondition(thermalmodel, "dirichlet",Edge=[2],u=20);
%% Apply thermal properties [copper]
rho= 8933 %
cp= 385 %
rhocp= rho*cp %
k= 401 % W/mK
alpha= k/rhocp
%% Define uniform volumetric heat generation rate
Qgen= 0 % W/m3 [1e12]
%% Define coefficients for generic Governing Equation to be solved
m= 0
a= 0
d= rhocp
c= [k]
f= [Qgen]
specifyCoefficients(thermalmodel, m=0, d=rhocp, c=k, a=0, f=f);
%% Apply initial condition
setInitialConditions(thermalmodel, 20);
thermalresults= solvepde(thermalmodel, tlist);
% Plot results
% Plot temperature profile along a defined line
plotAlongLine(thermalresults.Mesh.Nodes, thermalresults.NodalSolution(:,100), [0.0,0.5], [1,0.5], 100);
function plotAlongLine(p, u, xy1, xy2, numpts)
x = linspace(xy1(1),xy2(1),numpts);
y = linspace(xy1(2),xy2(2),numpts);
F = TriScatteredInterp(p(1,:)', p(2,:)', u);
uxy = F(x,y);
figure; plot(x, uxy);
end
ylim([20 50])
0 个评论
采纳的回答
Steven Lord
2025-5-10
When you run this line of code (commented out so I can run one line of code later in this answer):
%{
applyBoundaryCondition(thermalmodel,"neumann", ...
Edge=4,g=@bcfuncN);
%}
bcfuncN is a variable that contains a function handle (an anonymous function is a function handle as you can see from the output of whos.)
fh = @(x) x+1;
whos fh
Your code attempts to create a function handle to a function named bcfuncN and pass it into the function. That's not what you want; you want to pass the already existing function handle in like:
%{
applyBoundaryCondition(thermalmodel,"neumann", ...
Edge=4,g=bcfuncN);
%}
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!