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

조회 수: 2 (최근 30일)
mark
mark 2013년 10월 19일
댓글: Daniel Shub 2013년 10월 22일
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!

채택된 답변

mark
mark 2013년 10월 19일
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.

추가 답변 (2개)

Daniel Shub
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
mark
mark 2013년 10월 19일
편집: mark 2013년 10월 19일
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 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.

댓글을 달려면 로그인하십시오.


mark
mark 2013년 10월 19일
*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.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by