Error message "Functions definitions are not permitted in this context" Hey guys, I'm trying to build a code to calculate a polynomial using Newton's divided method, but every time I enter a code, even the one in the book , I get this error message.

10 views (last 30 days)
CODE #1:
function F = div_diff(x, y)
% div_diff function computes all divided differences for
% the data stored in x and y = f(x)
n = length(x);
m = length(y);
if m~=n; error('x and y must be same size');
else
F = zeros(n, n);
for i = 1:n % define 0th divided difference
F(i,1) = y(i);
end
for k = 1:n-1 % Get kth divided difference
for i = 1:n-k
F(i,k+1) = (F(i+1,k)-F(i,k))/(x(i+k)-x(i));
end
end
x=[1970,1990];
y=[3707475887,5281653820];
div_diff(x,y)
Code#2 from the Book(Timothy Sauer Numerical Analysis)
%Input: x and y are vectors containing the x and y coordinates
% of the n data points
%Output: coefficients c of interpolating polynomial in nested form
%Use with nest.m to evaluate interpolating polynomial
function c=newtdd(x,y,n)
for j=1:n
v(j,1)=y(j); % Fill in y column of Newton triangle
end
for i=2:n % For column i,
for j=1:n+1-i % fill in column from top to bottom
v(j,i)=(v(j+1,i-1)-v(j,i-1))/(x(j+i-1)-x(j));
end
end
for i=1:n
c(i)=v(1,i); % Read along top of triangle
end % for output coefficients
x0=[1970,1990];
y0=[37007475887,5281653820];
c=newtdd(x0,y0,2);
x=0:.01:5281653820;
y=nest(1,c,x,x0);

Answers (1)

Image Analyst
Image Analyst on 6 Nov 2013
Please attach the m-file(s). You probably put these functions into a script or something like that. Or possibly you're missing an end statement or parenthesis or something. You did not include the full error message (ALL the red text) so I'm just guessing at the moment.
  1 Comment
Gervais
Gervais on 6 Nov 2013
>> function F = div_diff(x, y) % div_diff function computes all divided differences for % the data stored in x and y = f(x) n = length(x); m = length(y); if m~=n; error('x and y must be same size'); else F = zeros(n, n); for i = 1:n % define 0th divided difference F(i,1) = y(i); end for k = 1:n-1 % Get kth divided difference for i = 1:n-k F(i,k+1) = (F(i+1,k)-F(i,k))/(x(i+k)-x(i)); end end
x=[1970,1990];
y=[3707475887,5281653820];
div_diff(x,y)
function F = div_diff(x, y)
|
Error: Function definitions are not permitted in this context.

Sign in to comment.

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!