Error: Function definitions are not permitted in this context.

I am using pdepe example given in your page https://www.mathworks.com/help/matlab/ref/pdepe.html
On executing the code, following error message appears, even though the output is obtained.
function [c,f,s] = pdex1pde(x,t,u,DuDx)
Error: Function definitions are not permitted in this context.
MATLAB Version: 9.3.0.948333 (R2017b) Update 9
MATLAB License Number: 1110***
Operating System: Mac OS X Version: 10.13.6 Build: 17G65
Java Version: Java 1.8.0_144-b01 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
=========================================
m = 0;
x = linspace(0,1,20);
t = linspace(0,2,5);
sol = pdepe(m,@pdex1pde,@pdex1ic,@pdex1bc,x,t);
% Extract the first solution component as u.
u = sol(:,:,1);
% A surface plot is often a good way to study a solution.
surf(x,t,u)
title('Numerical solution computed with 20 mesh points.')
xlabel('Distance x')
ylabel('Time t')
% A solution profile can also be illuminating.
figure
plot(x,u(end,:))
title('Solution at t = 2')
xlabel('Distance x')
ylabel('u(x,2)')
% --------------------------------------------------------------
function [c,f,s] = pdex1pde(x,t,u,DuDx)
c = pi^2;
f = DuDx;
s = 0;
% --------------------------------------------------------------
function u0 = pdex1ic(x)
u0 = sin(pi*x);
% --------------------------------------------------------------
function [pl,ql,pr,qr] = pdex1bc(xl,ul,xr,ur,t)
pl = ul;
ql = 0;
pr = pi * exp(-t);
qr = 1;

1 个评论

Exactly HOW did you try to execute it? You didn't just click the green run triangle without passing in agruments did you?

请先登录,再进行评论。

回答(2 个)

madhan ravi
madhan ravi 2018-11-13
编辑:madhan ravi 2018-11-14
You can‘t run a code which has function in command window. See https://www.mathworks.com/help/matlab/matlab_prog/calling-functions.html to avoid errors while calling a function.

1 个评论

in the latter versions you can even run the function in the command window but you need to pass arguments before calling a function.

请先登录,再进行评论。

Thank you for the clue, I am able to understand now.
The functions need to be in the script file and called into the command window, for execution.

类别

帮助中心File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by