这是线性回归的程序:
A=[...
1.2424 1 200 -0.0000850 400 400 200 -0.0532630
1.2424 1 350 -2.7112000 400 400 350 0.9908900
1.2424 1 500 0.3282800 400 400 500 1.2144000
1.2424 400 200 0.0027129 600 1 200 -0.7455400
1.2424 400 350 1.0997000 600 1 350 -0.5715200
1.2424 400 500 2.7984000 600 1 500 -0.4054000
200 1 200 -0.6427100 600 400 200 1.9441000
200 1 350 2.0863000 600 400 350 0.7021700
200 1 500 -0.4607700 600 400 500 0.6903000
200 400 200 3.4253000 800 1 200 -6.4449000
200 400 350 4.8946000 800 1 350 -2.2801000
200 400 500 2.4998000 800 1 500 0.1483200
400 1 200 -2.2702000 800 400 200 -3.7869000
400 1 350 -0.8187100 800 400 350 0.2750100
400 1 500 -0.6684200 800 400 500 0.0515480]
B=[A(:,1:4)A(:,5:end)]
x=B(:,1:3),y=B(:,4)
format short g
X1=[ones(length(y),1),x]
B1=regress(y,X1)
[m,n]=size(x)
X2=[]
for i=1:n
for j=1:n
if i<=j
X2=([X2,x(:,i).*x(:,j)])
else
continue
end
end
end
X=[X1,X2]
B2=regress(y,X)
[y X*B2 y-X*B2]
plot(y,X1*B1,'o',y,X*B2,'*')
hold on,line([min(y),max(y)],[min(y),max(y)])
axis([min(y) max(y) min(y) max(y)])
C=[...
1.2424 391 129.3769 -0.1019500
200 560 600 0.2769700
400 447 200 -0.1765000
800 234 500 0.2896000
1.2424 250 129.3769 1.3258000
200 300 129.376 0.8630000
200 100 200 0.8914800]
test=[[ones(length(C),1),C(:,1:3)]*B1,C(:,4)]
结果:
A =
1.24241 200-8.5e-005 400 400 200-0.053263
1.24241 350 -2.7112 400 400 350 0.99089
1.24241 500 0.32828 400 400 500 1.2144
1.2424 400 2000.0027129 6001 200 -0.74554
1.2424 400 350 1.0997 6001 350 -0.57152
1.2424 400 500 2.7984 6001 500 -0.4054
2001 200 -0.64271 600 400 200 1.9441
2001 350 2.0863 600 400 350 0.70217
2001 500 -0.46077 600 400 500 0.6903
200 400 200 3.4253 8001 200 -6.4449
200 400 350 4.8946 8001 350 -2.2801
200 400 500 2.4998 8001 500 0.14832
4001 200 -2.2702 800 400 200 -3.7869
4001 350 -0.81871 800 400 350 0.27501
4001 500 -0.66842 800 400 500 0.051548
B =
1.24241 200-8.5e-005
1.24241 350 -2.7112
1.24241 500 0.32828
1.2424 400 2000.0027129
1.2424 400 350 1.0997
1.2424 400 500 2.7984
2001 200 -0.64271
2001 350 2.0863
2001 500 -0.46077
200 400 200 3.4253
200 400 350 4.8946
200 400 500 2.4998
4001 200 -2.2702
4001 350 -0.81871
4001 500 -0.66842
400 400 200-0.053263
400 400 350 0.99089
400 400 500 1.2144
6001 200 -0.74554
6001 350 -0.57152
6001 500 -0.4054
600 400 200 1.9441
600 400 350 0.70217
600 400 500 0.6903
8001 200 -6.4449
8001 350 -2.2801
8001 500 0.14832
800 400 200 -3.7869
800 400 350 0.27501
800 400 500 0.051548
x =
1.24241 200
1.24241 350
1.24241 500
1.2424 400 200
1.2424 400 350
1.2424 400 500
2001 200
2001 350
2001 500
200 400 200
200 400 350
200 400 500
4001 200
4001 350
4001 500
400 400 200
400 400 350
400 400 500
6001 200
6001 350
6001 500
600 400 200
600 400 350
600 400 500
8001 200
8001 350
8001 500
800 400 200
800 400 350
800 400 500
y =
-8.5e-005
-2.7112
0.32828
0.0027129
1.0997
2.7984
-0.64271
2.0863
-0.46077
3.4253
4.8946
2.4998
-2.2702
-0.81871
-0.66842
-0.053263
0.99089
1.2144
-0.74554
-0.57152
-0.4054
1.9441
0.70217
0.6903
-6.4449
-2.2801
0.14832
-3.7869
0.27501
0.051548
X1 =
1 1.24241 200
1 1.24241 350
1 1.24241 500
1 1.2424 400 200
1 1.2424 400 350
1 1.2424 400 500
1 2001 200
1 2001 350
1 2001 500
1 200 400 200
1 200 400 350
1 200 400 500
1 4001 200
1 4001 350
1 4001 500
1 400 400 200
1 400 400 350
1 400 400 500
1 6001 200
1 6001 350
1 6001 500
1 600 400 200
1 600 400 350
1 600 400 500
1 8001 200
1 8001 350
1 8001 500
1 800 400 200
1 800 400 350
1 800 400 500
B1 =
-1.5119
-0.0031153
0.005381
0.0049226
m =
30
n =
3
Warning: X is rank deficient to within machine precision.
>In regress at 82
In try24 at 34
B2 =
-3.5118
0.00071381
0
0.016309
-9.3052e-006
-2.9789e-006
1.2068e-005
2.1238e-005
-5.5521e-006
-2.1576e-005
ans =
-8.5e-005 -1.1103 1.1102
-2.7112 -0.44256 -2.2686
0.32828 -0.74579 1.0741
0.0027129 1.8433 -1.8406
1.0997 2.1787 -1.079
2.7984 1.5432 1.2552
-0.64271 -0.86144 0.21873
2.0863 0.16607 1.9202
-0.46077 0.22264 -0.68341
3.4253 1.8559 1.5694
4.8946 2.5511 2.3435
2.4998 2.2754 0.22441
-2.2702 -1.3532 -0.91704
-0.81871 0.036396 -0.85511
-0.66842 0.45503 -1.1234
-0.053263 1.1265 -1.1797
0.99089 2.1837 -1.1928
1.2144 2.2701 -1.0557
-0.74554 -2.5893 1.8438
-0.57152 -0.8377 0.26618
-0.4054-0.057013 -0.34839
1.9441 -0.34741 2.2915
0.70217 1.0719 -0.36974
0.6903 1.5203-0.83
-6.4449 -4.5699 -1.875
-2.2801 -2.4562 0.17611
0.14832 -1.3135 1.4618
-3.7869 -2.5657 -1.2212
0.27501 -0.78432 1.0593
0.051548 0.026126 0.025422
C =
1.2424 391 129.38 -0.10195
200 560 600 0.27697
400 447 200 -0.1765
800 234 500 0.2896
1.2424 250 129.38 1.3258
200 300 129.380.863
200 100 200 0.89148
test =
1.2251 -0.10195
3.832 0.27697
0.63187 -0.1765
-0.28359 0.2896
0.46639 1.3258
0.116250.863
-0.6123 0.89148
可以用函数 regress( )来解决。[b,bint,r,rint,stats] = regress(y,X)
b——拟合线性函数的系数
bint——碰物系数b的置信区间
r——残值向量
rint——残值的置信区间
stats——检验统计量,第一值是回归方程的置信度,第二值是F统计量,第团吵液三值是与F统计量相应的p值,当p值很小,说明回塌物归模型成立
X——自变量向量,X=[ones(3,1) x1 x2 x3]
y——应变量向量
matlab程序如下:g=[0.86,0.80,0.62,0.52,0.44]
%加速度数组为g
m=[147.9,
197.9,
247.9,297.9,347.9]%对应质量数组m
corrc=corrcoef(m,g)
%相关系数计算,-0.9881,说明强负相关
result=polyfit(g,m,1)
%最小二乘法回归m=ag+b,系数a,b在result中
cal_m=result(1)*g+result(2)%根据线性回归计算得到的质量cal_m
plot(g,m,'*',g,cal_m,'-')
%画实测和线性拟合比较图,直线是拟合方程,点是实测嫌激
xlabel('加速度
(m/s^2)')
%
x
y
轴定义
ylabel('质量(g)')
图片附上供你参考,一定要自己做一遍才好哦,有助提高。
没写过物理实验,不过把这些变量与物理试验中的参数对应,然后运用数学方法编程实现,照这个洞者帆纳雹思路写写应该就没错了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)