plotting fitted curve to a set of data
74 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Jon
on 16 Apr 2019
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')
3 Comments
Jon
on 16 Apr 2019
Does this answer your question? If not please detail what else you are looking for. If this answers your question, please accept this as answered.
John D'Errico
on 16 Apr 2019
Edited: John D'Errico
on 16 Apr 2019
+1. I might have answered this question, but I'm not sure what I might have said that Jonathan did not say well already. I suppose there are a couple of possibilities you might explore.
One thing you might do is use the curvefitting toolbox, if you have it. It will offer a fairly wide variety of nonlinear models, splines, etc. You would need to decide on a model then. It cannot do that for you.
Another thing you can do is a spline fit. Again, in the CFT, or in MATLAB itself, you can find interpolating splines. In the CFT, there will also be smoothing splines.
You can also find a variety of simple fitting tools from the pulldown menu on your plot figure. Thus look at Tools/Basic Fitting.
More Answers (0)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!