plot a smooth curve

7 views (last 30 days)
Mohanned Al Gharawi
Mohanned Al Gharawi on 19 Jan 2023
Answered: Nikhil Sapre on 19 Jan 2023
Hello everyone,
I plotted a curve with a set of data by using a spline technique, but the problem is the start and the end of the curve is matching with the original dataset. My code is listed below. Is there any tecnhique or command that makes the curve more smoother or can we use the fitting technique. please help me in that.
Thanks
clc
clear
[x]=[1.111409702 2.561607677 4.152025868 5.825439427 7.677672228 9.669974952 11.68269125 13.63751636 15.45892018 17.11664862 18.82457281];
[y]=[0.05 0.3 0.74996023 9.99803011 13.68801508 14.57288221 11.85807424 4.94788522 2.04185776 0.49090234 0.03087319];
plot (x, y, 'g', 'LineWidth',2);
grid
A = trapz(x, y)
Increase = 10;
newX = linspace(1,20, 25 * Increase);
smoothedY = spline(x, y, newX);
hold on
% plot(newXSamplePoints, smoothedY, '-ob');
plot(newX, smoothedY);
hold off

Answers (2)

Kunal Kandhari
Kunal Kandhari on 19 Jan 2023
Hi Mohanned,
It sounds like you want a kind of interpolation, because "smoothing" usually trims the values of the extreme points of a curve, whereas interpolation fits those points exactly (as per your requirement that "the peak should be at same point").
I have done it using pchip.
clc
clear
[x]=[1.111409702 2.561607677 4.152025868 5.825439427 7.677672228 9.669974952 11.68269125 13.63751636 15.45892018 17.11664862 18.82457281];
[y]=[0.05 0.3 0.74996023 9.99803011 13.68801508 14.57288221 11.85807424 4.94788522 2.04185776 0.49090234 0.03087319];
plot (x, y, 'g', 'LineWidth',2);
grid
A = trapz(x, y)
Increase = 10;
newX = linspace(1,20, 25 * Increase);
smoothedY = spline(x, y, newX);
% hold on
% % plot(newXSamplePoints, smoothedY, '-ob');
% plot(newX, smoothedY);
% hold off
Xi = 0:0.1:19;
Yi = pchip(x,y,Xi);
hold on
plot(Xi,Yi)
hold off
There are other 1-D data interpolation methods to choose from, go to method — Interpolation method section in this page, so you should take a look and pick one that best suits your needs.
Hope this helps!

Nikhil Sapre
Nikhil Sapre on 19 Jan 2023
You can use the curveFitter app.

Community Treasure Hunt

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

Start Hunting!