Demo entry 6364632

数值计算方法

   

Submitted by suzui961228 on May 16, 2017 at 16:50
Language: Matlab. Code size: 606 Bytes.

clear;clc;
item=0.5;sum=0.5;sum2=0.5;s=5e-5;a=1;i=1;t=0; %Initialization item作为单项值,sum1作为单精度的值 sum2作为双精度的值
                                         %s作为4位有效数字的误差容限,a作为迭代误差的传递值,t临时存放上一次和的值
r=vpa(pi,15);                            %获取真值
while(abs(item)>eps(sum))                          %
    item=0.5^(2*i+1)*jijiecheng(2*i-1)/(oujiecheng(2*i)*(2*i+1));    %输入公式
    sum=sum+item;      %单精度输入
    a=(sum-t)/sum;                     %误差计算
    t=sum;                              
    er=(sum*6-r)/r;                    %单精度与真值的误差
    i=i+1;
end
p=double(sum*6);                        %四位有效数字的π值

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).