How to make plots with symbols(representing data points) and a fitting curve (fit the points with polynomial or other curve equations)

4 次查看(过去 30 天)
This is the code I have right which gives a smooth curve passing through the data points:
figure
x1 = [0,0.0521,0.1042, 0.1563, 0.2083, 0.2604, 0.3125, 0.3646, 0.4167];
x1q = linspace(x1(1),x1(end),100);
y1 = [2.4015, 2.9473, 4.5847, 5.7855, 7.4229, 9.6061, 12.2259, 13.2083, 13.6450];
y1q = interp1(x1,y1,x1q,'pchip');
x2 = [0, 0.0510, 0.1020, 0.1531, 0.2041, 0.2551];
x2q = linspace(x2(1),x2(end),100);
y2 = [1.6836, 2.3150, 3.2620, 3.9986, 4.9456, 6.8397];
y2q = interp1(x2,y2,x2q,'pchip');
plot(x1q,y1q,'r',x2q,y2q,'b')
hold on
plot(x1,y1,'rx',x2,y2,'bo')

回答(1 个)

Star Strider
Star Strider 2019-3-26
Try this:
b1 = polyfit(x1, y1, 1); % Linear Function
y1p = polyval(b1, x1q);
b2 = polyfit(x2, y2, 3); % 3-Degree Polynomial
y2p = polyval(b2, x2q);
figure
plot(x1, y1, '+r', x1q, y1p, '-r')
hold on
plot(x2, y2, 'ob', x2q, y2p, '-b')
hold off
grid
See the documentation for polyfit (link) for details.
Experiment to get the result you want.

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by