Getting multiple errors on project using functions

3 次查看(过去 30 天)
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 个评论
em804
em804 2015-2-9
The content of the file is simply the code in the question.
dpb
dpb 2015-2-11
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)...

请先登录,再进行评论。

回答(1 个)

Hayat
Hayat 2015-2-9
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.

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by