So I was given a set of x and y coordinates. We were asked to plot the set with a non-connecting points. Then to create a function that generates the best fit line for the list and plot it on the same chart which I did. But it ended up with a bunch of vertical lines connecting the data points and the best fit line together…(
(I've attached a picture)). How do I remove this? Here is my code if you need it for reference:
function cubicfit(x,y)xp=x;yp=y;plot(xp,yp,'.')xlabel('X data')ylabel('Y data')hold on%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=zeros(41,4);for i=1:length(x) A(i,1)=[((x(i))^3)];endfor i=1:length(x) A(i,2)=[((x(i))^2)];endfor i=1:length(x) A(i,3)=[((x(i))^1)]; endfor i=1:length(x) A(i,4)=[((x(i))^0)];end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
c=(((A')*A)^-1)*((A')*y)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%for i=1:length(x)y(i)=((c(1)*(x(i)))^3)+((c(2)*(x(i)))^2)+((c(3)*(x(i))))+(c(4));plot(x,y,'-')end
Best Answer