callculating values using cfit object with multiple p values

2 views (last 30 days)
I have 30 diferent sets of p1,p2 and p3 coefficients in a cfit object for a 2nd order polynomial. p1 = 30x1double made up of the 30 p1 coefficients values. Same for p2 and p3. Code attached below.
f=fittype('poly2')
CoRepeat=cfit(f,Co(:,1),Co(:,2), Co(:,3));
samples = [45:600]';
Repeat=CoRepeat(samples);
I would like to work out 'Repeat' for all 'samples' values. e.g y=p1*x^2 + p2*x + p3. For all 30 p1, p2 and p3 values.
Thanks in advance.

Answers (1)

dpb
dpb on 9 Jun 2021
Edited: dpb on 9 Jun 2021
I don't see any syntax by which to do that with the coefficents for multiple fits stored that way and the doc for cfit explicitly says
coeff1,...,coeffn — Coefficient values
scalar (default)
which says each coefficient shall be a scalar, not an array of coefficients.
That looks like a Quality of Implementation issue that should generate at least a warning on the use of the array there.
Only way I can see to do anything with it after having created it, will be to retrieve the coefficients individually as vectors and use them conventionally in things like polyval which defeats the whole purpose of having the fit object.
I think what you'll have to do is create an array of fit objects, not a single object with an array of coefficients.
ADDENDUM:
Neqn=5; % number equations desired
CO=mat2cell(randi(20,N,3),ones(1,N)); % arbitrary way to generate CO as cell array
f=fittype('poly2'); % fit object to clone/fill
F=cellfun(@(c)cfit(f,c(1),c(2),c(3)),CO,'UniformOutput',0); % build the array of cfit
Above produces (edited for brevity) for an aribtrary realization of randi() coefficients:
>> celldisp(F)
F{1} =
Linear model Poly2:
y(x) = p1*x^2 + p2*x + p3
Coefficients:
p1 = 8
p2 = 4
p3 = 13
...
F{4} =
Linear model Poly2:
y(x) = p1*x^2 + p2*x + p3
Coefficients:
p1 = 1
p2 = 14
p3 = 2
F{5} =
Linear model Poly2:
y(x) = p1*x^2 + p2*x + p3
Coefficients:
p1 = 20
p2 = 8
p3 = 20
>>
You can iterate over the F array to evaluate or do whatever as desired.
I don't see any other way than the above or something equivalent.
One could ask for the generalization as an enhancement request, but not sure how the syntax could work, even.

Categories

Find more on Get Started with DSP System Toolbox in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!