Is there any way to fit a function with n variables in Matlab? Any example would be very useful. Till now I used curve fitting toolbox, which provides solution I need for functions with 2 arguments. But now I need to fit a function with much more variables. The worst thing is that dependance is non-linear (probably something like a/x+b/y+c/z+…, but it's only a hypothesis). If it was linear, '\' operator would do the trick.
3
votes
2 Answers
3
votes
lsqnonlin
will do, e.g.
%% generate noisy points for fitting
a = 1; b = 2; c = 3;
x = rand(100,3);
y = a./x(:,1) + b./x(:,2) + c./x(:,3) + 0.1*rand(1,1);
%% fitting
% define residual vector
minRes = @(p) (p(1) ./ x(:,1) + p(2) ./ x(:,2) + p(3) ./ x(:,3) - y);
% start values
par0 = [1,1,1];
% optimize
par = lsqnonlin(minRes, par0);