# Get the coefficients in a vector from cftool ?

12 views (last 30 days)
Seb Seb on 22 Feb 2015
Answered: Seb Seb on 23 Feb 2015
Hi,
In my script (*.m file), I would like to use cftool to get the 15 coefficients from a polynomial function (degrees 4) and put them in a vector. This vector will be use latter in my script. It have to be done without configuring the parameters in cftool windows. I would like everything to be automatic.
Any ideas to do it with cftool ? I already tried with fittype but it don't allow a so big z matrix (x and y are 1x17 vectors and z is a 17x17 matrix for example).
Kind regards, Seb

Greig on 23 Feb 2015
Edited: Greig on 23 Feb 2015
To extract the coefficients use the "fit" function....
[fitobject,gof,output]=fit([x, y], z, 'poly44');
fitobject.p00, fitobject.p10, fitobject.p01, etc contain all of the coefficients. So,
Coeffs = [fitobject.p00, fitobject.p10, fitobject.p01,....
doc fit
[Edit: First post was a solution for curve fitting, then I realised you were surface fitting.]

Seb Seb on 23 Feb 2015
Unfortunately, it doesn't work, I got the same problem as fittype.
Here is the message error :
>> [fitobject,gof,output]=fit([x, y], z, 'poly44')
Error using fit>iFit (line 127)
X must be a matrix with one or two columns.
Error in fit (line 108)
[fitobj, goodness, output] = iFit( xdatain, ydatain, fittypeobj, ...
x is a 1x15 vector, y is a 1x15 vector, z is a 15x15 matrix (The equation output).
Any Ideas ?
Kind regards, Seb

Greig on 23 Feb 2015
You'll need to do some reshaping as fit expects column vectors for x, y and z. If I have my dimensions correct, something like this should work.
x2=reshape(meshgrid(x,y), numel(z), 1);
y2=repmat(y, 15, 1);
z2=reshape(z, numel(z), 1);
[fitobject,gof,output]=fit([x2, y2], z2, 'poly44');

Seb Seb on 23 Feb 2015
It still not working.
x2=reshape(meshgrid(x,y), numel(z), 1);
y2=repmat(y, 15, 1);
z2=reshape(z, numel(z), 1);
>> [fitobject,gof,output]=fit([x2, y2], z2, 'poly44')
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
x is a 1x15 vector.
y is a 1x15 vector.
z is a 15x15 matrix.
x2 is a 225x1 vector.
y2 is a 15x15 matrix. (Why is y2 became my matrix and not z2 ?)
z2 is a 225x1 vector.
Thanks for your help because I don't get exactly the purpose of reshaping, Seb.
Greig on 23 Feb 2015
Sorry, I missed a transpose, it should be
y2=repmat(y', 15, 1);
y2 should now be 225x1.
The fit command expects x, y, and z to be column vectors. As it stands, you have x and y laid out like a coordinate system on which z can be laid. If you look at x2, the first 15 elements should all be the same, then the second 15 all the same and so on. The first 15 elements of y2 are y, then this repeats. In this way [x2,y2] represents all of the x-y coordinates in a long list. z is then reshaped to match this. cftool is smart enough to recognize this and does the reshaping internally.
I hope this makes sense.

Seb Seb on 23 Feb 2015
Thanks a lot, it's working. And now reshaping make sense.
Thanks, Seb.