fittype

Fit type for curve and surface fitting

Syntax

ffun = fittype(libname)

ffun = fittype(expr)

ffun = fittype({expr1,...,exprn})

ffun = fittype(expr, Name, Value,...)

ffun= fittype({expr1,...,exprn}, Name, Value,...)

/*********************************** 线性拟合***********************************/

线性拟合公式:

coeff1 * term1 + coeff2 * term2 + coeff3 * term3 + ...

其中, coefficient 是系数, term 都是x 的一次项。

线性拟合Example :

Example1: y=kx+b;

法1:

[csharp] view plaincopy

1. x=[1,1.5,2,2.5,3];y=[0.9,1.7,2.2,2.6,3];

2. p=polyfit(x,y,1);   %

3. x1=linspace(min(x),max(x));

4. y1=polyval(p,x1);

5. plot(x,y, '*' ,x1,y1);

结果: p = 1.0200 0.0400

即y=1.0200 *x+ 0.0400

498fb82a2308892cb6881bd02e7935d7.png

法2:

[csharp] view plaincopy

1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];

2. p=fittype( 'poly1' )

3. f=fit(x,y,p)

4. plot(f,x,y);

运行结果:

[csharp] view plaincopy

1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];

2. p=fittype( 'poly1' )

3. f=fit(x,y,p)

4. plot(f,x,y);

5.

6. p =

7.

8. Linear model Poly1:

9. p(p1,p2,x) = p1*x + p2

10.

11. f =

12.

13. Linear model Poly1:

14. f(x) = p1*x + p2

15. Coefficients (with 95% confidence bounds):

16. p1 = 1.02 (0.7192, 1.321)

17. p2 = 0.04 (-0.5981, 0.6781)

79bfd09ec874def2fdaa3f3bb74342ce.png

Example2:y=a*x + b*sin(x) + c

法1:

[csharp] view plaincopy

1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];

2. EXPR = { 'x' , 'sin(x)' , '1' };

3. p=fittype(EXPR)

4. f=fit(x,y,p)

5. plot(f,x,y);

运行结果:

[csharp] view plaincopy

1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];

2. EXPR = { 'x' , 'sin(x)' , '1' };

3. p=fittype(EXPR)

4. f=fit(x,y,p)

5. plot(f,x,y);

6.

7. p =

8.

9. Linear model:

10. p(a,b,c,x) = a*x + b*sin(x) + c

11.

12. f =

13.

14. Linear model:

15. f(x) = a*x + b*sin(x) + c

16. Coefficients (with 95% confidence bounds):

17. a = 1.249 (0.9856, 1.512)

18. b = 0.6357 (0.03185, 1.24)

19. c = -0.8611 (-1.773, 0.05094)

法2:

[csharp] view plaincopy

1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];

2. p=fittype( 'a*x+b*sin(x)+c' , 'independent' , 'x' )

3. f=fit(x,y,p)

4. plot(f,x,y);

运行结果:

[csharp] view plaincopy

1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];

2. p=fittype( 'a*x+b*sin(x)+c' , 'independent' , 'x' )

3. f=fit(x,y,p)

4. plot(f,x,y);

5.

6. p =

7.

8. General model:

9. p(a,b,c,x) = a*x+b*sin(x)+c

10. Warning: Start point not provided, choosing random start

11. point.

12. > In fit>iCreateWarningFunction/nThrowWarning at 738

13. In fit>iFit at 320

14. In fit at 109

15.

16. f =

17.

18. General model:

19. f(x) = a*x+b*sin(x)+c

20. Coefficients (with 95% confidence bounds):

21. a = 1.249 (0.9856, 1.512)

22. b = 0.6357 (0.03185, 1.24)

23. c = -0.8611 (-1.773, 0.05094)

43dc185838622e9860b3d7a4ea7acde6.png

/*********************************** 非线性拟合***********************************/

Example :y=a*x^2+b*x+c

法1:

[cpp] view plaincopy

1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];

2. p=fittype( 'a*x.^2+b*x+c' , 'independent' , 'x' )

3. f=fit(x,y,p)

4. plot(f,x,y);

运行结果:

[csharp] view plaincopy

1. p =

2.

3. General model:

4. p(a,b,c,x) = a*x.^2+b*x+c

5. Warning: Start point not provided, choosing random start

6. point.

7. > In fit>iCreateWarningFunction/nThrowWarning at 738

8. In fit>iFit at 320

9. In fit at 109

10.

11. f =

12.

13. General model:

14. f(x) = a*x.^2+b*x+c

15. Coefficients (with 95% confidence bounds):

16. a = -0.2571 (-0.5681, 0.05386)

17. b = 2.049 (0.791, 3.306)

18. c = -0.86 (-2.016, 0.2964)

751151b9376ef22050c39aeee7c6b431.png

法2:

[csharp] view plaincopy

1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];

2. %use c=0;

3. c=0;

4. p1=fittype(@(a,b,x) a*x.^2+b*x+c)

5. f1=fit(x,y,p1)

6. %use c=1;

7. c=1;

8. p2=fittype(@(a,b,x) a*x.^2+b*x+c)

9. f2=fit(x,y,p2)

10. %predict c

11. p3=fittype(@(a,b,c,x) a*x.^2+b*x+c)

12. f3=fit(x,y,p3)

13.

14. %show results

15. scatter(x,y);%scatter point

16. c1=plot(f1, 'b:*' );%blue

17. hold on

18. plot(f2, 'g:+' );%green

19. hold on

20. plot(f3, 'm:*' );%purple

21. hold off

45eb2a3f51b0637d3464a96b3ab92ea3.png

更多推荐