MATLAB Answers

CurveFit on Vector Data Points

5 views (last 30 days)
hamzah khan
hamzah khan on 8 Sep 2020
Commented: hamzah khan on 9 Sep 2020
Hello,
I have the following data vectors; b_reference are the independant values and Y are the dependant values.
b_reference_1 = [485.9;251.975;358.55;313.6565;85.322;193.4422;53.425;106.66;71.88;84.8653];
Y_1 =[1;1;1.31;1.31;1.89;2;1.45;1.42;1.95;1.98];
b_reference_2 = [465.786;54.6699;351.2213;197.0658;125.7026;125.4637;68.0795;72.8793;83.1339;45.9044];
Y_2 =[1;1;1.3;1.3;1.95;2;1.41;1.40;2;1.85];
b_reference_3 = [465.9498;243.369;516.6983;452.2795;81.5134;179.418;65.7858;130.3574;58.6862;69.4206];
Y_3 =[0.95;0.96;1.88;1.88;1.85;1.85;1.75;1.75;1.65;1.58];
I am trying to fit a curve through these data points and trying to find a function which will fit a curve, and also predict some Y values for some other b_reference values.
I looked at lsqcurvefit and lsqnonlin, but they both require a function and I dont have it right now. I am trying to find a function which can fit the curve and also predict Y values for some other b_reference vector.
Does anyone know...?

  1 Comment

KSSV
KSSV on 8 Sep 2020
your data is highly random.....

Sign in to comment.

Answers (2)

KSSV
KSSV on 8 Sep 2020
b_reference_1 = [485.9;251.975;358.55;313.6565;85.322;193.4422;53.425;106.66;71.88;84.8653];
Y_1 =[1;1;1.31;1.31;1.89;2;1.45;1.42;1.95;1.98];
b_reference_2 = [465.786;54.6699;351.2213;197.0658;125.7026;125.4637;68.0795;72.8793;83.1339;45.9044];
Y_2 =[1;1;1.3;1.3;1.95;2;1.41;1.40;2;1.85];
b_reference_3 = [465.9498;243.369;516.6983;452.2795;81.5134;179.418;65.7858;130.3574;58.6862;69.4206];
Y_3 =[0.95;0.96;1.88;1.88;1.85;1.85;1.75;1.75;1.65;1.58];
x = [b_reference_1 ; b_reference_2 ; b_reference_3] ;
y = [Y_1 ; Y_2 ;Y_3] ;
[x,idx] = sort(x) ;
y = y(idx) ;
% Use interp1 to get your values
xi = 50 ; % give your required values
yi = interp1(x,y,xi) ; % get values at xi

  2 Comments

hamzah khan
hamzah khan on 8 Sep 2020
Hi,
Thanks :)
If I add xi = 550. which is not in the list, will it also give me the yi values?
like sort of extrapolation...
KSSV
KSSV on 8 Sep 2020
If the given value is not in the list, it is called extrapolation and we cannot trust the value. As your data is random we should not do that.

Sign in to comment.


John D'Errico
John D'Errico on 8 Sep 2020
Edited: John D'Errico on 8 Sep 2020
The problem is, your data appears to be highly noisy.
You can use simple linear interpolation to interpolate the data. KSSV hs shown how to do so, using interp1. No NOT use a spline to interpolate this!!!!!! It would be highly inappropriate to try a spline, since a spline will introduce large oscillations into the curve, chasing what appears to be noise.
When highly noisy data arises, it is often the simplest models that are correct. Essentially, the signal to noise ratio is very low, so the only signal we can see above the noise may be a simple linear trend. For this, polyfit will be sufficient.
plot(b_reference_1,Y_1,'o')
P1 = polyfit(b_reference_1,Y_1,1)
P1 =
-0.00187650587476397 1.90736627968731
So the fitted model is just y = P1(1)*x + P1(2). You can use the model in this form, or you can use polyval to evaluate it at any point x.
By the way, if you have the curve fitting toolbox, then I would have done:
mdl1 = fit(b_reference_1,Y_1,'poly1')
mdl1 =
Linear model Poly1:
mdl(x) = p1*x + p2
Coefficients (with 95% confidence bounds):
p1 = -0.001877 (-0.003439, -0.0003144)
p2 = 1.907 (1.525, 2.289)
And now you can use the fitted model to predict y as a function of x.
mdl1(200)
ans =
1.53206510473452
But since everybody has a copy of polyfit, I used that.
hold on
x = linspace(min(b_reference_1),max(b_reference_1),10);
plot(x,P1(1)*x + P1(2),'r-')
The alternative of linear interpolation will create this "curve", which to my eyes is hardly useful, since it appears to be chasing the noise in your data.
[b1sort,ind] = sort(b_reference_1);
y1sort = Y_1(ind);
plot(b1sort,y1sort,'o-')
I've overlaid it with the simple linear fit to show the difference. Now, I've worked with scientists who truly had noisy data they needed to track carefully.
The question is, if you measured the same data again for this instance, would that curve shape repeat? Or would you just see a simple trend again, but with different totally random noise on top? This is a good justification for replication, which will help you to learn if what appears to be noise in your data really is noise, or if the bumps are truly signal, and must be treated as such. Until you learn which is the case, you cannot know how to treat your data, but I would typically argue that until then, you need to use the simplest models available.

  1 Comment

hamzah khan
hamzah khan on 9 Sep 2020
Hi,
sorry for late reply.
your answer is very detaileld and very good :)
I double checked and my data does not have noise, those bumps are signals...
I want to treat them as such...
Can I use spline ?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by