Effacer les filtres
Effacer les filtres

Error when inputting function.

2 vues (au cours des 30 derniers jours)
Truxton
Truxton le 10 Fév 2013
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.

Réponse acceptée

Jos (10584)
Jos (10584) le 11 Fév 2013
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 commentaires
Jos (10584)
Jos (10584) le 11 Fév 2013
The editor is the program that you can use in matlab to edit m-files. At the command prompt, type:
>> edit
Truxton
Truxton le 11 Fév 2013
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 10 Fév 2013
Modifié(e) : Azzi Abdelmalek le 10 Fév 2013
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 commentaires
Truxton
Truxton le 11 Fév 2013
Yes, I have R2012a....I looked in the file, but there isn't a "save as" option.
Truxton
Truxton le 11 Fév 2013
it's also the student version.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Software Development Tools dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by