Filter löschen
Filter löschen

Global curve fitting for polynomial function

3 Ansichten (letzte 30 Tage)
Christian Sögaard
Christian Sögaard am 10 Okt. 2016
Hello all,
I have two sets of data (xdata1, ydata1, and xdata2, ydata2) and I would like to do a global curve fit for these. The function is:
y = y0 + ax + bx^2 + cx^3
where y0 is a shared parameter and a, b, and c can vary for each data set.
I am quite new to MATLAB so I am wondering if anyone have a clue of how to do this?
Thank you in advance for any answers.
/Christian

Antworten (3)

dbmn
dbmn am 10 Okt. 2016
I suggest you read the help for the polyfit function here
p = polyfit(xdata1,ydata1,3);
  3 Kommentare
Torsten
Torsten am 10 Okt. 2016
y_offset is also an unknown parameter.
Best wishes
Torsten.
Christian Sögaard
Christian Sögaard am 10 Okt. 2016
Bearbeitet: Christian Sögaard am 10 Okt. 2016
Thank you for your answers. However, Torsten is right. y0 is not known.
Let me clarify. A fitting needs to be done which produces a the same shared value for y0 but but still produces individual values for a, b, and c, for both data-sets. y0 should be determined so that it gives the best fit possible for both data-sets.

Melden Sie sich an, um zu kommentieren.


Torsten
Torsten am 10 Okt. 2016
The objective function for your problem is given by
f(y0,a1,b1,c1,a2,b2,c2)=
sum_{i=1}^{n1}(ydata1_{i}-(y0+a1*xdata1_{i}+b1*xdata1_{i}^2+c1*xdata1_{i}^3))^2 +
sum_{i=1}^{n2}(ydata2_{i}-(y0+a2*xdata2_{i}+b2*xdata2_{i}^2+c2*xdata2_{i}^3))^2
Now take partial derivatives of f with respect to y0,a1,b1,c1,a2,b2,c2.
You'll arrive at a (7x7) linear system of equations in the unknows y0,a1,b1,c1,a2,b2,c2. This can be solved using \ (backslash).
Best wishes
Torsten.
  2 Kommentare
Torsten
Torsten am 11 Okt. 2016
Bearbeitet: Torsten am 11 Okt. 2016
Alternativly, solve the (overdetermined) linear system of equations using \ (backslash):
y0+a1*xdata1_{i}+b1*xdata1_{i}^2+c1*xdata1_{i}^3=ydata1_{i} (i=1,...,n1)
y0+a2*xdata2_{i}+b2*xdata2_{i}^2+c2*xdata2_{i}^3=ydata2_{i} (i=1,...,n2)
These are (n1+n2) equations in the unknowns y0,a1,a2,a3,b1,b2,b3.
Best wishes
Torsten.
Christian Sögaard
Christian Sögaard am 14 Okt. 2016
Tank you Torsten for suggesting solutions to my problem. However, I was not able to work out how to define the objective function in matlab. Instead I worked out another way of solving my problem, which you can see below.

Melden Sie sich an, um zu kommentieren.


Christian Sögaard
Christian Sögaard am 14 Okt. 2016
I have managed to solve this rather crudely myself. I have written a while loop that takes small steps for the y0 parameter and fits the other parameters with regard to this y0 value. All of the different fittings is then evaluated and the best one is chosen. Below you can see the script code for this:
%initial fitting.
n=6;
p=polyfit(x,y,n)
c1=polyval(p,x);
figure
plot(x,y,'s')
hold on
plot(x,c1)
Rsq=1-sum((y-c1).^2)/sum((y - mean(y)).^2)
n=6;
p0=polyfit(x0,y0,n)
c2=polyval(p0,x0);
figure
plot(x0,y0,'s')
hold on
plot(x0,c2)
Rsq=1-sum((y0-c2).^2)/sum((y0 - mean(y0)).^2)
%global fitting
y_g=p(end)
i=1
%step size for the shared parameter
step=0.001;
while y_g<p0(end)
%blanc
f0=fitoptions('Method','NonlinearLeastSquares',...
'Lower',[-2,-2],...
'Upper',[Inf,max(x0)],...
'StartPoint',[-1 -1 -1 -1 -1 -1]);
ft=fittype('y_global+a*x+b*x^2+c*x^3+d*x^4+e*x^5+f*x^6','problem','y_global','options',f0);
[curve0,gof0]=fit(x0,y0,ft,'problem',y_g)
koef_0(i,:)=coeffvalues(curve0)
R2_0(i,:)=gof0.rsquare
%sample
[curve1,gof1]=fit(x,y,ft,'problem',y_g)
koef1=coeffvalues(curve1)
koef_1(i,:)=coeffvalues(curve1)
R2_1(i,:)=gof1.rsquare
% y_g parameter save
y_save(i,:)=y_g;
%while loop parameters
y_g=y_g+step;
i=i+1;
end
%calc of best fit
R_average=(R2_0+R2_1)./2;
val=1;
tmp=abs(R_average-val);
[idx idx]=min(tmp);
closestR=R_average(idx)
closest_koef0=koef_0(idx,:)
closest_koef1=koef_1(idx,:)
closest_R2_0=R2_0(idx)
closest_R2_1=R2_1(idx)
closest_y0=y_save(idx)

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by