how to input constant value in polyfitn functions

5 次查看(过去 30 天)
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

采纳的回答

John D'Errico
John D'Errico 2015-3-20
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 个评论
Oleksandr Oleksandras
编辑:Oleksandr Oleksandras 2015-4-9
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;
John D'Errico
John D'Errico 2015-4-9
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);

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by