问题
I have a kind of data and want to find the equation(poly coeff) of given data. For example equation for given sample data is simple a^2*b+10
a\b 5 10 15
________________________
3| 55 100 145
4| 90 170 250
5| 135 260 385
6| 190 370 550
I checked forpolfit
but It only works for one variable.
回答1:
As Dusty Campbell pointed out you can use the fit
function. To do this you have to build a mesh with your data
a = [3 4 5 6];
b = [5 10 15];
[A, B] = meshgrid(a, b);
C = (A.^2).*B + 10;
and then call fit
with a custom equation
ft = fittype('p1*a^2*b + p2', 'independent',{'a','b'}, 'dependent','c');
opts = fitoptions('Method','NonlinearLeastSquares', 'StartPoint',[0.5,1]);
[fitresult, gof] = fit([A(:), B(:)], C(:), ft, opts);
As you'll see the solver converges to the correct solution p1 = 1
, p2 = 10
.
回答2:
polyfitn should help...
Another approach: In the general case of non-linear data fitting you can easily use lsqnonlin.
回答3:
Looks like you need the fit function from the Curve Fitting Toolbox. Or perhaps polyfitn created and shared by another Matlab user.
来源:https://stackoverflow.com/questions/25440188/polyfit-for-two-variables