Global curve fitting for polynomial function
显示 更早的评论
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
2016-10-10
p = polyfit(xdata1,ydata1,3);
3 个评论
I just read, that the y0 should be shared. For forcing the curve trough a specific point y_offset @x==0 you can use the following
params = [x'.^3, x'.^2, x']\(y-y_offset)';
c = params(1); b = params(2); a = params(3);
You might need of not need the ' depending on the shape of your x and y vectors.
[edit: credit to John d'Errico for the solution with the Solution with the mldivide operator]
Torsten
2016-10-10
y_offset is also an unknown parameter.
Best wishes
Torsten.
Christian Sögaard
2016-10-10
编辑:Christian Sögaard
2016-10-10
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 个评论
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
2016-10-14
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!