编程介的小学生 2017-01-14 14:39 采纳率: 20.5%
浏览 916
已采纳

Numerical Integration

Description

In his freshman year, flymouse studied integral. Symbolic integration frustrated him a lot. He was often confused by those subtle techniques: alternately trying integration by substitution and integration by parts until the integrand appears in a tabulated form then employing integration by quadrature to have the work finally done. In contrast, numerical integration intrigued him much. By following some fixed procedures such as Newton-Cotes formulas, he could easily find the approximation to some definite integrals without too much effort. Given the exercises flymouse did, can your program do as nice a job as flymouse did?

Input

The input contains several test cases. Each test case consists of one line containing a univariate function f(x) and two real numbers a and b meaning the approximate value of is to be found. f(x) is expressed as a C-expression-like string. Allowed tokens are summarized in the following table:

Token Meaning Example Example in Mathematical Form

  • addition, positive x+1, +x x + 1, +x
  • subtraction, negative x-1, -x x − 1, −x
  • multiplication x*x x · x / division x/2 x⁄2 ^ power x^-x x−x ( left parenthesis (x+1)*x (x + 1) x ) right parenthesis -(x+1) −(x + 1) sin sine sin(x) sin x cos cosine cos(x) cos x tan tangent tan(x) tan x log natural logarithm log(x) ln x exp exponential exp(x) ex asin inverse sine asin(x) arcsin x acos inverse cosine acos(x) arccos x atan inverse tangent atan(x) arctan x abs absolute value abs(x) |x| x variable x x constant C floating-point constants without suffixes 0, 1.5 0, 1.5 And the syntax is given below:

S ::= +S | -S | S+S | S-S | S*S | S/S | S^S | F(S) | (S) | x | constant
F ::= sin | cos | tan | log | exp | asin | acos | atan
Operator precedence and associativity are almost the same as those in C except that ‘^’ takes higher precedence than ‘*’ and ‘/’ and has right associativity.

f(x) is not necessarily continuous. It can have singularities which are always isolated if exist. It can be oscillatory, but it will never be oscillatory in the neighborhood of singularities where it blows up. The integral is guaranteed to converge.

Length of the interval over which f(x) is integrated is not longer than 10.

Process to end of file.

Output

For each test case, output one line with only the approximate value of the integral rounded to exactly four digits past the decimal point.

Sample Input

x+1 0 1
+x 0 1
x-1 0 1
-x 0 1
x*x 0 1
x/2 0 1
x^-x 0 1
(x+1)*x 0 1
-(x+1) 0 1
sin(x) 0 1
cos(x) 0 1
tan(x) 0 1
log(x) 1 2
exp(x) 0 1
asin(x) 0 1
acos(x) 0 1
atan(x) 0 1
abs(x) 0 1
x 0 1
1 0 1
sin(x)/x 0 1
Sample Output

1.5000
0.5000
-0.5000
-0.5000
0.3333
0.2500
1.2913
0.8333
-1.5000
0.4597
0.8415
0.6156
0.3863
1.7183
0.5708
1.0000
0.4388
0.5000
0.5000
1.0000
0.9461

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch