物理实验要做杨氏模量的计算,网上找了一些但是都不完全符合教材测量值,就自己写顺手写一个在这里分享:教材如下 话不多说直接上代码(数据需求都已经在代码中给出了)
#include<iostream>
#include<math.h>
#include<string.h>
#define g 9.8
#define ul 0.5
#define ud 0.5
#define tp 1.59
#define dyi 0.05
#define d2yi 0.004
#define pi 3.1415926
using namespace std;
template<class T>
double aveSum(T& a)
{
double length=sizeof(a)/sizeof(a[0]),sum=0;
for(int i=0;i<length;i++)
{
sum=sum+a[i];
}
double ave=double(sum)/length;
return ave;
}
template<class T>
double stddev(T& a)
{
double length=sizeof(a)/sizeof(a[0]),sum=0;
double ave= aveSum(a);
for(int i=0;i<length;i++)
{
sum=sum+(a[i]-ave)*(a[i]-ave);
}
double ans=sqrt(sum/(length-1));
return ans;
}
int main()
{
double gainweight[8]; //增重
double loseweight[8]; //减重
double aveweight[8]; //x均值
double detalweight[4]; //detalx的值
double avedetalweight; //detalx均值
double sigmaweight[4]; //sigmax
cout<<"请输出八组增重数据"<<endl;
for(int i=0;i<8;i++)
{
cin>>gainweight[i];
}
cout<<"请输入八组减重数据"<<endl;
for(int i=0;i<8;i++)
{
cin>>loseweight[i];
}
for(int i=0;i<8;i++)
{
aveweight[i]=(gainweight[i]+loseweight[i])/2;
}
for(int i=0;i<8;i++)
{
cout<<"x"<<i+1<<"的平均值"<<aveweight[i]<<endl;
}
cout<<endl;
for(int i=0,j=4;i<4;i++,j++)
{
detalweight[i]= (aveweight[j]-aveweight[i])/4;
cout<<"detalX"<<i+1<<"的平均值"<<detalweight[i]<<endl;
}
cout<<endl;
avedetalweight=aveSum(detalweight);
cout<<"detalX的平均值"<<avedetalweight<<endl;
cout<<endl;
for(int i=0;i<4;i++)
{
sigmaweight[i]=detalweight[i]-avedetalweight;
cout<<"sigmaX"<<i+1<<"的平均值"<<sigmaweight[i]<<endl;
}
cout<<endl;
double udetalxA=tp*stddev(detalweight);
double udetalX=sqrt(udetalxA*udetalxA+dyi*dyi);
cout<<"udetalx:"<<udetalX<<endl;
cout<<"detalX="<<avedetalweight<<"+"<<udetalX<<"(cm)"<<endl;
//金属丝测量
double d[6];
cout<<"请输入金属丝直径的六次测量值"<<endl;
for(int i=0;i<6;i++)
{
cin>>d[i];
}
double aved=aveSum(d);
cout<<"金属丝直径平均值aved:"<<aved<<endl;
double usd=sqrt(d2yi*d2yi+stddev(d)*stddev(d));
cout<<"结果d="<<aved<<"+"<<usd<<endl;
cout<<endl;
//钢丝长度L和标尺到镜面距离D
cout<<"请输入钢丝长度L和标尺到镜面距离D的测量值"<<endl;
double L,D;
cin>>L>>D;
cout<<"结果L="<<L<<"+"<<"0.5"<<endl<<"结果D="<<D<<"+"<<"0.5"<<endl;
cout<<endl;
//光杠杆镜臂长
cout<<"输入游标卡尺示值误差和光杆杆镜臂长"<<endl;
double ub,b;
cin>>ub>>b;
cout<<"结果b"<<"="<<b<<"+"<<ub<<endl;
cout<<endl;
//实验结果计算
double Y=(8*1*g*L/1000*D/1000)/(pi*(aved/1000)*(aved/1000)*(b/1000)*(avedetalweight/100));
cout<<"Y的数值"<<Y<<endl;
double E=sqrt(((ul/1000)/(L/1000))*((ul/1000)/(L/1000))+((ud/1000)/D/1000)*((ud/1000)/D/1000)+(2*usd/1000)/(aved/1000)*(2*usd/1000)/(aved/1000)+(ub/1000)/(b/1000)*(ub/1000)/(b/1000)+(udetalX/1000)/(avedetalweight/1000)*(udetalX/1000)/(avedetalweight/1000));
cout<<"相对误差E="<<E<<endl;
double uy=E*Y;
cout<<"不确定度uy="<<uy<<endl;
cout<<"结果Y="<<Y<<"+"<<uy<<endl;
return 0;
}
注:上述数据均应该按该书中数据要求输入程序中。程序中螺旋测微仪的示值误差为0.004mm(我所做实验条件下的)。
示例运行结果: