MATLAB: Plotting fitted curve to a set of data

fitted curveMATLABplotting

Hi, I am trying to plot a graph of a certain data set of x and y, which I have no problem with, but also i have to plot a FITTED CURVE to that set of data. I been trying and looking for solution on how to get it on google but this is all i've got so far: plot(x, y, '*r') / hold on / plot(x,y, '-b') / hold off.
however the output is more likely to be a line that connects all the points more than a fitted curve
please anybody have a solution???
thanks

Best Answer

Here is some example code that shows two typical ways of plotting a data set a long with a fitted curve.
% example plotting x,y data and fitted curve
% make example data set (quadratic with some noise added)
x = 1:10;
y = 2*x.^2 + 3 + 25*rand(1,length(x));
% fit the data
c = polyfit(x,y,2);
% generate fitted curve
xfit = 0:0.1:10;
yfit = polyval(c,xfit);
% plot the original data as symbols and fitted curve as connected line
figure % open a new figure for the plot

plot(x,y,'o',xfit,yfit,'-')
legend({'data','curve-fit'})
title('data and fitted curve')
% alternatively you can use the hold on function and make separate calls to
% the plot function
figure % open a new figure for the plot
plot(x,y,'o'); % plot the data
hold on % set hold on so next curve will go in the same plot
plot(xfit,yfit,'-')
hold off % turn hold off in case there is a later call to the plot function
legend({'data','curve-fit'})
title('data and fitted curve')