^.
数值分析上机报告
^.
第一章
一、题目
精确值为
1311(??)。 22NN?11) 编制按从大到小的顺序SN?2) 编制按从小到大的顺序SN?111,计算SN的通用程序。 ?????22?132?1N2?1111,计算SN的通用程?????N2?1(N?1)2?122?1序。
3) 按两种顺序分别计算S102,S104,S106,并指出有效位数。(编制程序时用单精度) 4) 通过本次上机题,你明白了什么?
二、通用程序
clear N=input('Please Input an N (N>1):'); AccurateValue=single((0-1/(N+1)-1/N+3/2)/2); Sn1=single(0); for a=2:N; Sn1=Sn1+1/(a^2-1); end Sn2=single(0); for a=2:N; Sn2=Sn2+1/((N-a+2)^2-1); end fprintf('The value of Sn using different algorithms (N=%d)\\n',N); disp('____________________________________________________') fprintf('Accurate Calculation %f\\n',AccurateValue); fprintf('Caculate from large to small %f\\n',Sn1); fprintf('Caculate from small to large %f\\n',Sn2); disp('____________________________________________________') ^.
三、求解结果
Please Input an N (N>1):10^2 The value of Sn using different algorithms (N=100) ____________________________________________________ Accurate Calculation 0.740049 Caculate from large to small 0.740049 Caculate from small to large 0.740050 ____________________________________________________ Please Input an N (N>1):10^4 The value of Sn using different algorithms (N=10000) ____________________________________________________ Accurate Calculation 0.749900 Caculate from large to small 0.749852 Caculate from small to large 0.749900 ____________________________________________________ Please Input an N (N>1):10^6 The value of Sn using different algorithms (N=1000000) ____________________________________________________ Accurate Calculation 0.749999 Caculate from large to small 0.749852 Caculate from small to large 0.749999 ____________________________________________________ 四、结果分析
有效位数 n 顺序 从大到小 从小到大 6 5 3 6 3 6 100 10000 1000000 可以得出,算法对误差的传播又一定的影响,在计算时选一种好的算法可以使结果更为精确。从以上的结果可以看到从大到小的顺序导致大数吃小数的现象,容易产生较大的误差,求和运算从小数到大数算所得到的结果才比较准确。