Error: Function definitions are not permitted in this context.

11 views (last 30 days)
I am not able to use function as all in my code, I am confused now that is using because I am suing R2015b version? Do I need to buy the toolbox ?

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jun 2019
It is never valid to copy and paste a "function" definition to the command line. It is also not valid to eval() a character vector that contains a "function" definition.
In releases up to R2015a, it was never valid to have a "function" definition inside a script file (a .m file in which the first executable word was not "function" and not "classdef"). In R2015b, it became legal to define functions inside of script files, provided that they were after the script, and provided that every "function" definition ended with a corresponding "end" statement.
Whether in functions or scripts, there are places where it is not valid to have a function definition. For example it is not valid to have a function definition inside a for loop or if statement.
if true
function result = test %invalid function
result = 5;
end
end

More Answers (2)

Nishaben Desai
Nishaben Desai on 19 Jun 2019
Thanks so much Walter, I however have one last question; I am very very new to MATLAB so excuese me if my question might sound u silly. here is what I tried to create a code for an oderinary differential equation, but its showing me the following error.
syms y(t)
ode = (diff(y,t)+y) == 5;%(u=1)
cond = y(0) == 0;
ySol(t) = dsolve(ode,cond)
To use 'syms', you might need:
syms - Symbolic Math Toolbox
Do you think I can run this code without having this toolbox?
Thank you so so much in advance
  2 Comments
Walter Roberson
Walter Roberson on 19 Jun 2019
No, you cannot run that code without having the symbolic toolbox.
You would have to numeric methods, such as using ode45(). That would not, however, give you the equation of the result.

Sign in to comment.


Nishaben Desai
Nishaben Desai on 19 Jun 2019
this is what I am trying to achieve
dy/dt = u - y
with initial condition y(t = 0) = 0
Where t is independent variable (time)
Where y = 0 to 90‘ C
U= mass flow rate (unknown variable)
  2 Comments
Walter Roberson
Walter Roberson on 19 Jun 2019
If U is an unknown variable and dy/dt = u - y and presuming that U and u are the same thing, then you have three values to be concerned with: t, y(t), and u
If u is a constant, then integrating dy/dt = u - y on both sides, we get y = u*t - 1/2*y^2 + C for some boundary condition value C. y(0) = 0 tells us that u*0 + 1/2*0^2 + C = 0 which tells us that C = 0, so y = u*t - 1/2*y^2 which gives us that 1/2*y^2 + y - u*t = 0 which gives us that y = sqrt(2*t*u + 1) - 1 . We know this must stay in the range 0 to 90, and by examination we can see that it is largest when u is largest, so we can solve 90 == sqrt(2*t*u + 1) - 1 which would give us u = 4140/t . But time is potentially unlimited and this is not a constant in time.
From this we conclude that either u is not a constant or else time for the system is not unlimited.
Either way, we do not have enough information to determine u .
Nishaben Desai
Nishaben Desai on 19 Jun 2019
Edited: Nishaben Desai on 19 Jun 2019
Thank you so so much once again. this would help me so much. Actually this is a part of my research where I am developing a mobile game application, and value of "u" would be based on how game user add cyclohexanol to Nitric acid in a Nitration reaction. the moment flow rate u is higher(added cyclohexanol too fast), it would create accident called Runaway reaction and another variable is temperature, y, which impact directly to this accident if it goes above 90' C, so this is what I am trying to simulate here. in MATLAB.
Thanks for your time :)

Sign in to comment.

Tags

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!