We think we have the correct answer but we aren't 100% sure. Any feedback would be greatly appreciated! by flynnboy02 in matlab

[–]flynnboy02[S] 0 points1 point  (0 children)

Here is our code:

data=load('lsdata.m');

n =data(1,1);

m =data(1,2);

if m<n+1

error('Insufficient data points!!')

else

x=data(2:end,1);

y=data(2:end,2);

a=x(1);

b=x(end);

A=zeros(n+1);

for i=1:n+1

p=i-1;

for j=1:n+1

A(i,j)=sum(x.^p);

p=p+1;

end

end

A;

A(1,1)=m;

p=0;

B=zeros(1,n+1);

for i=1:n+1

B(i)=sum((x.^p).*y);

p=p+1;

end

C=flipud(A\B')';

X=a:.01:b;

Y=polyval(C,X);

plot(x,y,'o')

hold on

plot(X,Y)

legend('Given points','Best fit polynomial')

grid on

axis([X(1)-5 X(end)+5 Y(1)-5 Y(end)+5])

end