0% found this document useful (0 votes)
497 views2 pages

MATLAB Nlinfit Example

This document demonstrates how to use MATLAB's nlinfit function to perform a nonlinear regression of data to a user-defined function. The data is fit to the function y = 1/(b1 + b2/x). MATLAB's nlinfit function takes the x and y data, the proposed functional form, and initial guesses for the parameters b1 and b2 to determine the best fit values for the coefficients. The output displays the estimated values for b1 and b2, and a graph shows the original data points in black and the regression curve in red.

Uploaded by

komadora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
497 views2 pages

MATLAB Nlinfit Example

This document demonstrates how to use MATLAB's nlinfit function to perform a nonlinear regression of data to a user-defined function. The data is fit to the function y = 1/(b1 + b2/x). MATLAB's nlinfit function takes the x and y data, the proposed functional form, and initial guesses for the parameters b1 and b2 to determine the best fit values for the coefficients. The output displays the estimated values for b1 and b2, and a graph shows the original data points in black and the regression curve in red.

Uploaded by

komadora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CHEN26210 Transport I

Chemical Engineering Department


T.D. Placek

Fall 2011
MATLAB nlinfit Example
Auburn University

MATLAB nlinfit Example


This example demonstrates how to use MATLABs nlinfit to perform a nonlinear
regression of data to a user defined function.
In this case, a set of x and y data is to be fit to the function:

y=

1 b2
+
b1 x

Note: this is similar to fitting data to a polynomial function using polyfit except
the user must provide the function whose coefficients are to be determined.
b0 represents the initial guesses for the parameters b1 and b2. In this case, they
are both assumed to be 0.5.
bhat (or in math notation,

b^

) are the parameter estimates (that is, the answers)

In the graph that is produced, black symbols are the original data and red symbols
are the results of the regression.
% nlinfit example (simplified)
% Tim Placek - [email protected]
% CHEN 2610 - Fall 2011
clear
clc
clf
format compact
% the data
x=2:16;
y=[6.42,8.2,9.58,9.5,9.7,10,9.93,9.99,...
10.49,10.59,10.6,10.8,10.6,10.9,10.76];
% the proposed functionality (fh is a handle to the function)
fh=@(b,x) 1./(b(1)+b(2)./x);
% guess values for parameters (beta0)
b0=[0.5,0.5];
% plot the raw data
plot(x,y,'s','markersize',5,'color',[0,0,0]);
hold on
% determine best fit values for coefficient (bhat)
bhat=nlinfit(x,y,fh,b0);
% plot the fit
xf = linspace(x(1), x(length(x)));
plot(xf,fh(bhat,xf),'linewidth',1,'color',[1,0,0]);
legend('original data','fit data','location','Best') % the result
bhat(1)
bhat(2)

Output:
ans =
0.0845
ans =
0.1152
Graph:
11
10.5
10
9.5
9
8.5
original data
fit data

8
7.5
7
6.5
6

10

12

14

16

You might also like