Fit scatter plot with a curve

25 views (last 30 days)
giannit
giannit on 2 Dec 2021
Commented: giannit on 20 Dec 2021
I'm trying to fit the following data (here plotted using scatter)
with a curve so that the result will be something like this
I tried with polyfit and polyval but failed, so probably I used them in the wrong way, any help?
x = [0.2337;0.296;0.3071;0.4208;0.2055;0.9597;0.8683;0.243;0.3363;0.2793;0.5292;0.2471;0.2282;0.4774;1.0392;0.4361;0.1367;0.2952;0.1983;1.0468;0.906;0.9578;0.5368;0.5956;0.8616;0.1641;0.1312;1.0381;0.2361;0.4668;0.7477;0.5303;1.367;1.0894;1.2836;0.2487;0.5869;0.8664;0.3446;0.5062;0.7245;1.3289;0.4958;1.6644;0.2826;0.6825;0.103;0.3205;0.4456;0.1835;0.2622;0.0673;0.4219;0.639;0.7599;0.2172;0.5491;0.6694;0.3774;1.1869;0.7206;0.9669;0.0672;0.6705;0.1681;1.5364;0.3779;0.3483;0.5097;1.7493;0.5388;0.4481;0.2657;1.2815;0.9019;0.9402;0.12;0.4465;1.0316;0.5493;1.0942;0.2359;0.1906;2.1019;0.9408;0.8557;0.1598;0.9746;0.3083;1.0001;0.9645;0.498;0.0614;0.1956;0.7869;1.2872;0.4342;0.0462];
y = [0.0136;0.0075;0.0089;0.0088;0.0104;0.0153;0.0024;0.01;0.0047;0.0137;0.0026;0.0094;0.0093;0.0044;0.013;0.0018;0.0154;0.0058;0.0107;0.011;0.0019;0.013;0.0078;0.0071;0.0018;0.0204;0.0179;0.007;0.0119;0.0013;0.0142;0.022;0.0182;0.0054;0.0434;0.0079;0.0008;0.0066;0.0039;0.0009;0.0018;0.0199;0.0107;0.0326;0.0092;0.0013;0.0194;0.0057;0.0471;0.0133;0.0139;0.0255;0.016;0.0016;0.0013;0.0093;0.0011;0.0014;0.0233;0.0217;0.0003;0.004;0.0251;0.0049;0.0133;0.0316;0.0029;0.0082;0.0156;0.0476;0.0013;0.0016;0.0079;0.0307;0.013;0.012;0.0239;0.0134;0.0203;0.0007;0.013;0.009;0.0146;0.0556;0.0221;0.0027;0.0159;0.0037;0.0053;0.0035;0.0076;0.0008;0.0291;0.0162;0.017;0.0418;0.0146;0.033];
f = polyfit(x, y, 5);
v = polyval(f, x);
plot(x,y,'o', x,v,'-')

Accepted Answer

Alan Stevens
Alan Stevens on 2 Dec 2021
More like ths?
x = [0.2337;0.296;0.3071;0.4208;0.2055;0.9597;0.8683;0.243;0.3363;0.2793;0.5292;0.2471;0.2282;0.4774;1.0392;0.4361;0.1367;0.2952;0.1983;1.0468;0.906;0.9578;0.5368;0.5956;0.8616;0.1641;0.1312;1.0381;0.2361;0.4668;0.7477;0.5303;1.367;1.0894;1.2836;0.2487;0.5869;0.8664;0.3446;0.5062;0.7245;1.3289;0.4958;1.6644;0.2826;0.6825;0.103;0.3205;0.4456;0.1835;0.2622;0.0673;0.4219;0.639;0.7599;0.2172;0.5491;0.6694;0.3774;1.1869;0.7206;0.9669;0.0672;0.6705;0.1681;1.5364;0.3779;0.3483;0.5097;1.7493;0.5388;0.4481;0.2657;1.2815;0.9019;0.9402;0.12;0.4465;1.0316;0.5493;1.0942;0.2359;0.1906;2.1019;0.9408;0.8557;0.1598;0.9746;0.3083;1.0001;0.9645;0.498;0.0614;0.1956;0.7869;1.2872;0.4342;0.0462];
y = [0.0136;0.0075;0.0089;0.0088;0.0104;0.0153;0.0024;0.01;0.0047;0.0137;0.0026;0.0094;0.0093;0.0044;0.013;0.0018;0.0154;0.0058;0.0107;0.011;0.0019;0.013;0.0078;0.0071;0.0018;0.0204;0.0179;0.007;0.0119;0.0013;0.0142;0.022;0.0182;0.0054;0.0434;0.0079;0.0008;0.0066;0.0039;0.0009;0.0018;0.0199;0.0107;0.0326;0.0092;0.0013;0.0194;0.0057;0.0471;0.0133;0.0139;0.0255;0.016;0.0016;0.0013;0.0093;0.0011;0.0014;0.0233;0.0217;0.0003;0.004;0.0251;0.0049;0.0133;0.0316;0.0029;0.0082;0.0156;0.0476;0.0013;0.0016;0.0079;0.0307;0.013;0.012;0.0239;0.0134;0.0203;0.0007;0.013;0.009;0.0146;0.0556;0.0221;0.0027;0.0159;0.0037;0.0053;0.0035;0.0076;0.0008;0.0291;0.0162;0.017;0.0418;0.0146;0.033];
f = polyfit(x, y, 5);
xx = 0:0.1:2.5; %%%%%%%%%%%%%%%%%%%%%%
v = polyval(f, xx); %%%%%%%%%%%%%%%%%%%
plot(x,y,'o', xx,v,'-')
  3 Comments
Alan Stevens
Alan Stevens on 2 Dec 2021
You could split it into regions. For example:
x = [0.2337;0.296;0.3071;0.4208;0.2055;0.9597;0.8683;0.243;0.3363;0.2793;0.5292;0.2471;0.2282;0.4774;1.0392;0.4361;0.1367;0.2952;0.1983;1.0468;0.906;0.9578;0.5368;0.5956;0.8616;0.1641;0.1312;1.0381;0.2361;0.4668;0.7477;0.5303;1.367;1.0894;1.2836;0.2487;0.5869;0.8664;0.3446;0.5062;0.7245;1.3289;0.4958;1.6644;0.2826;0.6825;0.103;0.3205;0.4456;0.1835;0.2622;0.0673;0.4219;0.639;0.7599;0.2172;0.5491;0.6694;0.3774;1.1869;0.7206;0.9669;0.0672;0.6705;0.1681;1.5364;0.3779;0.3483;0.5097;1.7493;0.5388;0.4481;0.2657;1.2815;0.9019;0.9402;0.12;0.4465;1.0316;0.5493;1.0942;0.2359;0.1906;2.1019;0.9408;0.8557;0.1598;0.9746;0.3083;1.0001;0.9645;0.498;0.0614;0.1956;0.7869;1.2872;0.4342;0.0462];
y = [0.0136;0.0075;0.0089;0.0088;0.0104;0.0153;0.0024;0.01;0.0047;0.0137;0.0026;0.0094;0.0093;0.0044;0.013;0.0018;0.0154;0.0058;0.0107;0.011;0.0019;0.013;0.0078;0.0071;0.0018;0.0204;0.0179;0.007;0.0119;0.0013;0.0142;0.022;0.0182;0.0054;0.0434;0.0079;0.0008;0.0066;0.0039;0.0009;0.0018;0.0199;0.0107;0.0326;0.0092;0.0013;0.0194;0.0057;0.0471;0.0133;0.0139;0.0255;0.016;0.0016;0.0013;0.0093;0.0011;0.0014;0.0233;0.0217;0.0003;0.004;0.0251;0.0049;0.0133;0.0316;0.0029;0.0082;0.0156;0.0476;0.0013;0.0016;0.0079;0.0307;0.013;0.012;0.0239;0.0134;0.0203;0.0007;0.013;0.009;0.0146;0.0556;0.0221;0.0027;0.0159;0.0037;0.0053;0.0035;0.0076;0.0008;0.0291;0.0162;0.017;0.0418;0.0146;0.033];
xbreak = 1.02; % change value as desired
n = size(find(x<=xbreak),1); % Number of values <= xbreak
z = [x y]; % Combine for sorting
z = sortrows(z); % Sort rows based on first column of z, namely the x values
% Separate data into two sets
xlo = z(1:n,1); ylo = z(1:n,2);
xhi = z(n+1:end,1); yhi = z(n+1:end,2);
plot(xlo,ylo,'o',xhi,yhi,'s'),grid % plot data
hold on
% Fit separate curves to each set
flo = polyfit(xlo,ylo,2);
fhi = polyfit(xhi,yhi,2);
% Construct fitted curves
xxlo = 0:0.1:xbreak;
xxhi = xbreak:0.1:2.5;
vlo = polyval(flo, xxlo);
vhi = polyval(fhi, xxhi);
% plot fitted curves on top of data
plot(xxlo,vlo,xxhi,vhi)
Only you can decide if the results make sense!
giannit
giannit on 20 Dec 2021
Thank you very much!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 2 Dec 2021
Not sure if the scattered data is legitimate or noise. The bottom of the data looks like a nice polynomial. So if you want to fit just the highly clustered points along the bottom and ignore some of the outliers in the middle, you could try fitPolynomialRANSAC if you have the Computer Vision Toolbox.

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!