How to create a smooth curve through data points?

1,051 views (last 30 days)
I have a basic plot as follows. x = [0 0.2 0.4 0.8 1.2 1.6 2.0]; y = [0 0.155 0.240 0.328 0.450 0.582 0.692];
plot(x,y,'x','MarkerEdgeColor','black') grid on; xlabel('Protein standard concentration (µg/µl)'); ylabel('Average absorbance value');
Now I want a smooth curve to go through the data points. Any suggestions?

Accepted Answer

Star Strider
Star Strider on 5 Dec 2017
Edited: MathWorks Support Team on 6 Mar 2023
There are various ways you can achieve this:
Below is an example of using 'polyfit'. A linear fit is probably appropriate, unless you have a specific equation you want to fit to it:
x = [0 0.2 0.4 0.8 1.2 1.6 2.0];
y = [0 0.155 0.240 0.328 0.450 0.582 0.692];
p = polyfit(x, y, 1);
v = polyval(p, x);
figure(1)
plot(x,y,'x','MarkerEdgeColor','black')
hold on
plot(x, v)
hold off
grid on;
xlabel('Protein standard concentration (µg/µl)');

More Answers (2)

John D'Errico
John D'Errico on 5 Dec 2017
The question is, do you have knowledge of this process? Well, everybody knows something about their data, about the underlying system that produced it.
For example, do you know the curve passes through (0,0)? Very often, when point is supplied as (0,0), you know the curve passes through that point.
Is the bump down at the bottom real, or is it just noise?
Thus, the smoothest curve I can imagine is this:
S = x(:)\y(:)
S =
0.36704
plot(x,y,'o',x,S*x,'r-')
What I don't know is if the lack of fit is significant, or just data noise.
Of course, if the point at zero is not important, then a simple linear fit with a constant makes sense.
P1 = polyfit(x,y,1); plot(x,y,'o',x,P1(1)*x+P1(2),'r-')
Alternatively, we might consider a cubic polynomial, with no constant term.
C = (x(:).^(1:4))\y(:);
xint = linspace(0,2,100)';
plot(x,y,'o',xint,(xint.^(1:4))*C,'r-')
Thus,
y = C(1)*x + C(2)*x^2 + C(3)*x^3 + C(4)*x^4
That forces the curve to pass through zero, has a bump at the bottom end, etc. But it has a little tweak near the top.
spl = spline(x,y);
plot(x,y,'o',xint,ppval(spl,xint),'r-')
What matters is what you know about the curve, what you know about the data.

MHZ
MHZ on 5 Dec 2017
One option is cftool Another option is polyfit function Third option is smooth function

Community Treasure Hunt

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

Start Hunting!