Getting multiple errors on project using functions

조회 수: 3 (최근 30일)
em804
em804 2015년 2월 9일
댓글: dpb 2015년 2월 11일
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