资源描述
(word完整版)Matlab线性回归(拟合)
Matlab线性回归(拟合)
对于多元线性回归模型:
设变量的n组观测值为
.
记 ,,则 的估计值为
(11。2)
在Matlab中,用regress函数进行多元线性回归分析,应用方法如下:
语法:b = regress(y, x)
[b, bint, r, rint, stats] = regress(y, x)
[b, bint, r, rint, stats] = regress(y, x, alpha)
b = regress(y, x),得到的维列向量b即为(11。2)式给出的回归系数的估计值.
[b, bint, r, rint, stats]=regress(y, x) 给出回归系数的估计值b,的95%置信区间(向量)bint,残差r以及每个残差的95%置信区间(向量)rint;向量stats给出回归的R2统计量和F以及临界概率p的值.
如果的置信区间(bint的第行)不包含0,则在显著水平为时拒绝的假设,认为变量是显著的.
[b, bint, r, rint, stats]=regress(y, x, alpha) 给出了bint和rint的100(1—alpha)%的置信区间.
三次样条插值函数的MATLAB程序
matlab的spline
x = 0:10; y = sin(x); %插值点
xx = 0:.25:10; %绘图点
yy = spline(x,y,xx);
plot(x,y,'o’,xx,yy)
非线性拟合
非线性拟合可以用以下命令(同样适用于线形回归分析):
1。beta = nlinfit(X,y,fun,beta0)
X给定的自变量数据,Y给定的因变量数据,fun要拟合的函数模型(句柄函数或 者内联函数形式),
beta0函数模型中系数估计初值,beta返回拟合后的系数
2.x = lsqcurvefit(fun,x0,xdata,ydata)
fun要拟合的目标函数,x0目标函数中的系数估计初值,xdata自变量数据,ydata函数值数据
X拟合返回的系数(拟合结果)
nlinfit
格式: [beta,r,J]=nlinfit(x,y,’model’, beta0)
Beta 估计出的回归系数
r 残差
J Jacobian矩阵
x,y 输入数据x、y分别为n*m矩阵和n维列向量,对一元非线性回归,x为n维列向量.
model是事先用m-文件定义的非线性函数
beta0 回归系数的初值
例1已知数据:
x1=[0。5,0。4,0.3,0.2,0。1];
x2=[0.3,0.5,0。2,0.4,0。6];
x3=[1。8,1。4,1.0,1。4,1。8];
y=[0。785,0.703,0.583,0。571,0.126]’;
且y与x1,x2 , x3关系为多元非线性关系(只与x2,x3相关)为:
y=a+b*x2+c*x3+d*(x2。^2)+e*(x3。^2)
求非线性回归系数a , b , c , d , e 。
(1)对回归模型建立M文件model。m如下:
function yy=myfun(beta,x)
x1=x(:,1);
x2=x(:,2);
x3=x(:,3);
yy=beta(1)+beta(2)*x2+beta(3)*x3+beta(4)*(x2.^2)+beta(5)*(x3.^2);
(2)主程序如下:
x=[0.5,0。4,0.3,0.2,0.1;0.3,0。5,0。2,0.4,0.6;1.8,1.4,1。0,1。4,1。8]’;
y=[0.785,0。703,0.583,0。571,0.126]';
beta0=[1,1, 1,1, 1]';
[beta,r,j] = nlinfit(x,y,@myfun,beta0)
例题2:混凝土的抗压强度随养护时间的延长而增加,现将一批混凝土作成12个试块,记录了养护日期(日)及抗压强度y(kg/cm2)的数据:
养护时间:x =[2 3 4 5 7 9 12 14 17 21 28 56 ]
抗压强度:y =[35+r 42+r 47+r 53+r 59+r 65+r 68+r 73+r 76+r 82+r 86+r 99+r ]
建立非线性回归模型,对得到的模型和系数进行检验。
注明:此题中的+r代表加上一个[-0。5,0.5]之间的随机数
模型为:y=a+k1*exp(m*x)+k2*exp(-m*x);
Matlab程序:
x=[2 3 4 5 7 9 12 14 17 21 28 56];
r=rand(1,12)-0。5;
y1=[35 42 47 53 59 65 68 73 76 82 86 99];
y=y1+r ;
myfunc=inline(’beta(1)+beta(2)*exp(beta(4)*x)+beta(3)*exp(—beta(4)*x)’,’beta’,'x');
beta=nlinfit(x,y,myfunc,[0。5 0.5 0.5 0.5]);
a=beta(1),k1=beta(2),k2=beta(3),m=beta(4)
%test the model
xx=min(x):max(x);
yy=a+k1*exp(m*xx)+k2*exp(—m*xx);
plot(x,y,’o',xx,yy,’r’)
结果:
a = 87.5244
k1 = 0。0269
k2 = —63.4591
m = 0。1083
图形:
lsqnonlin
非线性最小二乘(非线性数据拟合)的标准形式为
其中:L为常数
在MATLAB5。x中,用函数leastsq解决这类问题,在6。0版中使用函数lsqnonlin。
设
则目标函数可表达为
其中:x为向量,F(x)为函数向量。
函数 lsqnonlin
格式 x = lsqnonlin(fun,x0) %x0为初始解向量;fun为,i=1,2,…,m,fun返回向量值F,而不是平方和值,平方和隐含在算法中,fun的定义与前面相同.
x = lsqnonlin(fun,x0,lb,ub) %lb、ub定义x的下界和上界:.
x = lsqnonlin(fun,x0,lb,ub,options) %options为指定优化参数,若x没有界,则lb=[ ],ub=[ ]。
[x,resnorm] = lsqnonlin(…) % resnorm=sum(fun(x).^2),即解x处目标函数值。
[x,resnorm,residual] = lsqnonlin(…) % residual=fun(x),即解x处fun的值.
[x,resnorm,residual,exitflag] = lsqnonlin(…) %exitflag为终止迭代条件。
[x,resnorm,residual,exitflag,output] = lsqnonlin(…) %output输出优化信息。
[x,resnorm,residual,exitflag,output,lambda] = lsqnonlin(…) %lambda为Lagrage乘子。
[x,resnorm,residual,exitflag,output,lambda,jacobian] =lsqnonlin(…) %fun在解x处的Jacobian矩阵.
例5—17 求下面非线性最小二乘问题初始解向量为x0=[0.3, 0。4].
解:先建立函数文件,并保存为myfun。m,由于lsqnonlin中的fun为向量形式而不是平方和形式,因此,myfun函数应由建立:
k=1,2,…,10
function F = myfun(x)
k = 1:10;
F = 2 + 2*k—exp(k*x(1))-exp(k*x(2));
然后调用优化程序:
x0 = [0。3 0.4];
[x,resnorm] = lsqnonlin(@myfun,x0)
结果为:
Optimization terminated successfully:
Norm of the current step is less than OPTIONS。TolX
x =
0.2578 0。2578
resnorm = %求目标函数值
lsqcurvefit
非线性曲线拟合是已知输入向量xdata和输出向量ydata,并且知道输入与输出的函数关系为ydata=F(x, xdata),但不知道系数向量x.今进行曲线拟合,求x使得下式成立:
在MATLAB5.x中,使用函数curvefit解决这类问题。
函数 lsqcurvefit
格式 x = lsqcurvefit(fun,x0,xdata,ydata)
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
[x,resnorm] = lsqcurvefit(…)
[x,resnorm,residual] = lsqcurvefit(…)
[x,resnorm,residual,exitflag] = lsqcurvefit(…)
[x,resnorm,residual,exitflag,output] = lsqcurvefit(…)
[x,resnorm,residual,exitflag,output,lambda] = lsqcurvefit(…)
[x,resnorm,residual,exitflag,output,lambda,jacobian] =lsqcurvefit(…)
参数说明:
x0为初始解向量;xdata,ydata为满足关系ydata=F(x, xdata)的数据;
lb、ub为解向量的下界和上界,若没有指定界,则lb=[ ],ub=[ ];
options为指定的优化参数;
fun为拟合函数,其定义方式为:x = lsqcurvefit(@myfun,x0,xdata,ydata),
其中myfun已定义为 function F = myfun(x,xdata)
F = … % 计算x处拟合函数值fun的用法与前面相同;
resnorm=sum ((fun(x,xdata)-ydata).^2),即在x处残差的平方和;
residual=fun(x,xdata)—ydata,即在x处的残差;
exitflag为终止迭代的条件;
output为输出的优化信息;
lambda为解x处的Lagrange乘子;
jacobian为解x处拟合函数fun的jacobian矩阵。
例5—16 求解如下最小二乘非线性拟合问题
已知输入向量xdata和输出向量ydata,且长度都是n,拟合函数为
即目标函数为
其中:
初始解向量为x0=[0.3, 0.4, 0。1]。
解:先建立拟合函数文件,并保存为myfun。m
function F = myfun(x,xdata)
F = x(1)*xdata。^2 + x(2)*sin(xdata) + x(3)*xdata.^3;
然后给出数据xdata和ydata
>>xdata = [3.6 7.7 9。3 4.1 8。6 2.8 1.3 7.9 10。0 5。4];
>〉ydata = [16.5 150.6 263。1 24。7 208。5 9。9 2。7 163.9 325.0 54。3];
〉>x0 = [10, 10, 10]; %初始估计值
>〉[x,resnorm] = lsqcurvefit(@myfun,x0,xdata,ydata)
结果为:
Optimization terminated successfully:
Relative function value changing by less than OPTIONS.TolFun
x =
0.2269 0.3385 0.3021
resnorm =
6.2950
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
31
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
32
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
33
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
34
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
35
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
36
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
37
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
39
39
39
39
39
39
39
39
39
39
39
39
39
39
39
39
39
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
40
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
45
46
46
46
46
46
46
46
46
46
47
47
47
47
47
47
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
48
50
50
50
50
51
53
53
53
54
54
54
54
54
54
55
55
55
56
56
56
57
57
57
57
57
58
58
59
59
61
62
62
62
62
62
63
64
64
64
64
64
64
66
67
68
68
68
70
72
72
72
72
72
72
72
72
73
73
73
73
73
74
74
74
74
74
74
74
74
74
75
75
75
75
75
76
76
76
76
76
76
76
76
76
76
76
76
76
76
76
76
76
76
76
76
76
77
77
77
77
77
77
77
77
77
77
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
78
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
80
81
81
81
81
81
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
82
83
83
83
83
83
83
83
83
83
83
83
83
83
83
83
83
83
83
83
83
84
84
84
84
84
84
84
84
84
85
85
85
85
85
85
85
85
85
85
86
86
86
86
86
86
86
86
86
86
86
86
86
86
86
87
87
87
87
87
87
87
87
87
87
87
87
87
87
88
88
88
88
88
88
88
88
88
88
88
88
88
88
88
88
88
89
89
89
89
89
89
89
89
89
89
89
89
89
89
89
89
89
90
90
90
91
91
91
91
91
91
91
91
91
91
91
91
91
91
91
91
92
92
92
92
92
92
92
92
92
92
92
92
92
92
92
92
93
93
93
93
93
93
93
93
93
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
94
95
96
96
96
96
96
96
96
96
96
96
96
97
97
97
98
98
98
98
98
99
99
99
99
99
99
100
100
100
100
100
100
100
100
101
101
101
101
101
101
101
101
101
101
101
101
101
101
101
101
101
101
101
101
101
102
102
103
103
103
103
103
104
104
104
104
104
104
104
104
104
104
105
105
105
105
105
105
105
105
105
105
105
105
105
105
105
105
106
106
106
106
107
107
107
107
107
107
107
107
107
108
108
108
108
108
108
108
108
108
108
108
108
109
109
109
109
109
109
109
109
110
110
110
110
110
110
111
111
111
111
111
111
111
111
111
111
111
111
111
111
112
112
113
113
113
113
113
114
114
114
115
115
115
115
115
115
116
116
116
117
117
117
117
117
117
117
117
118
119
119
119
119
120
120
120
120
120
120
121
121
121
121
121
121
121
121
121
122
122
122
122
122
123
123
123
123
123
123
123
124
124
124
125
125
125
125
126
127
127
127
127
127
127
127
129
129
129
129
129
129
129
129
129
129
129
130
130
130
130
131
132
132
132
132
132
132
132
132
132
132
133
134
134
134
134
134
134
134
134
134
135
135
135
135
135
135
135
135
135
135
135
135
135
136
136
136
136
136
136
136
136
137
137
138
138
138
138
138
138
138
138
138
138
138
139
139
139
140
140
140
140
140
140
141
141
141
141
141
141
141
141
141
141
142
142
143
143
143
143
143
144
144
144
144
144
144
144
144
144
144
144
144
144
144
144
144
145
145
145
145
146
147
147
147
147
148
148
148
148
148
148
149
149
150
150
150
150
150
150
151
151
151
151
151
151
152
152
152
152
152
152
152
152
153
153
154
154
154
154
154
154
155
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
157
157
157
157
157
157
157
157
158
158
158
158
158
158
158
158
159
159
159
159
159
159
160
160
160
160
160
160
161
161
161
162
162
162
163
164
164
164
164
164
164
164
164
165
165
165
165
165
165
165
166
166
166
167
167
167
167
167
168
168
168
168
168
168
168
168
169
169
171
171
171
173
173
173
174
175
175
176
176
176
176
176
176
176
176
176
177
177
177
177
177
177
177
177
177
178
179
179
179
179
179
179
179
179
179
179
179
179
180
180
180
180
180
182
182
182
182
183
183
183
185
185
186
186
187
188
188
188
188
188
188
188
188
188
188
188
188
188
188
188
188
189
189
189
189
189
189
191
191
192
192
192
192
192
193
195
195
195
195
195
195
195
195
195
195
195
195
195
195
196
196
196
196
197
197
197
197
197
197
197
197
197
197
197
198
198
199
200
200
202
202
203
204
204
204
205
205
205
205
205
205
205
205
206
206
206
206
206
206
207
207
207
207
214
214
214
214
214
214
214
214
214
214
215
215
215
215
215
216
216
216
216
216
216
216
216
216
216
216
216
216
216
216
216
217
217
217
217
217
217
217
217
217
217
217
217
217
217
217
219
219
219
219
220
220
220
220
221
221
222
222
222
222
223
223
224
224
224
224
224
224
224
225
225
225
225
225
226
226
229
229
229
229
229
229
229
229
230
230
230
230
230
230
230
230
231
231
233
233
234
235
235
235
235
235
235
235
236
237
237
237
238
238
238
238
238
238
238
238
238
238
238
238
238
238
238
238
238
238
238
240
240
240
240
241
241
241
241
241
242
243
244
244
245
245
245
245
245
245
245
245
245
245
247
247
247
247
247
247
247
247
247
247
247
247
247
展开阅读全文