I have the data for independent variables x1, x2, ... (2 or more) and
for the dependent variables say y = f(x1, x2, x3, x4, ...). The
degree of the equation is more than 2(non-linear). Is there a way
tofindout the best fit for the equation for different degrees?
Any help is greatly appriciated.
Thanks,
sasmita
Hi,
In general you can use 'polyfit' several times.For example if you have
two variables do the following:
For each value of Yi fit Z to X so that:
Z(:,Yi)=sum(a(k,Yi)*X(:).^k) % sum over k
Then fit a(k,:) using 'polyfit' again:
a(k,:)=sum(b(k,n).*Y(:).^n) % sum over n.
b(k,n) are the 2D polynomial coefficients.
One more thing: If your data is scattered, use griddata first to
create a continuous mesh.
Joe
BSTEX - Equation viewer for Matlab
http://www.geocities.com/bstex2001 <http://www.geocities.com/bstex2001>
> Hi,
>
>
> I have the data for independent variables x1, x2, ... (2 or more) and
> for the dependent variables say y = f(x1, x2, x3, x4, ...). The
> degree of the equation is more than 2(non-linear). Is there a way
> tofindout the best fit for the equation for different degrees?
Try these functions. They handle any general multinomial
model for any number of parameters. I've included an
evaluator at the end.
HTH,
John D'Errico
% ================ begin matlab code =====================
function model=estimate_model(indvars,depvars,model_exp)
% builds and estimates a linear regression model with multiple inputs
%
% arguments:
% indvars - (n,p) array of independent variables (inputs)
% n is the number of data points, p is the number
% of variables
% depvars - (n,nout) array of outputs. Allows for multiple
% dependent variables
% model_exp - array of independent variable exponents
%
% for example, a model with two independent variables, x & y
% If the model has these terms: constant, x, y, xy, x^2
% use model_exp = [0 0;1 0;0 1;1 1;2 0]
[n,p]=size(indvars);
[m,nout]=size(depvars);
% data must be the same size.
if n~=m
error 'row dimension of indvars and depvars must be the same.'
end
[nterms,m]=size(model_exp);
if m~=p
error 'indvars and model_exp are inconsistent in size'
end
% create design matrix
mat=zeros(n,nterms);
for i=1:nterms
t=indvars(:,1).^model_exp(i,1);
for j=2:p
t=t.*(indvars(:,j).^model_exp(i,j));
end
mat(:,i)=t;
end
% do linear regression
coef=mat\depvars;
% stuff results into model structure
model.exponents=model_exp;
model.coefficients=coef;
% stuff this information into structure for later use.
% its not necessary, but its nice to do.
model.nout=nout;
model.numparams=p;
model.numterms=nterms;
% I suppose we could generate statistics on the fit, etc here ...
% ... On another day.
% ========================================================
function pred=evaluate_model(evaldata,model)
% evaluates a regression model stored in model.
%
% arguments:
% evaldata - (n,numparams) array contains data to be
% evaluated through model
% model - structure containing model as defined by estimate_model
[n,p]=size(evaldata);
if p~=model.numparams
error 'data is of the wrong size for this model.'
end
pred=zeros(n,model.nout);
for i=1:model.numterms
t=evaldata(:,1).^model.exponents(i,1);
for j=2:model.numparams
t=t.*(evaldata(:,j).^model.exponents(i,j));
end
pred=pred+t*model.coefficients(i,:);
end
% ================ end matlab code =====================
--
Hi I use another method like this:
function [predtrain,predtest]=npolyfit(trainc,trainIndex,testIndex,n, testc)
m = fullfact((n+1)*ones(1,length(trainIndex))) - 1;
m(sum(m,2)>n,:) = [];
predtrain=zeros(size(trainc,1),length(testIndex));
predtest=zeros(size(trainc,1),length(testIndex));
dtrain = x2fx(trainc(:,trainIndex),m);
dtest=x2fx(testc(:,trainIndex),m);
for i=1:length(testIndex),
[b]=regress(trainc(:,testIndex(i)),dtrain);
predtrain(:,i)=dtrain*b;
if (nargin>4)
predtest(:,i)=dtest*b;
end
end
end
Hi, I do this using my code: