MATLAB拟合曲线

今天想帮朋友做一个简单的数据拟合,然后画图,结果发现网上都没有现成的模板,只能自己摸索了,最后搜到了一个简单的方法,将多条曲线放到一个图上。
以下是数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
X1 =
0 0.8800
4.0000 1.0400
8.0000 1.0200
12.0000 1.1000
x1 =
0 4 8 12
y1 =
0.8800 1.0400 1.0200 1.1000
X2 =
0 2.1600
4.0000 3.0500
8.0000 3.2400
12.0000 3.9500
x2 =
0 4 8 12
y2 =
2.1600 3.0500 3.2400 3.9500
X3 =
0 4.2900
4.0000 3.2300
8.0000 2.8600
12.0000 3.3200
16.0000 4.7900
x3 =
0 4 8 12 16
y3 =
4.2900 3.2300 2.8600 3.3200 4.7900

注意画图的x与y都必须是行向量
首先做拟合,这个很简单,命令行中输入cftool就可以了,曲线类型选择smoothingspline平滑曲线,选择x1,y1,就可以得到第一条拟合曲线了,但是怎么样将多条拟合曲线画在一张图上呢,首先在cftool中选择fit->new fit,然后依次得到第二条与第三条拟合曲线,然后在file->generate code中得到画图的代码,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function [fitresult, gof] = createFits1(x1, y1, x2, y2, x3, y3)
%CREATEFITS1(X1,Y1,X2,Y2,X3,Y3)
% Create fits.
%
% Data for 'untitled fit 1' fit:
% X Input : x1
% Y Output: y1
% Data for 'untitled fit 3' fit:
% X Input : x2
% Y Output: y2
% Data for 'untitled fit 4' fit:
% X Input : x3
% Y Output: y3
% Output:
% fitresult : a cell-array of fit objects representing the fits.
% gof : structure array with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.

% Auto-generated by MATLAB on 12-Oct-2016 20:52:56

%% Initialization.

% Initialize arrays to store fits and goodness-of-fit.
fitresult = cell( 3, 1 );
gof = struct( 'sse', cell( 3, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );

%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( x1, y1 );

% Set up fittype and options.
ft = fittype( 'smoothingspline' );

% Fit model to data.
[fitresult{1}, gof(1)] = fit( xData, yData, ft );

% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult{1}, xData, yData );
legend( h, 'y1 vs. x1', 'untitled fit 1', 'Location', 'NorthEast' );
% Label axes
xlabel x1
ylabel y1
grid on

%% Fit: 'untitled fit 3'.
[xData, yData] = prepareCurveData( x2, y2 );

% Set up fittype and options.
ft = fittype( 'smoothingspline' );

% Fit model to data.
[fitresult{2}, gof(2)] = fit( xData, yData, ft );

% Plot fit with data.
figure( 'Name', 'untitled fit 3' );
h = plot( fitresult{2}, xData, yData );
legend( h, 'y2 vs. x2', 'untitled fit 3', 'Location', 'NorthEast' );
% Label axes
xlabel x2
ylabel y2
grid on

%% Fit: 'untitled fit 4'.
[xData, yData] = prepareCurveData( x3, y3 );

% Set up fittype and options.
ft = fittype( 'smoothingspline' );

% Fit model to data.
[fitresult{3}, gof(3)] = fit( xData, yData, ft );

% Plot fit with data.
figure( 'Name', 'untitled fit 4' );
h = plot( fitresult{3}, xData, yData );
legend( h, 'y3 vs. x3', 'untitled fit 4', 'Location', 'NorthEast' );
% Label axes
xlabel x3
ylabel y3
grid on

根据这一段代码其实就可以发现我们其实根本不需要打开cftool,直接写代码就可以得到拟合曲线,通过hold on就可以很轻易的将不同的曲线画在同一张图上了,具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
ft = fittype( 'smoothingspline' );
[fit1, gof] = fit( x1', y1', ft );
[fit2, gof] = fit( x2', y2', ft );
[fit3, gof] = fit( x3', y3', ft );
h=plot( fit1, x1, y1 );
set(h,'Color','g')
hold on
h1=plot( fit2, x2, y2 );
set(h1,'Color','r')
hold on
h2=plot( fit3, x3, y3 );
set(h2,'Color','b')

通过set更改曲线的颜色,注意Color要大写,小写只会改数据点的颜色
其他的设置像图例坐标轴标题什么的百度一下就好