how to define a function

3 views (last 30 days)
Charles Kinoshita
Charles Kinoshita on 20 Jul 2017
Edited: Walter Roberson on 21 Jul 2017
why do I have error message when I type
function dy = ff(t,y)
  2 Comments
Charles Kinoshita
Charles Kinoshita on 21 Jul 2017
Edited: Stephen23 on 21 Jul 2017
but shouldn't I be able to type
>> clear
>> function yprime = bvexample(t,y)
function yprime = bvexample(t,y)
Error: Function definitions are not permitted in this context.
Steven Lord
Steven Lord on 21 Jul 2017
No. You cannot define functions by typing the text of their body at the Command Prompt. You need to write them as a function file. [If your function can be written as one statement, you could create an anonymous function handle.]

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 20 Jul 2017
Edited: James Tursa on 20 Jul 2017
If you are getting this message:
Error: Function definitions are not permitted in this context.
It means that you cannot define a function in that part of the code. E.g., you cannot define functions at the end of script files in older versions of MATLAB. So either turn your script file into a function file itself, or copy your function code to a new file (with the name ff.m).
But this is all just a guess, since you haven't told us what the error message is or shown us any code.

Tags

Products

Community Treasure Hunt

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

Start Hunting!