Need help: Script working in 2012b but not 2013b???

1 view (last 30 days)
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!

Accepted Answer

mark
mark on 19 Oct 2013
Okay, I found out why, and it revolves around me being a lazy tw@;
I don't normally " save " scripts — instead I choose to use " Run and Advance " as it generally runs scripts fine, allowing you to test them out before you save it. It seems that, although I was saving the function first, using " Run and Advance " on a function doesn't work (at least a function in the form above). You actually need to use the more general, and obvious, " Run ".
I feel like an idiot but I'm glad I found it out instead of giving up on it.

More Answers (2)

Daniel Shub
Daniel Shub on 19 Oct 2013
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 Comments
mark
mark on 19 Oct 2013
Edited: mark on 19 Oct 2013
I'm sorry but it works.
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
Copy and paste that into a script. Save it. Then click the green play button.
My issue was that I wasn't playing the script properly. I don't quite know why the guy writes scripts in this way, but he does.
Daniel Shub
Daniel Shub on 22 Oct 2013
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.

Sign in to comment.


mark
mark on 19 Oct 2013
*writers computer
I should probably add that I can get the script to work if I remove the "function function_name" part and reorganise the script. But, I don't understand how it doesn't work on my computer.

Categories

Find more on Programming 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!