Global curve fitting for polynomial function

4 次查看(过去 30 天)
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

回答(3 个)

dbmn
dbmn 2016-10-10
I suggest you read the help for the polyfit function here
p = polyfit(xdata1,ydata1,3);
  3 个评论
Torsten
Torsten 2016-10-10
y_offset is also an unknown parameter.
Best wishes
Torsten.
Christian Sögaard
Christian Sögaard 2016-10-10
编辑:Christian Sögaard 2016-10-10
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.

请先登录,再进行评论。


Torsten
Torsten 2016-10-10
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 个评论
Torsten
Torsten 2016-10-11
编辑:Torsten 2016-10-11
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 2016-10-14
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.

请先登录,再进行评论。


Christian Sögaard
Christian Sögaard 2016-10-14
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)

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by