Matlab various fitting

Author: Z-HE
link: https: //zhuanlan.zhihu.com/p/36103034
Source: know almost
copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

1) polyfit

Code examples below, fitting a cubic curve, and drawing.

x = 0:1:9;

y = [0 2 4 6 8 20 12 14 16 18]

A=polyfit(x,y,3);

z=polyval(A,x);

plot(x,y,'r*',x,z,'b')

 

1) lsqcurvefit nlinfit

Use lsqcurvefit (least squares fit) or nlinfit. The following example is a cubic curve fit. These two functions can also be fitted exponential functions, trigonometric functions and so on.

x=[0 1 2 3 4 5 6 7 8 9];

y = [0 1 2 3 4 5 + 10 6 7 8 9];% Here, a noise

w = [1 1 1 1 1 0.1 1 1 1 1]; we know that very low noise of heavy weights%

A0 = [1 1 1 1];

f=@(a,x)a(1)+a(2)*x+a(3)*x.*x+a(4)*x.*x.*x;

f1 = lsqcurvefit(f,a,x,y)

f2 = nlinfit(x,y,f,a0)

y1=f(f1,x)

y2=f(f2,x)

plot(x,y,'r*',x,y1,'b',x,y2,'g')

2)cftool

Note that the above fitting, is not entitled to re-fit. w is not functioning, you will find the fitted curve is greater impact noise. The following will be used to reduce noise cftool fitting:

Command line, type: cftool, Enter, will open the cftool window.

In the window, preferably the X data x, the Y data selected y, this case is automatically fitted to a straight line. Note that the important place came: the Weights selected as w, will find a straight line closer to the normal point!

The following article has reference to all types of explanation fit matlab cftool usage and its menu [at Plum homes _404 7]

3) using the fit note, this is the most important fit! cftool can achieve, achieve all-round here.

x=[0;1;2;3;4;5;6;7;8;9];

y = [0; 1; 2; 3; 4; 5 + 10; 6; 7; 8; 9];% Here, a noise

w = [1; 1; 1; 1; 1; 0.1; 1; 1; 1; 1];% We know that noise is very low weights

A0 = [1 1];

 

plot(x,y,'o')

fo = fitoptions('Method','NonlinearLeastSquares',...

'StartPoint',a0);

ft = fittype('a+x*b','options',fo);

[curve,gof] = fit(x,y,ft,'Weights',w)

hold on

plot(curve,'m')

legend('Data','curve')

hold off

See also:

Fit curve or surface to data

Fit type for curve and surface fitting

Create or modify fit options object

4) fit to use SmoothingSpline

x=[0;1;2;3;4;5;6;7;8;9];

y = [0; 1; 2; 3; 4; 5 + 10; 6; 7; 8; 9];% Here, a noise

w = [1; 1; 1; 1; 1; 0.2; 1; 1; 1; 1];% we know the weight of the low noise

A0 = [1 1];

plot(x,y,'o')

fo = fitoptions('method','SmoothingSpline','SmoothingParam',0.07,'Weights',w);

ft = fittype('smoothingspline');

f = fit(x,y,ft,fo)

y1= f(x)

hold on

plot(x,y1,'r')

legend('Data','f')

hold off

 

Unfortunately, the fitting code can not turn C ++

1) write a file fixSmoothingSpline.m, as follows, it is to make a weighted spline fitting,

function y1 = fixSmoothingSpline(x, y, w)%#codegen

f = fit(x,y,'SmoothingSpline','SmoothingParam',0.07,'Weights',w);

y1= f(x);

2) Input the command line coder, the coder transfer window,

3) However, since the above-mentioned document with the words fit, does not support turn C.

Guess you like

Origin www.cnblogs.com/yibeimingyue/p/12026275.html