Getting multiple errors on project using functions

2 views (last 30 days)
I am trying to run the following code:
function y = epsilon(x)
a = 1;
b = 1;
c = 1;
y = ((a*b/exp(b*x))-(a*c))/-b;
And I get different error messages each time I try to fix something. This code is in an m-file saved as epsilon.m, so I'm really confused. I either get an error about not being allowed to define functions or an error about not enough input arguments. Help, please? I am even running the code my teacher gave us in class and it's giving the error about not being allowed to define functions in this context.
  3 Comments
em804
em804 on 9 Feb 2015
The content of the file is simply the code in the question.
dpb
dpb on 11 Feb 2015
The error is the one when you try to create a function in a running environment like the workspace--
>> function y=epsilon(x)
function y=epsilon(x)
|
Error: Function definitions are not permitted in this context.
>>
So, the answer of simply assuring us you think that the file content is ok isn't sufficient; if it were and nothing else in context is wrong you'd not receive the error.
Aagin, SHOW us exactly the results of the previously requested information and cut and paste the EXACT commands in context of where you do them (or describe precisely how you're using the IDE if not typing in the command line)...

Sign in to comment.

Answers (1)

Hayat
Hayat on 9 Feb 2015
Are you just clicking "Run" along the top of the MATLAB toolbar without calling up the function for a specific x value? If so, that's your problem. You have to actually define x in a script, or else MATLAB does not know what "x" you are trying to solve for. Try opening up a new script file in the same folder as epsilon.m, and type the following:
g = 5; h = epsilon(x)
This means that you are defining g as 5, and you are plugging in "5" into the "x" of your eplison.m function in order to get an output answer of "h". The "h" in your script corresponds to the "y" of your function, and the "g" of your script corresponds to the "x" of your function. Actually, you can use any letters that you want.

Community Treasure Hunt

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

Start Hunting!