Laurengineer
Laurengineer

Reputation: 747

MATLAB's nonlinear regression

I've recently been attempting a nonlinear regression in MATLAB and am unsure how to go about it.

I've attempted use of MATLAB's help pages and used this one

However, even if I just copy and paste the example (under "nonlinear model from matrix data")

I get this error:

Undefined function 'fitnlm' for input arguments of type 'function_handle'.

What I've tried is:

load carbig
X = [Horsepower,Weight];
y = MPG;
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + b(4)*x(:,2).^b(5);
beta0 = [-50 500 -1 500 -1];
mdl = fitnlm(X,y,modelfun,beta0)

But it doesn't seem to like the modelfun part

I'm wondering why it has issues, if this is their example?

Upvotes: 1

Views: 6316

Answers (2)

Laurengineer
Laurengineer

Reputation: 747

I think with my version of matlab fitnlm doesn't exist so I have to use NonLinearModel.fit

Just tried it and it seems to work okay:

load carbig
X = [Horsepower,Weight];
y = MPG;
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + b(4)*x(:,2).^b(5);
beta0 = [-50 500 -1 500 -1];
mdl = NonLinearModel.fit(X,y,modelfun,beta0)

Thanks for your help!

Upvotes: 2

Nick
Nick

Reputation: 3193

You need to install Matlab's Statistics Toolbox to use fitnlm. As it can be seen from the list of functions provided by the toolbox. As you haven't installed it, it will not find the function on its path and therefore run into the above error.

fitnlm is introduced in matlab 2013b and statistics toolbox 8.3.

Upvotes: 3

Related Questions