Lagrange Interpolation Code Error/Help

Hello! I am to graph an interpolating polynomial and the data points. The data points are from a file which I will provide. I keep running into an error which is "Array indices must be positive integers or logical values." for my variable ys. I would love to use polyval however I am not allowed in this assignment. Any idea how to fix this or what I need to change?? I know that p is not the right thing to do...Thanks in advance!
data file (first line are the x values, second line is the y values, and third line is N which will be the number of points to use to graph the polynomial which is found in xs)
1 2 3 4 5 6 7
1 4 9 16 25 36 49
101
code:
clear;
clf;
%reading from dot_data file
fid = fopen('data_file.m','r');
x = str2num(fgetl(fid)); %reads in first line of data
n = length(x); %length of x
tline = fgets(fid); %reads in the second line of data
y = str2num(tline); %converts to numbers
tline = fgets(fid, 3); %reads in last line of data
N = str2num(tline); %N, also converts to number
fclose(fid); %close file
xx = [x];
yy = [y];
deg = n - 1;
%lagrange
sum=0;
for i=1:length(x)
p=1;
for j=1:length(x)
if j~=i
c = poly(x(j))/(x(i)-x(j));
p = conv(p,c);
end
end
term = p*y(i);
sum= sum + term;
end
%max x value
x_n = max(x);
%plotting the polynomial and data points
xs = linspace(x(1), x_n, N);
ys = p(xs); %this is where I have a mistake when using p
plot(xx,ys, 'r'); %plotting the curve/line
hold on;
plot(x, y, '-o');%plotting the points

 采纳的回答

You can evaluate polyval using:
deg = length(p)-1 ; % degree of he polynomial
% get powers of x till deg
X = xs'.^(deg:-1:0) ;
y = sum(p.*X,2) ; % assuming p has coefficients are in decresing powers of n

9 个评论

Thank you! How would I go about implementing that in my code? I am still getting errors when I copy and pasted it and change it to the variables in my code.
You would replace your
ys = p(xs); %this is where I have a mistake when using p
with
deg = length(p)-1 ; % degree of he polynomial
% get powers of x till deg
X = xs'.^(deg:-1:0) ;
ys = sum(p.*X,2) ; % assuming p has coefficients are in decresing powers of n
Thank you! I am still getting an error with the variable ys and unsure why that is :/ Matlab keeps saying
"Error in HW_7 (line 43)
ys = sum(p.*X,2) ; % assuming p has coefficients are in decresing powers of n"
Show the whole code.....don't include file reading.....give the values of inputs instead.
x = [1 2 3 4 5 6 7];
y = [1 4 9 16 25 36 49];
n = length(x); %length of x
deg = n - 1; %deg of polynomial
sum=0;
for i=1:length(x)
p=1;
for j=1:length(x)
if j~=i
c = poly(x(j))/(x(i)-x(j));
p = conv(p,c);
end
end
term = p*y(i);
sum= sum + term;
end
x_n = max(x); %max x value
%plotting the polynomial and data points
xs = linspace(x(1), x_n, N);
%deg = n-1 ; % degree of he polynomial
% get powers of x till deg
X = xs'.^(deg:-1:0);
ys = sum(x.*X,2) ; % assuming p has coefficients are in decresing powers of n
plot(xx, ys, 'r');
hold on;
plot(x, y, '-o');
Is your p a row or a column?
sum= sum + term;
What are you doing with that variable named sum ?
p = conv(p,c);
You are overwriting p for each x value, so it is going to end up holding the value it was last assigned, the one for i = length(x). Are you sure that is what you want?
You should not use the variable names as inbuilt functions. Check the variable sum = 0, this is not allowed.
clear all ;
x = [1 2 3 4 5 6 7];
y = [1 4 9 16 25 36 49];
n = length(x); %length of x
deg = n - 1; %deg of polynomial
thesum=0;
for i=1:length(x)
p=1;
for j=1:length(x)
if j~=i
c = poly(x(j))/(x(i)-x(j));
p = conv(p,c);
end
end
term = p*y(i);
thesum= thesum + term;
end
x_n = max(x); %max x value
%plotting the polynomial and data points
N = 100 ;
xs = linspace(x(1), x_n,N);
%deg = n-1 ; % degree of he polynomial
% get powers of x till deg
X = xs'.^(deg:-1:0);
ys = sum(x.*X,2) ; % assuming p has coefficients are in decresing powers of n
plot(xs, ys, 'r');
hold on;
plot(x, y, '-o');
Using sum as a variable name leads to problems more often than not, as it is very common to want to use sum as a function in the same section of code. We advise to never use sum as the name of a variable.
That makes so much sense! I didn't realize sum was already a variable name so now understand why there were so many issues.
Now the only issue left that I can see is that my data points are being plotted on a straight line and not on the curve. I notice the x axis with 10^4 on the top.
I apologize for asking so many questions and I appreciate all of the help!!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by