[关闭]
@740340735 2016-01-11T12:23:33.000000Z 字数 3043 阅读 809

科学计算作业 一

科学计算

陆一洲 5140309557


Page 45 - 1.7

  1. double x=1;
  2. for (int i=0; i<=16; i++){
  3. double h=pow(10.0, -i),
  4. a=(tan(x+h)-tan(x))/h,
  5. b=1.0/sqr(cos(x));
  6. fprintf(FOUT, "%d\t%.25lf\n", i, fabs(a-b));
  7. }
  8. for (int i=0; i<=16; i++){
  9. double h=pow(10.0, -i),
  10. a=(tan(x+h)-tan(x-h))/h/2,
  11. b=1.0/sqr(cos(x));
  12. fprintf(FOUT, "%d\t%.30lf\n", i, fabs(a-b));
  13. }
(a)

时,误差取得最小值,高于标准误差
(b)

时,误差取得最小值,低于标准误差
由于,该倒数取的为区间的中点值,所以该倒数的斜率相对更具有代表性,所以精度得到提升。

Page46 - 1.9

(a)
  1. double ans=0, x;
  2. scanf("%lf", &x);
  3. for (int i=0; ; i++){
  4. double inc=Power(x, i)/Factorial((double)i);
  5. if (ans+inc==ans) break;
  6. ans+=inc;
  7. }
  8. printf("%.30lf\t%.30lf\t%.30lf\n", ans, exp(x), ans-exp(x));
(b)
进行不断累加,直到结果不再变动(由于精度问题不再变动)。
(c)
Tayler_exp() exp() 绝对误差 相对误差
-20 8.359241e-009 2.061154e-009 6.298087e-009 7.534281e-001
-15 3.059036e-007 3.059023e-007 1.319961e-012 4.314957e-006
-10 4.539993e-005 4.539993e-005 1.241689e-014 2.735002e-010
-5 6.737947e-003 6.737947e-003 1.232607e-016 1.829350e-014
-1 3.678794e-001 3.678794e-001 6.795237e-017 1.847137e-016
1 2.718282e+000 2.718282e+000 2.994566e-016 1.101639e-016
5 1.484132e+002 1.484132e+002 3.191891e-014 2.150679e-016
10 2.202647e+004 2.202647e+004 5.902834e-012 2.679882e-016
15 3.269017e+006 3.269017e+006 3.092282e-011 9.459362e-018
20 4.851652e+008 4.851652e+008 1.199078e-007 2.471484e-016
时,精度误差较小;
时,精度误差随着 的递减而增大。
(d)
Tayler_exp() exp() 绝对误差 相对误差
-20 2.061154e-009 2.061154e-009 4.564034e-025 2.214310e-016
-15 3.059023e-007 3.059023e-007 2.481542e-024 8.112203e-018
-10 4.539993e-005 4.539993e-005 9.423242e-021 2.075607e-016
-5 6.737947e-003 6.737947e-003 1.639432e-018 2.433133e-016
-1 3.678794e-001 3.678794e-001 4.306993e-017 1.170762e-016
可以看出精度有了显著提升。
(e)
将所有计算出来的 都存入数组中,并将其按绝对值从小到大排序,再进行累加,以便得到更高的精度,以下为测试结果:
Tayler_exp() exp() 绝对误差 相对误差
-20 2.061154e-009 2.061154e-009 4.281306e-026 2.077141e-017
-15 3.059023e-007 3.059023e-007 5.045802e-023 1.649481e-016
-10 4.539993e-005 4.539993e-005 1.090555e-020 2.402107e-016
-5 6.737947e-003 6.737947e-003 9.529121e-020 1.414247e-017
-1 3.678794e-001 3.678794e-001 1.244122e-017 3.381874e-017
发现精度并没有太大的变化,这主要原因为double本身精度的问题。

MATLEB TEST

  1. (Matlab test) For n=500, 1000, 2000, 4000, 8000, generate an n*n random matrix, B, and an n*1 vector, b. Find the symmetric matrix A=B’*B.
    1) Using function “eig” to test the time cost for eigen decomposition;
    2) Using x=A\b and x=A^(-1)*b to test the time cost in finding the solution of Ax=b;
    3) Plot all the time costs as a function of n and the power law for the time costs.
(1)
用时() 0.263475 1.279685 6.288209 38.239515 269.477866
(2)
0.007135 0.037733 0.213802 1.326795 9.614620
0.030764 0.134277 0.848402 5.948203 55.072248
(3)
General model Power1:
f(x) = a*x^b
Coefficients (with 95% confidence bounds):
a = 2.975e-09 (1.234e-09, 4.717e-09)
b = 2.807 (2.742, 2.872)
Goodness of fit:
SSE: 0.9548
R-square: 1
Adjusted R-square: 1
RMSE: 0.5641

General model Power1:
f(x) = a*x^b
Coefficients (with 95% confidence bounds):
a = 7.369e-11 (3.274e-11, 1.146e-10)
b = 2.848 (2.786, 2.91)
Goodness of fit:
SSE: 0.001032
R-square: 1
Adjusted R-square: 1
RMSE: 0.01855

General model Power1:
f(x) = a*x^b
Coefficients (with 95% confidence bounds):
a = 1.785e-11 (3.07e-12, 3.263e-11)
b = 3.2 (3.108, 3.292)
Goodness of fit:
SSE: 0.04505
R-square: 1
Adjusted R-square: 1
RMSE: 0.1225

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注