Examples of MATLAB: linear curve fit
Author: Kailugaji - blog Park http://www.cnblogs.com/kailugaji/
By least squares fitting the nonlinear curve, two methods are given: (1) specified nonlinear function, (2) fitting curve of the Fourier function
1. MATLAB program
clear clc xdata=[0.1732;0.1775;0.1819;0.1862;0.1905;0.1949;0.1992;0.2035;0.2079;0.2122;0.2165;0.2208;0.2252;0.2295;0.2338;0.2384]; ydata = [- 3.41709; -4.90887; -6.09424; -6.95362; -7.63729; -8.12466; -8.37153; -8.55049; -8.61958; -8.65326; -8.60021; -8.52824; -8.43502; -8.32234; -8.20419; -8.04472 ]? %% specified nonlinear function fitting curve X0=[1 1]; [Parameter, resnorm] = lsqcurvefit (@ fun, X0, xdata, ydata);% specifying fit curve A=parameter(1); B=parameter(2); fprintf ( 'curve fitting parameters A Lennard-Jones potential function is:% 8f, B is:..% 8f', A, B); fit_y=fun(parameter,xdata); figure(1) plot (xdata, ydata, 'r.') hold on plot(xdata,fit_y,'b-') xlabel('r/nm'); ylabel('Fe-C Ec/eV'); xlim ([00:17 0:24]); Legend ( 'observations point', 'Fit Curve') % legend('boxoff') saveas(gcf,sprintf('Lennard-Jones.jpg'),'bmp'); % print(gcf,'-dpng','Lennard-Jones.png'); %% fitting curve of the Fourier function figure(2) [fit_fourier,gof]=fit(xdata,ydata,'Fourier2') plot (fit_fourier, xdata, ydata) xlabel('r/nm'); ylabel('Fe-C Ec/eV'); xlim ([00:17 0:24]); saveas(gcf,sprintf('demo_Fourier.jpg'),'bmp'); % print(gcf,'-dpng','demo_Fourier.png');
function f=fun(X,r) f=X(1)./(r.^12)-X(2)./(r.^6);
2. Results
Curve fitting Lennard-Jones potential as a function of the parameters A: 0.00000003, B is: 0.00103726 fit_fourier = General model Fourier2: fit_fourier(x) = a0 + a1*cos(x*w) + b1*sin(x*w) + a2*cos(2*x*w) + b2*sin(2*x*w) Coefficients (with 95% confidence bounds): a0 = 79.74 (-155, 314.5) ? 1 = 112.9 (-262.1, 487.9) b1 = 28.32 (-187.9, 244.6) a2 = 24.5 (-114.9, 163.9) b2 = 13.99 (-75.89, 103.9) w = 15.05 (3.19, 26.9) gof = It contains the following fields of struct: beg: 0.0024 rsquare: 0.9999 dfe: 10 adjrsquare: 0.9999 rmse: 0.0154
Potential Fig 1. Lennard-Jones curve fitting function
Fig 2. Fourier function fitting curve