Newtons Form Trouble Shooting

5 Ansichten (letzte 30 Tage)
Carter
Carter am 25 Okt. 2014
Beantwortet: Geoff Hayes am 25 Okt. 2014
So I am using Newton's form for polynomial interpolation and I am using code I found from "Applied Numerical Analysis Using MATLAB" by Fausett. Below is my what I have in my M-file with my nodes of interpolation (x and y).
x = [-81.091,-81.058,-80.818,-80.387,-79.789]'
y = [48,72,96,120,140]'
function a = NewtonCoef(x, y)
n = length(y)
%calculate coefficients of Newton interpolating polynomial
a(1) = y(1)
for k = 1 : n-1
d(k,1) = (y(k+1)-y(k))/(x(k+1)-x(k));
end
for j = 2 : n-1
for k = 1 : n-j
d(k,j) = (d(k+1,j-1) - d(j,j-1))/(x(k+j) - x(k));
end
end
d
for j = 2 : n
a(j) = d(1,j-1);
end
My problem is when ever I try to run this I get the error...
error: 'y' undefined near line 5 column 12
error: evaluating argument list element number 1
error: called from:
error: NewtonCoef at line 5, column 3
I don't get it, do I need to enter my y and x in a different way? As far as I can tell the vector should work. I am a novice user of MATLAB but have done well in the class until now.
Any help would be appreciated.
Thanks

Antworten (1)

Geoff Hayes
Geoff Hayes am 25 Okt. 2014
Carter - your definitions of x and y are fine, but these are initializations that must occur in the base workspace and not be put in the m file. From your above code, it looks like you pasted the
x = [-81.091,-81.058,-80.818,-80.387,-79.789]'
y = [48,72,96,120,140]'
into the file, and then tried to run it. (Though this might give a different error if you had done this.)
Make sure that the first line in your NewtonCoef.m file is the function signature
function a = NewtonCoef(x, y)
and then do the following - in the Command Window, create the x and y as
>> x = [-81.091,-81.058,-80.818,-80.387,-79.789]'
>> y = [48,72,96,120,140]'
and then call your function with these two input parameters
>> a = NewtonCoef(x,y);
When I run the above, I see that
a =
48 727.27 0 -1.9383 0
Note that you may want to add some semicolons at the end of your assignment lines (in NewtonCoef) to prevent the Command Window from being cluttered with extra information.

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by