Need help: Script working in 2012b but not 2013b???
显示 更早的评论
Been working my way through a matlab tutorial. The scripts in the tutorial all have a similar layout:
if true
function solve_bernoulli
% Function to solve dy/dt = (t*y^4+2y)/6
% from t=tstart to t=tstop, with y(0) = initial_y
% Define parameter values below.
tstart = 0;
tstop = 20;
initial_y = -2;
[t_values,sol_values] = ode45(@diff_eq,[tstart,tstop],initial_y);
plot(t_values,sol_values);
function dydt = diff_eq(t,y) % Function defining the ODE
dydt = (t*y^4+2*y)/6;
end
end
end
The layout is as below:
- function function_name
- %stuff in the function
- %May even include an actual embeded function (as the example above has)
- end
Every time I run it I get the error:
"The selected section cannot be evaluated because it contains an invalid statement"
and in the command window:
" function solve_bernoulli | Error: Function definitions are not permitted in this context."
HOWEVER , this script works in the tutorial writings computer using 2012b. So how can this not work on my 2013b version???
Need help as this is driving me nuts!
采纳的回答
更多回答(2 个)
Daniel Shub
2013-10-19
The above code will not work in ANY version of MATLAB because
if true
function solve_bernoulli
...
is not valid. The FUNCTION keyword can only be used in an m file. Further, the first line of code in the m file must begin with the FUNCTION keyword. Despite your claims, removing
function solve_bernoulli
and the corresponding end will not solve the problem since it will leave
function dydt = diff_eq(t,y)
in the middle of the m file, which again is not valid (or you are leaving out part of the code).
2 个评论
Daniel Shub
2013-10-22
No it doesn't ...
>> temp
Error: File: temp.m Line: 3 Column: 5
Function definitions are not permitted in this context.
MATLAB has never permitted code like this.
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!