how to input constant value in polyfitn functions

Hi, I have data for "x,y,z" independent variables which I have to fit to the reference "N" data.
p = polyfitn([x(:),y(:),z(:)], N(:), 'constant x(:), y(:), z(:)')
modelterms = 0 0 0; 1 0 0; 0 1 0; 0 0 1; Coefficients: [0.4112 1.6207 -1.8792 0.4623] VarNames: {'x(:)' 'y(:)' 'z(:)'}
Now I need to change the constant[000] value let's say from 0.4112 to 0.9812 and to find the rest of the coefficients? How to do this with polyfitn?
Thanks
Oles

 Accepted Answer

So instead of a constant term to be estimated, you wish to fix it at some new given value, and then estimate the coefficients? While there is no way in polyfitn to explicitly fix a coefficient, you can do so by a simple subterfuge.
Just subtract off the given constant term from the dependent variable, and drop the constant term from your model. So this will suffice.
p = polyfitn([x(:),y(:),z(:)], N(:) - 0.9812 , 'x, y, z')
Of course, the resulting model will have no constant term in it. You could slip it back in using some afterwards sleight of hand though. So after the above fit, this should create a model with the desired (forced) constant term.
p.ModelTerms = [0 0 0;p.ModelTerms];
p.Coefficients = [0.9812,p.Coefficients];
You could now evaluate the model with that fixed constant term in it.
If you worry about things like R^2, then be careful, as models with no constant term, or those with a forced term as we have done here, tend to create screwy numbers for R^2. As I recall, this is why I included an adjusted R^2, which may be more meaningful in those cases.

8 Comments

thank you, if I execute p = polyfitn([x(:),y(:),z(:)], N(:), 'constant x(:), y(:), z(:)') and then take the constant value explicitly as you suggested p = polyfitn([x(:),y(:),z(:)], N(:) - 0.4112 , 'x, y, z') it should give the same results but R2 is slightly different:
R2: 0.9583 AdjustedR2: 0.9573 RMSE: 0.1598
R2: 0.9582 AdjustedR2: 0.9572 RMSE: 0.1598
By the way is it possible to write the function:
model=9.812 + c1.*x +c2.*y +c3.*z
and after that
p = polyfitn([x(:),y(:),z(:)], N(:),'model') ?
Yes, R^2 will possibly be different in the 4th decimal place. I'm not surprised, because the constant term probably was NOT exactly 0.4112.
This is a common mistake, believing that just because MATLAB has displayed a number to 4 digits of accuracy, that this is the true value.
I'm sorry, but I did not write the model parser to be smart enough that if you provide a number there, that it would use that value. I would have told you to do that if you could do so. In hindsight, that would have been a nice feature, but it would require a re-write of the model parser code.
I'm aware that it is not 4 digit number, thefore I entered 15decimal value. The R2 is 0.958276417 and R2' is 0.958243655, but coefficients are the same except one for which difference is 10^-15. RMSE is identical in both cases. If I understood correctly it is not possible to enter for fitting your own function like:
fitfunct(x,y,z)=1-exp(-x/y)+z?
The last time I checked, that is not a polynomial.
p = polyfitn([x(:),y(:),z(:)], N(:) - 0.9812 , 'x, y, z')
-this is ok for 1-st order polynomial and how to set up the same but for the 4-th order of polynomial without constant i.e. 34x3 modelterms?
You can pass in the terms as a numeric arrays, thus that 34x3 array. Or you can pass in the list of terms as a string or cell array of strings. That forces polyfitn to use its own parser for the terms, and any string processing will be less efficient.
So you could use a scheme like this to create the array of terms:
modelterms = dec2base(0:124,5) - '0';
modelterms(1,:) = [];
modelterms(sum(modelterms,2) > 4,:) = [];
Or just use that field from the result of a previous call to polyfitn with the desired model.
not sure if I understood correctly, here what I do:
1) p = polyfitn([x(:),y(:),z(:)], N(:), 4)
ModelTerms: [35x3 double]
2) Copy ModelTerms to ModelTermsNew whereas cut 000 row to have
ModelTermsNew: [34x3 double]
3) p = polyfitn([x(:),y(:),z(:)], N(:) - 0.9812 , ModelTermsNew)
which prompts an error:
Undefined function or variable "varlist".
Error in polyfitn (line 232) polymodel.VarNames = varlist;
I have no idea what you are doing wrong, since you don't actually show the complete code, in terms of where that ModelTermsNew variable came from. HOWEVER, this works with no problems.
modelterms = dec2base(0:124,5) - '0';
modelterms(1,:) = [];
modelterms(sum(modelterms,2) > 4,:) = [];
mdl = polyfitn(rand(100,3),rand(100,1),modelterms);
Now, as I said, I can also use that set of terms in a second call.
MT = mdl.ModelTerms;
mdl2 = polyfitn(rand(100,3),rand(100,1),MT);

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!