Ran script given to me. Error message: Function definitions are not permitted in this context.

These were the directions: For use in the next few problems, we will need the mle simpvec. So write the following script and save it as simpvec.m.
This is the script that I put into matlab:
>> function s=simpvec(n)
s=2*ones(1,n+1);
s(2:2:n)=4*ones(1,n/2);
s(1)=1; s(n+1)=1
??? function s=simpvec(n)
|
Error: Function definitions are not permitted in this context.
Please help!

回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2011 年 12 月 2 日
You need to save your code into a file called simpvec.m
function s=simpvec(n)
s=2*ones(1,n+1);
s(2:2:n)=4*ones(1,n/2);
s(1)=1; s(n+1)=1
Then run it in the Command Window, for example
s=simpvec(5)
Sean de Wolski
Sean de Wolski 2011 年 12 月 2 日
You're trying to define the function simpvec at the command line (at least that's what it looks like). This isn't allowed. The function has tro be in its own file.
At the command line run:
edit simpvec
It'll prompt you to open a new editor window. Copy and paste what you have above and save it. Then call:
n = pi; %or whatever it is
s = simpvec(n);
at the command line.
doc function
for more info. Good Luck!

カテゴリ

ヘルプ センター および File ExchangeSimulink についてさらに検索

質問済み:

2011 年 12 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by