Handles and Function outputs?
Show older comments
Hi
I am having difficulty understanding the following code:
function parabolic
global rho cp k
global q
L=0.1 ; %m
k=200 ; %W/m-K
rho=10000; %kg/m^3
cp=500 ; %J/kg-K
q=1e6 ; %W/m^2
tend=10; %seconds
m = 0;
x = linspace(0,L,200);
t = linspace(0,tend,50)
sol = pdepe(m,@pdex1pde,@pdex1ic,@pdex1bc,x,t)
function [c,f,s] = pdex1pde(x,t,u,DuDx)
global rho cp k
c = rho*cp;
f = k*DuDx;
s = 0;
% --------------------------------------------------------------
function u0 = pdex1ic(x)
u0 = 0;
% --------------------------------------------------------------
function [pl,ql,pr,qr] = pdex1bc(xl,ul,xr,ur,t)
global q
pl = q; %these two set k*dT/dx-q=0 on right side
ql = 1;
pr = ur;
qr = 0; %sets right side temperature to 0
specifically i do not understand what happens at line 14, and then in the other functions as the outputs are defined but there do not appear to be any inputs??? I don't really get handles at the moment..... any help will prevent me from probably going crazy!
2 Comments
Bardakova Regina
on 8 Jan 2022
clear all;
global a
m=0;
a=10;
x=linspace(0,1,20);
t=linspace(0,2,5);
sol=pdepe(m,@pdex1pde,@pdex1ic,@pdex1bc,x,t);
u=sol(:,:,1);
[nr nc]=size(u);
figure(1)
surf(x,t,u)
title('Numerical solution computed with 20 mesh points')
xlabel('координата x')
xlabel('время t')
figure(2)
plot(x,u(1,:),x,u(3,:),x,u(nr,:));
title('u в трех сечениях по времени');
xlabel('координата x')
xlabel('u(x)');
grid on
%----------------------------------------------
function [c,f,s]=pdex1pde(x,t,u,DuDx)
global a
c=1/a^2;
f=DuDx;
s=0;
end
%----------------------------------------------
function u0=pdex1ic(x)
u0=sin(pi*x);
end
%----------------------------------------------
function [pl,ql,pr,qr]=pdex1bc(xl,ul,xr,ur,t)
global a
pl=ul;
ql=0;
pr=pi*exp(t);
qr=1;
end
function [c,f,s]=pdex1pde(x,t,u,DuDx)
↑
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.
Help
Walter Roberson
on 9 Jan 2022
You are using an older version of MATLAB that did not permit functions to be defined inside script files. You will need to either convert your script to a function, or else store those functions in separate files.
Answers (2)
Sean de Wolski
on 24 Feb 2012
Internally pdepe() uses the handles of those functions (with the @) to call them with inputs it determines it needs. It changes these inputs as necessary to solve the PDE. pdepe() uses the outputs of those functions internally. This is explained in:
doc pdepe
More on function handles:
doc function_handle
1 Comment
Martin Caton
on 27 Feb 2012
Andrew Newell
on 24 Feb 2012
This code is solving rho*Cp*(du/dt) = d/dx(k du/dx) (a heat transport equation). The terms c, f, s returned by pdex1pde could depend on x, t, u, but they don't. In recent versions of MATLAB you could replace the first function statement by
function [c,f,s] = pdex1pde(~,~,~,DuDx)
to make clear that pdex1pde depends only on DuDx (and then only for the output f).
Note that you could define c = rho*Cp/k and have just one constant. You could also turn the subfunctions into nested functions to get rid of all the global statements:
function [x,t,sol] = parabolic
L=0.1 ; %m
k=200 ; %W/m-K
rho=10000; %kg/m^3
cp=500 ; %J/kg-K
q=1e6 ; %W/m^2
tend=10; %seconds
m = 0;
x = linspace(0,L,200);
t = linspace(0,tend,50);
sol = pdepe(m,@pdex1pde,@pdex1ic,@pdex1bc,x,t);
function [c,f,s] = pdex1pde(~,~,~,DuDx)
c = rho*cp/k;
f = DuDx;
s = 0;
end
function u0 = pdex1ic(~)
u0 = 0;
end
function [pl,ql,ur,qr] = pdex1bc(~,~,~,ur,~)
pl = q; %these two set k*dT/dx-q=0 on right side
ql = 1;
qr = 0; %sets right side temperature to 0
end
end
(Edited for clarity.)
Categories
Find more on Eigenvalue Problems 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!