How to fit multiple data sets?

Dear all, I have the following question: I need to determine some coefficients(A,B,C,D,E):
ydata=[1:1:10];
xdata=[0.1:0.1:1];
zdata=[10:10:100];
Y=[A+B*X+C*X^2+D*X^3]*Z^E;
the code look as:
xx=xdata'; % X
yy=ydata'; % Y
zz=zdata'; % Z
vect_ones=(10,1);
%declare function
functionfit=@(x,a)x(1)*vect_ones+x(2)*a(1,:)+x(3)*power(a(1,:),2)+x(4)*power(a(1,:),3))*power(b(1,:),x(5));
initial values;
x0=[1 1 1 1 1]; % how arbitrary should be this value?
[x] = lsqcurvefit(functionfit,x0,XX,ZZ,XX);
Questions:
Why "b" is not recognized as variable as "a" is? Can lsqcurvefit function really work with two (multiple) sets of data)?
Thank you

 Risposta accettata

Matt J
Matt J il 17 Giu 2013
Modificato: Matt J il 17 Giu 2013
Why "b" is not recognized as variable as "a" is?
Because you didn't designate it as a variable. You only designated x and a
functionfit=@(x,a)...
That's the way Anonymous Functions work.
Can lsqcurvefit function really work with two (multiple) sets of data)?
It's not clear what you mean by multiple sets of data. As long as ydata has the same dimensions as the output of functionfit, it should be fine.

4 Commenti

Ionut  Anghel
Ionut Anghel il 17 Giu 2013
Modificato: Matt J il 17 Giu 2013
Hi, Thank you for you answer, but still there are some issues. I declared b as but here are the errors: " Input argument "b" is undefined.
Error in ==>
@(x,a,b)(x(1)*vect_ones+x(2)*a(1,:)+x(3)*power(a(1,:),2)+x(4)*power(a(1,:),3))*power(b(1,:),x(5))
Error in ==> lsqcurvefit at 209
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in ==> testfunxtionfit at 14
[x] = lsqcurvefit(functionfit,x0,xx,zz,yy);
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT
cannot continue."
The funny thing is that the function is working if I consider only the plynomial part and not the power part (i.e power (b(1,:)x(5)))
Regards, Ionut
Matt J
Matt J il 17 Giu 2013
Modificato: Matt J il 17 Giu 2013
LSQCURVEFIT expects fitnessfunction to have only 2 input arguments. If a and b are both part of xdata, you must bundle them together in a single array and pass them as 1 argument.
BTW, is there a reason you are doing power(p,q) instead of p.^q ?
The speed seems to be higher (I have about 600 data in one set). However I replace as follow: aa=[xx,zz];
functionfit=@(x,a)((x(1)*vect_ones+x(2)*a(:,1)+x(3)*power(a(:,1),2)+x(4)*power(a(:,1),3))*power(a(:,2)^x(5)));
%
[x] = lsqcurvefit(functionfit,x0,aa,yy);
Still troubles for the "inner matrix dimensions"
Matt J
Matt J il 17 Giu 2013
Modificato: Matt J il 17 Giu 2013
I think you want this:
xx=xdata'; % X
yy=ydata'; % Y
zz=zdata'; % Z
M=bsxfun(@power,xx(:), 0:3);
functionfit=@(p,z) ( M*p(1:4) ).*(z.^p(5));
p0=[1 1 1 1 1].';
p = lsqcurvefit(functionfit,p0(:),zz,yy);

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by