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

4 views (last 30 days)
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')

Answers (1)

Star Strider
Star Strider on 26 Mar 2019
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.

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!