www.gusucode.com > stats 源码程序 matlab案例代码 > stats/NonlinearModelExample.m

    %% Fit a Nonlinear Regression Model  
% Fit a nonlinear regression model for auto mileage based on the |carbig| 
% data. Predict the mileage of an average car.

%% 
% Load the sample data. Create a matrix |X| containing the measurements for
% the horsepower (|Horsepower|) and weight (|Weight|) of each car. Create a
% vector |y| containing the response values in miles per gallon (|MPG|).
load carbig
X = [Horsepower,Weight];
y = MPG;

%%
% Fit a nonlinear regression model.
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)  

%% 
% Find the predicted mileage of an average auto. Since the sample data
% contains some missing (|NaN|) observations, compute the mean using |nanmean|.
Xnew = nanmean(X)  
MPGnew = predict(mdl,Xnew)