How would you fit a 2d curve such as ln(x^2) + 3y to an mxn array?
Update
I mean I have mxn array and want fit it with a 2D curve. Sorry for confusion.
How would you fit a 2d curve such as ln(x^2) + 3y to an mxn array?
Update
I mean I have mxn array and want fit it with a 2D curve. Sorry for confusion.
I would recommend running cftool
. It's actually quite capable for wizard-type gadget.
Here's a programmatic fitting example (i like the MATLAB documentation), and a perhaps pertinent excerpt...
s = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,max(cdate)],...
'Startpoint',[1 1]);
f = fittype('a*(x-b)^n','problem','n','options',s);
Fit the data using the fit options and a value of n = 2:
[c2,gof2] = fit(cdate,pop,f,'problem',2)
anyways. i hope that helps some
You can start with using meshgrid to generate two arrays that correspond to the indices of your mxn array (which we'll call z for simplicity). For example:
[x,y] = meshgrid(1:size(z,2),1:size(z,1));
Trace x and y in your command window to see their structure, it'll make sense.
Then you can use a function such as lsqnonlin (nonlinear least squares) to fit a 2d curve to your matrix z. So if the curve you want to fit is "a*log(x^2) + b*y", where a and b are free parameters, then you could use something like this:
%Define a function which returns the residual between your matrix and your fitted curve
myfun = @(params) params(1)*log(x(:).^2) + params(2)*y(:) - z(:)
%Define initial guesses for parameters a, b
params0 = [1,3];
%Add lots of debugging info
opts = optimset('Display','Iter');
%Fit
fitparams = lsqnonlin(myfun,params0,[],[],opts);