1.1解:m=3;f=@(x)digit(digit(x^4,m)- digit(x^3,m)+ digit(3*x^2,m)+ digit(x-2,m),m);g=@(x)digit(digit(digit( digit(digit(digit( (x-1)*x,m)+3,m)*x,m)+1,m)*x,m)-2,m);f(3.33)g(3.33)有ans = 121ans =121实际上,当m=2时,就可以看出这两种算法在计算的精确度上的区别:m=2;f=@(x)digit(digit(x^4,m)- digit(x^3,m)+ digit(3*x^2,m)+ digit(x-2,m),m);g=@(x)digit(digit(digit( digit(digit(digit( (x-1)*x,m)+3,m)*x,m)+1,m)*x,m)-2,m);f(3.33)g(3.33)有ans = 120ans =130,可以看出,两者在计算精度上的不同区别,数学上恒等,在数值上不一定恒等。
1.2解:(1)精确到小数点后第三位,故有4位有效数字(2)精确到小数点后第三位,故有2位有效数字(3)精确到小数点后第三位,故有0位有效数字1.3 解;记圆的面积为S,由题意有|e(S)|≤1%。
由S=πr2知:dS=2πrdr所以dS/S=(2πrdr)/(πr2)=2(dr/r)∴|e(r)|≈1/2|e(S)|≤0.5×1%=0.5%1.4 解:由题有:|e(x)|≤1/2×10^-2 ; |e(y)|≤1/2×10^-2; |e(z)||≤1/2×10^-2∴|e(S)|≈|xe(x)+ye(y)|+ |ze(z)|^2≈x|e(x)|+y|e(y)|+z^2|z(z)|^2≤4.21×0.005+1.79×1.005+2.11×2.11×0.005^2=0.03≤1/2×10^-1又S=4.21*1.79+2.11^2=11.988∴S至少具有3位有效数字。
在字长为3的计算机上运行,误差为:S1=4.21*1.79+2.11;S2=digit(digit(4.21*1.79,3)+digit(2.11^2,3),3);err=S1-S2err = -2.35411.6 解:clcdisp('Please input the coefficients of');disp('quadratic equation ax^2+bx+c=0, respectively')a=input('a=');b=input('b=');c=input('c=');m=4; % m-digit rounding arithmeticif abs(a)<eps & abs(b)<epserror('incorrect input')endif abs(a)<epsdisp('Since a=0, quadrtic equation degenerates into a linear equation.')disp('The only solution of the linear eqution is')x=digit(-c/b,m)returnenddelta=b^2-4*a*c;temp=sqrt(delta);if b>0x1=(-b-temp)/(2*a)endif b<0x1=(-b+temp)/(2*a)endif b==0x1=temp/(2*a)endx2=(c/a)/x1在输入a=1,b=-112,c=2后有结果x1 =111.9821x2 =0.01791.8方法一:00!!n n k x n n x x e n n ∞===≈∑∑,x -∞<<+∞ 方法二:00()()1/1/1/!!n n k x x n n x x e en n ∞-==--==≈∑∑,x -∞<<+∞,clc;%Initialize the datax=5; k=10;m=4; %three-digit rounding arithmetic%------------------------------------% Compute exp(x) by using Method (A)% with the computer precisionresults_1=1;power_x=1;for i=1:kfactor_x=x/i;power_x=power_x*factor_x;results_1=results_1+power_x;endresults_1err_1=abs(exp(x)-results_1)%------------------------------------% Compute exp(x) by using Method (A)% with the 3-digits precisionresults_2=1;power_x=1;for i=1:kfactor_x=digit(x/i,m);power_x=digit(power_x*factor_x,m);results_2=digit(results_2+power_x,m);endresults_2err_2=abs(exp(x)-results_2)%------------------------------------% Compute exp(x) by using Method (B)% with the computer precisiont=-x;results_3=1;power_x=1;for i=1:kfactor_x=t/i;power_x=power_x*factor_x;results_3=results_3+power_x;endresults_3=1/results_3err_3=abs(exp(x)-results_3)%------------------------------------% Compute exp(x) by using Method (B)% with the 3-digits precisiont=-x;results_4=1;power_x=1;for i=1:kfactor_x=digit(t/i,m);power_x=digit(power_x*factor_x,m);results_4=digit(results_4+power_x,m);endresults_4=digit(1/results_4,m)err_4=abs(exp(x)-results_4)%------------------------------------上述程序用公式(1)及(2)分别在Matlab许可精度下及限定在字长为4的算术运算情况下给出的近似计算结果,其中results_1, results_2为用方法(1)在上述两种情况下的计算结果,err_1, err_2为相应的绝对误差;类似的,results_3, results_4为用方法(2)在上述两种情况下的计算结果,err_3, err_4为相应的绝对误差.本题程序,来自与我们敬爱的徐明华老师的上机练习题,请大家注意!1.9 解:∵1-cos(x) ≈242211(1)()224224x x x x --+=-∴f(x)= 21()224x -Matlab 实现:f=@(x)digit((1/2-digit(x^2/24,10)),10);f(0.012)在字长为10的计算机中有结果为:ans =0.50001.10 解:Matlab 实现:f=@(x)digit(digit(exp(x)-1,5)/x,5);g=@(x)digit(1+digit(x/2,5)+digit(digit(x^2,5)/6,5)+digit(digit(x^3,5)/24,5),5);f(0.015)g(0.015)exc=1.0077376410479;err1=f(0.015)-excerr2=g(0.015)-exc结果为:ans =1.0075ans = 1.0075err1 =-2.3764e-04err2 =-2.3764e-04误差的主要原因是由于字长为5导致计算中不断的产生舍去误差,从而使得计算值与相对精确值之间产生了差异,如果加大字长,可以进一步的减少误差1.12 解:方法一:直接利用一元二次方程的求根公式1,22x a =方法二:改进上述求根公式,避免相近数相减,得到下述求根公式12x a = 21(/)/x c a x =,其中1,0sgn()0,01,0x x x x >⎧⎪==⎨⎪-<⎩。
Matlab实现:disp('Please input the coefficients of');disp('quadratic equation ax^2+bx+c=0, respectively')a=input('a=');b=input('b=');c=input('c=');m=3; % m-digit rounding arithmeticif abs(a)<eps & abs(b)<epserror('incorrect input')end% First,find the root of the quadratic% equation in computer precision.if abs(a)<epsdisp('Since a=0, quadrtic equation degenerates into a linear equation.') disp('The only solution of the linear eqution is')x=digit(-c/b,m)returnenddelta=b^2-4*a*c;temp=sqrt(delta);% solve ax^2+bx+c=0 with method 1x1=(-b+temp)/(2*a)x2=(-b-temp)/(2*a)err1=abs(a*x1^2+b*x1+c)err2=abs(a*x2^2+b*x2+c)% solve ax^2+bx+c=0 with method 2.if b>0x1=(-b-temp)/(2*a)endif b<0x1=(-b+temp)/(2*a)endif b==0x1=temp/(2*a)endx2=(c/a)/x1err1=abs(a*x1^2+b*x1+c)err2=abs(a*x2^2+b*x2+c)% Second,find the root of the quadratic% equation in m-digit precision.if abs(a)<epsdisp('Since a=0, quadrtic equation degenerates into a linear equation.') disp('The only solution of the linear eqution is')x=digit(-c/b,m)returnenddelta=digit(digit(b^2,m)-digit(4*digit(a*c,m),m),m); temp=digit(sqrt(delta),m);% solve ax^2+bx+c=0 with method 1x1=digit(digit(-b+temp,m)/digit(2*a,m),m)x2=digit(digit(-b-temp,m)/digit(2*a,m),m)err1=abs(a*x1^2+b*x1+c)err2=abs(a*x2^2+b*x2+c)% solve ax^2+bx+c=0 with method 2.if b>0x1=digit(digit(-b-temp,m)/digit(2*a,m),m)endif b<0x1=digit(digit(-b+temp,m)/digit(2*a,m),m)endif b==0x1=digit(temp/digit(2*a,m),m)endx2=digit(digit(c/a,m)/x1,m)err1=abs(a*x1^2+b*x1+c)err2=abs(a*x2^2+b*x2+c)以上程序参考了徐明华老师的程序,请大家注意!1.13 解:x=3^(-0.5);s=0;y=atan(x);for i=1:1e6n=2*i-1;s=s+(-(-1)^i)*(x^n)/n;err=y-s;if abs(err) <= 1e-11break;endendysierrpai=6*s运行后可以得出:y = 0.523598775598299s =0.523598775607971i =19err =-9.672040945929439e-12pai = 3.141592653647825附录:实现在特定字长计算机中模拟的函数digit的实现:大家可新建一个m文件将下面的程序拷入,保存在当前的工作目录下,就可以直接调用digit函数,格式为digit(x,m),m 为字长!function y=digit(x,m)% This function is used to round x towards% a nearest normalized scientific m-digit number.% For example% digit(12.345,3)=0.123*10^2; digit(12.345,4)=0.1235*10^2;% digit(0.012345,3)=0.123*10^{-1}.% Input:% - x is a vector in R^{k}.% - m is the given number of significant decimal digits of computer. k=max(size(x));y=x; % initialize the value of y.for i=1:kif x(i)<0sign=-1;elsesign=1;endx(i)=abs(x(i));p=0;if x(i) < 0.1 & x(i) > epswhile x(i) < 0.1x(i)=x(i)*10;p=p-1;endendif x(i) >=1while x(i)>=1x(i)=x(i)/10;p=p+1;endendy(i)=round(x(i)*10^m)/10^m;y(i)=sign*y(i)*10^p;endreturn。