least squares fitting nonlinear function and its matlab/excel implementation

Source: Internet
Author: User

1, least squares principle

Matlab directly implements the least squares example:

Closex = 1:1:100;a = -1.5;b = -10;y = A*log (x) +b;yrand = y + 0.5*rand (1,size (y,2)); min. least squares fitting Xf=log (x); yf=yrand;xfa = [Ones ( 1,size (xf,2)); xf]w = INV (XFA*XFA ') *XFA*YF ';% Direct fit Results

Resources:

1, http://blog.csdn.net/lotus___/article/details/20546259

2, http://blog.sina.com.cn/s/blog_5404ea4f0101afth.html

2. Matlab Call function to realize least squares

By using the least squares fitting function of Matlab to fit the nonlinear function, the function is fitted specifically:

[Q r] = Lsqcurvefit (fun, Q_0, XData, Ydata);
Input parameters:
Fun: A function that needs to be fitted, assuming that there are n parameters to fit, q = [q1,q2,..., qn]
Q_0: Represents a given starting point for a user
XData: Arguments to functions
Ydata: The dependent variable of a function
Output parameters:
Q: The optimal parameters of the solution are obtained
R: The value of the target function that represents the least squares, that is, the residuals.

Implementation code

 closex = 1:1:100;a = -1.5;b = -10;y = A*log (×) +b;plot (x, y), Yrand = y + 0.5*rand (1,size (y,2));p lot (X,yrand, ' ro '); Multiply-fit xf=log (x); Yf=yrand;f=inline (' A (1) +a (2). *x ', ' a ', ' X '); [Q,r]=lsqcurvefit (F,[1,0],XF,YF) plot (X,yrand, ' ro ', ' linewidth ', 2)% draw chart hold on;%% draw fit curve yn = q (1) +q (2) *log (x); Hold on ;p lot (X,yn, ' B ', ' linewidth ', 2); percent set Legendhleg = Legend ([' Primitive function (y= ' num2str (b,3) ' + ' Num2str (a,3) ' ln (x) ') Data '],[' Fitting results: y= ' Num2str (q (1), 3) ' + ' Num2str (q (2), 3) ' ln (x) '], ' location ', ' northeast '), the% itself cannot set the size of the font, it needs to be set (Hleg, '     FontSize ', ' fontangle ', ' Italic ', ' fontweight ', ' bold ',... ' TextColor ', [. 6,.2,.1], ' color ', [1,1,1]);%color sets the caption title for the background color of the coordinates (' least squares fit: y=a+b*ln (x) ', ' Color ', ' k ', ' FontSize  ', 20); The percent Axis title is set Xlabel (' x ', ' Color ', ' k ', ' FontSize ', 15);% horizontal axis title Ylabel (' Y ', ' color ', ' k ', ' FontSize ', 15);% vertical axis Title percent save Image Set (1, ' Inverthardcopy ', ' off '); the background color of the% setting is valid, if the graphic does not save the background color for on, maltab defaults to Onfilename = ' Lnx ';p rint (1, '-djpeg ', filename);% Other formats- Djpeg,-dpng,-dbmp,-dtiff,-dgif 

Accessories: Least Squares Matlab.zip

Fitting results

As shown

3, the implementation of Excel

Using the Lenest function for least squares fitting, the above data are fitted, and the fitting results are consistent with MATLAB.

If the fitted function y=ax+b is known, then

The slope calculation formula is:

A = Lenest (Ydata,xdata,,false)

The Intercept calculation formula is:

b = Index (Lenest (ydata,xdata), FALSE), 2)

To fit the code, please refer to the attachment: least squares fitting. xlsx, which contains raw data with noise, fits the formula, fits the result diagram

The fitting results are as follows:

Http://www.cnblogs.com/cv-pr/p/4741262.html

least squares fitting nonlinear function and its Matlab/excel implementation (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.