Clear Filters
Clear Filters

how can i create a smooth curve through data points

4 views (last 30 days)
from this code Matlab, how can I create a smooth curve through data points? As shown in the figure.
clear
clc
x=-0.6745:0.06745:0.6745;
x1=-0.6499:0.06499:0.6499;
x2=-0.5856:0.05856:0.5856;
f=[21.0956 20.4078 19.6966 18.9596 18.1937 17.3952 16.5596 15.6810 14.7522 13.7634 12.7012 11.5462 10.2690 8.8199 7.1021 4.8672 0.0048 4.8672 7.1021 8.8199 10.2690 ];
f1=[20.7378 20.0626 19.3646 18.6411 17.8892 17.1054 16.2850 15.4225 14.5107 13.5400 12.5972 11.3633 10.1093 8.6865 6.9997 4.8045 0.01215 4.8045 6.9997 8.6865 10.1093];
f2=[19.7669 19.1259 18.4632 17.7764 17.0626 16.3185 15.5396 14.7208 13.8552 12.9336 11.9435 10.8669 9.6763 8.3251 6.7231 4.6374 0.0090 4.6374 6.7231 8.3251 9.6763];
figure
h=plot(x,f,'-o',x1,f1,'-o',x2,f2,'k-o');
set(h(1),'Color',[0.6350 0.0780 0.1840]);
set(h(2),'Color',[0.4660 0.6740 0.1880]);
title('(a)');
hXL=xlabel('V(Volt)','FontSize',11,'FontWeight','bold');
hXL1=ylabel('{\omega(Ghz)}','FontSize',11,'FontWeight','bold','Rotation',0);
hXL1.Position=hXL.Position+[-0.7 12 0];
lgd1 = legend('{\itb_w} {\mu=0}','{\itb_w} {\mu=1nm}','{\itb_w} {\mu=2nm}');
lgd1.FontSize=9;
xlim([-0.6 0.6]);
xticks(-0.6:0.2:0.6);
ylim([0 22]);
grid minor
grid on

Accepted Answer

John D'Errico
John D'Errico on 16 Dec 2023
Edited: John D'Errico on 17 Dec 2023
It VERY much depends on what you define as "smooth". A smooth curve would generally not have a derivative singularity in it, yet the data you show would belie that, since it looks as if there is a break in the derivative at one point.
x=-0.6745:0.06745:0.6745;
x1=-0.6499:0.06499:0.6499;
x2=-0.5856:0.05856:0.5856;
f=[21.0956 20.4078 19.6966 18.9596 18.1937 17.3952 16.5596 15.6810 14.7522 13.7634 12.7012 11.5462 10.2690 8.8199 7.1021 4.8672 0.0048 4.8672 7.1021 8.8199 10.2690 ];
f1=[20.7378 20.0626 19.3646 18.6411 17.8892 17.1054 16.2850 15.4225 14.5107 13.5400 12.5972 11.3633 10.1093 8.6865 6.9997 4.8045 0.01215 4.8045 6.9997 8.6865 10.1093];
f2=[19.7669 19.1259 18.4632 17.7764 17.0626 16.3185 15.5396 14.7208 13.8552 12.9336 11.9435 10.8669 9.6763 8.3251 6.7231 4.6374 0.0090 4.6374 6.7231 8.3251 9.6763];
plot(x,f,'ro-',x1,f1,'gs-',x2,f2,'bx-')
S = pchip(x,f);
S1 = pchip(x1,f1);
S2 = pchip(x2,f2);
fnplt(S)
hold on
fnplt(S1)
fnplt(S2)
grid on
hold off
The curves drawn are essentially splines (not true splines though, but that is a minor point.) But you should see the curves drawn go through that bottom point without a derivative singularity. (That is, those cusps are not really cusps in this plot, but the bottoms on each low point is now neatly rounded.)
So is that what you are looking for? If not, we can create a curve that does have such a cusp in it. The simplest way might be to just break the curves into two separate fragments.
fnplt(spline(x(1:17),f(1:17)))
hold on
fnplt(spline(x(17:21),f(17:21)))
The resulting curve is nice and smooth within each fragment, but now has a break at the join point.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Dec 2023
Edited: Walter Roberson on 16 Dec 2023
x=-0.6745:0.06745:0.6745;
x1=-0.6499:0.06499:0.6499;
x2=-0.5856:0.05856:0.5856;
f=[21.0956 20.4078 19.6966 18.9596 18.1937 17.3952 16.5596 15.6810 14.7522 13.7634 12.7012 11.5462 10.2690 8.8199 7.1021 4.8672 0.0048 4.8672 7.1021 8.8199 10.2690 ];
f1=[20.7378 20.0626 19.3646 18.6411 17.8892 17.1054 16.2850 15.4225 14.5107 13.5400 12.5972 11.3633 10.1093 8.6865 6.9997 4.8045 0.01215 4.8045 6.9997 8.6865 10.1093];
f2=[19.7669 19.1259 18.4632 17.7764 17.0626 16.3185 15.5396 14.7208 13.8552 12.9336 11.9435 10.8669 9.6763 8.3251 6.7231 4.6374 0.0090 4.6374 6.7231 8.3251 9.6763];
ux = union(union(x, x1),x2);
N = 10;
p = polyfit(x, f, N);
p1 = polyfit(x1, f1, N);
p2 = polyfit(x2, f2, N);
h = plot(ux, polyval(p, ux), '-o', ux, polyval(p1, ux), '-o', ux, polyval(p2, ux), 'k-o');
set(h(1),'Color',[0.6350 0.0780 0.1840]);
set(h(2),'Color',[0.4660 0.6740 0.1880]);
title('(a)');
hXL=xlabel('V(Volt)','FontSize',11,'FontWeight','bold');
hXL1=ylabel('{\omega(Ghz)}','FontSize',11,'FontWeight','bold','Rotation',0);
hXL1.Position=hXL.Position+[-0.7 12 0];
lgd1 = legend('{\itb_w} {\mu=0}','{\itb_w} {\mu=1nm}','{\itb_w} {\mu=2nm}');
lgd1.FontSize=9;
xlim([-0.6 0.6]);
xticks(-0.6:0.2:0.6);
ylim([0 22]);
grid minor
grid on

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!