Error when inputting function.

Hi, I can't figure out why I am getting the error. Here is the function.
function y=nest(d,c,x,b)
|
Error: Function definitions are not permitted in this context.
And this is the code I am trying to perform in matlab.
%Program 0.1 Nested multiplication %Evaluates polynomial from nested form using Horner’s Method
%Input: degree d of polynomial,
% array of d+1 coefficients c (constant term first),
% % x-coordinate x at which to evaluate, and array of d base points b, if needed
%Output: value y of polynomial at x
function y=nest(d,c,x,b)
if nargin<4,
b=zeros(d,1);
end
y=c(d+1);
for i=d:-1:1
y = y.*(x-b(i))+c(i);
end
RunningthisM atlab fun
Page(s): 3, Numerical Analysis, 2/e by Timothy Sauer, Pearson Education NOOK Study ( , truxton@udel.edu). This material is protected by copyright.

 채택된 답변

Jos (10584)
Jos (10584) 2013년 2월 11일

0 개 추천

I assume you did put that code in the editor and saved it as an m-file which you called from the command line (or from a script) and then got the error?
Is the second line ("Horner's method") on a separate line without a comment symbol ("%") in front of it? If so, that is causing the error.

댓글 수: 3

Truxton
Truxton 2013년 2월 11일
Hi Jos, What is "the editor"...And I have not been able to find a "save as" option, only "save as workspace". I did have the % symbol infront of the description of the code. When i copy and pasted the code it didn't come out so well in the initial post. So, if I can figure out exactly what the "editor" is, then figure out how to "save as .m"...then im good to go. I have the R2012a student version.
The editor is the program that you can use in matlab to edit m-files. At the command prompt, type:
>> edit
Truxton
Truxton 2013년 2월 11일
Cool! So I just put the function into the editor, then call it in the command window. awesome. Also, from the editor window I found the save as from the file menu. You helped me out soooo much! Thanks. Beleive it or not, im not a total noob with programming...ive taken a class or two, just its been a while.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 10일
편집: Azzi Abdelmalek 2013년 2월 10일

0 개 추천

Because you did not call your function, you run it as a m-file. Try this
d=5;
c=1:10;
x=100:200;
out=nest(d,c,x)

댓글 수: 11

Truxton
Truxton 2013년 2월 10일
Do you mean I should define the variables in matlab before defining the function?
Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 10일
편집: Azzi Abdelmalek 2013년 2월 10일
I mean, you can't run a function file as a script file. Save your code
function y=nest(d,c,x,b)
if nargin<4,
b=zeros(d,1);
end
y=c(d+1);
for i=d:-1:1
y = y.*(x-b(i))+c(i);
end
as nest.m
Then call your function in Matlab command or from a script file (m-file) like I posted it in my answer
Truxton
Truxton 2013년 2월 10일
o.k. I tried to save the code by clicking file, "save workspace as". then I typed nest.m and the same error happened. Am I suppose to save it differently?
Walter Roberson
Walter Roberson 2013년 2월 10일
Do not save the workspace, save the code, from the editor.
Also please check all the lines before the "function" line. The "function" line must be the first non-blank non-comment line. If that word "needed" appears on a line of it own, you would have difficulty.
Truxton
Truxton 2013년 2월 10일
o.k. cool. How do I "save the code, from the editor"?
Don't type "save workspace as", just click save your file as nest.m. Then don not type nest.m but:
d=5;
c=1:10;
x=100:200;
out=nest(d,c,x)
Truxton, I think you should read the basics of Matlab.
Truxton
Truxton 2013년 2월 10일
o.k. I will do that for sure. Where do you see "save file"? can't find it. Thanks
Truxton
Truxton 2013년 2월 10일
I am looking into the help files for this, and its not helping. It says, to " on the editor tab, in the file selection, click save". But what is "the editor tab"? Not so clear.
Image Analyst
Image Analyst 2013년 2월 11일
You may have an old version - before R2012b with the tabbed ribbon interface. Look for "Save as..." under the File pulldown menu.
Truxton
Truxton 2013년 2월 11일
Yes, I have R2012a....I looked in the file, but there isn't a "save as" option.
Truxton
Truxton 2013년 2월 11일
it's also the student version.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

질문:

2013년 2월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by