4-5迭代法求解节点温度。说明:此处给出的是C++程序代码,使用牛顿迭代法,迭代收敛精度1.0e-6;程序运行结果附后。/*NHT4-5newton*createdon2012-10-19bySanye*/#includecmath#includestdio.h#includeiostreamusingnamespacestd;intmain(){doublefunT=1.0,dfunT=1.0,temp1=1.0,temp2=1.0;doubleT=20.0;//primaryvalueinti=0;//forTEST!coutprimaryT=T;endl;while(fabs(funT/dfunT)=1.0e-6){i++;if(i==1&&(T=20.0))T=100.0;//incaseunreasonableT;temp1=pow(T-20.0,0.25);temp2=pow(T-20.0,-0.75);funT=0.5*T-80+2*T*temp1-40*temp1;dfunT=0.5+2*temp1+0.5*T*temp2-10*temp2;T=T-funT/dfunT;coutstepi;\tT3'-T3=funT/dfunT;endl;}couttotalsteps:iendl;cout\tT[1]=100\tT[2]=0.5*(T+130)\tT[3]=Tendl;return0;}运行结果:4-12编写TDMA算法程序验证其正确性。说明:此处给出的是C++程序代码,程序运行结果附后。/*TDMA.cpp*Createdon2012-10-19bySanye*/#includeiostream#includeiomanip#includecmath#includestdlib.h#includeTDMA.husingnamespacestd;intmain(){intnum=10;//num:numberofTpoints;vectordoubleA,B,C,D,T_before,T_after;intrand_temp1,rand_temp2;/*GenerateA,B,C,D,T_beforeRandomly*/for(inti=0;inum;++i){rand_temp1=rand()%10;rand_temp2=rand()%20;A.push_back((1.0+rand_temp1/2.0));B.push_back(rand_temp2/(rand_temp1+3.0)/5.0+1.0);C.push_back(rand_temp1*rand_temp2/10.0+2.0);T_before.push_back(sqrt(i*i+1.0)*11.0-20.0);}B[0]=0.0;C[num-1]=0.0;for(inti=0;inum;++i){doubletemp;if(i==0)temp=A[0]*T_before[0]-B[0]*T_before[1];elseif(i==num-1)temp=A[num-1]*T_before[num-1]-C[num-1]*T_before[num-1-1];elsetemp=A[i]*T_before[i]-B[i]*T_before[i+1]-C[i]*T_before[i-1];D.push_back(temp);}/*GetT_after*/T_after.resize(num);T_after=TDMA(A,B,C,D);/*CalculateT_err*/vectordoubleT_err;for(inti=0;inum;++i)T_err.push_back(T_after[i]-T_before[i]);/*CoutT*/cout.setf(ios::fixed);cout.precision(4);for(inti=0;inum;++i)coutT_before[i+1]=T_before[i]\tT_after[i+1]=T_after[i]\tT_err[i+1]=T_err[i]endl;return0;}/*TDMA.h*/#ifndefTDMA_H_INCLUDED#defineTDMA_H_INCLUDED#includevectorusingnamespacestd;vectordoubleTDMA(vectordouble&A,vectordouble&B,vectordouble&C,vectordouble&D){vectordoubleP,Q,T;intn=A.size();P.resize(n);Q.resize(n);T.resize(n);/*GetP,Q*/P[0]=B[0]/A[0];Q[0]=D[0]/A[0];for(inti=1;in;++i){P[i]=B[i]/(A[i]-C[i]*P[i-1]);Q[i]=(D[i]+C[i]*Q[i-1])/(A[i]-C[i]*P[i-1]);}/*CalculateT*/T[n-1]=Q[n-1];for(inti=n-1;i0;--i)T[i-1]=P[i-1]*T[i]+Q[i-1];returnT;}#endif//TDMA_H_INCLUDED运行结果: