From: Dan on
I am having trouble using "nlinfit" function for a fitting a
specific type of model to the data set. I would like to fit
a model of the form of:
y = alfa*x1 + beta*exp(-gamma*x2)
where x1 and x2 are the predictor variables and y is the
response, while alfa, beta and gamma are the coefficients to
be determined.

The help documentation on nlinfit seems to suggest that the
function can be used to fit a custom model but I am not able
to understand the syntax of the function calling (for the
model).

Has anyone come across a similar problem or has anyone
implemented nlinfit for their custom models? If yes, please
send in your suggestions.

Thanks,
db
From: Rik on
"Dan " <dbansal(a)gatech.edu> wrote in message
<fvrcbv$des$1(a)fred.mathworks.com>...
> I am having trouble using "nlinfit" function for a
fitting a
> specific type of model to the data set. I would like to
fit
> a model of the form of:
> y = alfa*x1 + beta*exp(-gamma*x2)
> where x1 and x2 are the predictor variables and y is the
> response, while alfa, beta and gamma are the
coefficients to
> be determined.
>
> The help documentation on nlinfit seems to suggest that
the
> function can be used to fit a custom model but I am not
able
> to understand the syntax of the function calling (for the
> model).
>
> Has anyone come across a similar problem or has anyone
> implemented nlinfit for their custom models? If yes,
please
> send in your suggestions.
>
> Thanks,
> db

%% nlinfit example
close all
clear all
%% model function
hModfun = @(par, x) par(1)*x(:,1) + par(2) * exp(-par(3) *x
(:,2));

%% simulate noisy data
x1 = 0:0.05:1; x1 = x1';
x2 = x1;
X = [x1, x2];
alpha = 1;
beta = 3;
gamma = 2;
par = [alpha, beta, gamma];

y = hModfun(par, X); y = y + 0.05 * y.*randn(size(y));
plot3(X(:,1), X(:,2), y, 'o-')

%% fit
par_ini = [0.01,0.01,0.01];
par_out = nlinfit(X, y, hModfun, par_ini)
% R2008: hModFun is cleared after calling nlinfit ?!!!!
hModFun = @(par, x) par(1)*x(:,1) + par(2) * exp(-par(3) *x
(:,2));
fit = hModFun(par_out, X);
hold on
plot3(X(:,1), X(:,2), fit, 'r')

From: Rik on
"Rik " <rdewijn(a)xs4all.nl> wrote in message <fvrmq9
$rvb$1(a)fred.mathworks.com>...
> "Dan " <dbansal(a)gatech.edu> wrote in message
> <fvrcbv$des$1(a)fred.mathworks.com>...
> > I am having trouble using "nlinfit" function for a
> fitting a
> > specific type of model to the data set. I would like
to
> fit
> > a model of the form of:
> > y = alfa*x1 + beta*exp(-gamma*x2)
> > where x1 and x2 are the predictor variables and y is
the
> > response, while alfa, beta and gamma are the
> coefficients to
> > be determined.
> >
> > The help documentation on nlinfit seems to suggest
that
> the
> > function can be used to fit a custom model but I am
not
> able
> > to understand the syntax of the function calling (for
the
> > model).
> >
> > Has anyone come across a similar problem or has anyone
> > implemented nlinfit for their custom models? If yes,
> please
> > send in your suggestions.
> >
> > Thanks,
> > db
>
> %% nlinfit example
> close all
> clear all
> %% model function
> hModfun = @(par, x) par(1)*x(:,1) + par(2) * exp(-par(3)
*x
> (:,2));
>
> %% simulate noisy data
> x1 = 0:0.05:1; x1 = x1';
> x2 = x1;
> X = [x1, x2];
> alpha = 1;
> beta = 3;
> gamma = 2;
> par = [alpha, beta, gamma];
>
> y = hModfun(par, X); y = y + 0.05 * y.*randn(size(y));
> plot3(X(:,1), X(:,2), y, 'o-')
>
> %% fit
> par_ini = [0.01,0.01,0.01];
> par_out = nlinfit(X, y, hModfun, par_ini)
> % R2008: hModFun is cleared after calling
nlinfit ?!!!!
> hModFun = @(par, x) par(1)*x(:,1) + par(2) * exp(-par(3)
*x
> (:,2));
> fit = hModFun(par_out, X);
> hold on
> plot3(X(:,1), X(:,2), fit, 'r')
>

Alternatively, you can define the model function in a
separate m-file (available on the matlab path):

function y = modFun(par, x)
y = par(1)*x(:,1) + par(2) * exp(-par(3));
From: Peter Perkins on
Rik wrote:

> par_out = nlinfit(X, y, hModfun, par_ini)
> % R2008: hModFun is cleared after calling nlinfit ?!!!!
> hModFun = @(par, x) par(1)*x(:,1) + par(2) * exp(-par(3) *x(:,2));
> fit = hModFun(par_out, X);

Rik, I think you've just got a typo in the name: "hModfun" vs. "hModFun".
From: Rik on
Peter Perkins <Peter.PerkinsRemoveThis(a)mathworks.com>
wrote in message <fvsduu$8e0$1(a)fred.mathworks.com>...
> Rik wrote:
>
> > par_out = nlinfit(X, y, hModfun, par_ini)
> > % R2008: hModFun is cleared after calling
nlinfit ?!!!!
> > hModFun = @(par, x) par(1)*x(:,1) + par(2) * exp(-par
(3) *x(:,2));
> > fit = hModFun(par_out, X);
>
> Rik, I think you've just got a typo in the
name: "hModfun" vs. "hModFun".

yes, oops, thanks.