Error when inputting function.
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
Azzi Abdelmalek
il 10 Feb 2013
Modificato: Azzi Abdelmalek
il 10 Feb 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 Commenti
Truxton
il 10 Feb 2013
Azzi Abdelmalek
il 10 Feb 2013
Modificato: Azzi Abdelmalek
il 10 Feb 2013
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
il 10 Feb 2013
Walter Roberson
il 10 Feb 2013
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
il 10 Feb 2013
Azzi Abdelmalek
il 10 Feb 2013
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
il 10 Feb 2013
Truxton
il 10 Feb 2013
Image Analyst
il 11 Feb 2013
You may have an old version - before R2012b with the tabbed ribbon interface. Look for "Save as..." under the File pulldown menu.
Truxton
il 11 Feb 2013
Truxton
il 11 Feb 2013
Categorie
Scopri di più su Function Creation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!