plot a smooth curve
16 views (last 30 days)
Show older comments
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1268375/image.jpeg)
0 Comments
Answers (2)
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").
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!
0 Comments
See Also
Categories
Find more on Interpolation 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!