计算机程序设计艺术习题解答(Excercise 1.2.2-28~30题)

28. [M30 ] (R. Feynman.) 开发一个计算 b^{x} 的算法,其中 0 ≤ x < 1, 只使用加法、减法和移位(shifting)运算(类似于 exercise 25 使用的算法), 并分析算法的精度。

解:

Exercise 25 是计算 \log_{b}{x} 其中 1\leq x< 2 的一个简易算法,算法用到了两个变量 x 和 y,其中 x 初值为输入的 x,y 的初值为零。在算法中 x 不断减去它当前数值的 \frac{1}{2^{k}}( k 从 1 开始逐渐递增),相当于 x 以 ((2^{k}-1)/2^{k}) 的比例逐渐递减,同时 y 会相应加上 \log_{b}(2^{k}/(2^{k}-1)) 来弥补 x 的减少,使得当前的 \log_{b}{x}+y 的值始终保持近似恒定,直到 x(或 x 的近似值)减到1为止,此时 y 的值就是算法所求的 \log_{b}{x}。当然算法需要一个所有 k (k\geq 1) 对应的 \log_{b}(2^{k}/(2^{k}-1)) 的值的辅助表来快速计算 y 的值。

Exercise 28 刚好是 Exercise 25 的逆运算,因此我们正好将对 x 和 y 变量的操作互换。注意到 此时算法输入的 x 限制为 0 ≤ x < 1,而我们发现 b^{x}= b/b^{1-x},因此可以将变量 x 的初值设为(1-x),变量 y 的初值设为分子 b  (x:1-x\rightarrow 0,\, \, y:b\rightarrow b^{x}) ,在算法进行中 y 变量的值以 ((2^{k}-1)/2^{k}) 的比例逐渐递减( k 从 1 开始逐渐递增),同时 x 变量的值会相应减去 \log_{b}(2^{k}/(2^{k}-1)) 来弥补 y 变量值的减少,使得当前变量 y/b^{x} 的值始终保持近似恒定(y\rightarrow y((2^{k}-1)/2^{k})\: \: \Leftrightarrow \: \: b^{x}\rightarrow :b^{x}b^{\log_{b}((2^{k}-1)/2^{k})}=b^{x-\log_{b}(2^{k}/(2^{k}-1))}),直到 变量 x(或 x 的近似值)减到 0 为止,此时 变量 y 的值就是算法所求的 b^{x} 的值。具体英文算法如下(考虑到计算机的有限精度限制,引入很小的常数 \epsilon):

  1. E1.  [Initialize.] Set x\leftarrow (1-x-\epsilon ) , y\leftarrow y_{0} , and k\leftarrow 1, where (1-\epsilon) is the largest possible value of x, and y_{0} is the nearest approximation to b^{1-\epsilon }. (The quantity y/b^{x} will remain approximately constant in the following steps.)

  2. E2.  [Test for end.] If x=0, stop.

  3. E3.  [Compare.] If x< \log_{b}(2^{k}/(2^{k}-1)), increase k by 1 and repeat this step.

  4. E4.  [Reduce values.] Set x\leftarrow x-\log_{b}(2^{k}/(2^{k}-1))y\leftarrow y-(y \, shifted\, right \, k),

    and go to E2.

 当然算法需要复用 25 题用到过的所有 k (k\geq 1) 对应的 \log_{b}(2^{k}/(2^{k}-1)) 的值的辅助表。

在步骤 E1 中,假设 y 的初值(考虑近似误差)为 b^{1-\epsilon}(1+\epsilon _{0}) ,在其后每次执行 E4 时,都可能会产生计算误差,对应到第 j 次 (j > 0),可表示为 x\leftarrow x+\log_{b}(1-2^{-k})+\delta _{j}  ,y\leftarrow y(1-2^{-k})(1+\epsilon _{j}) ,其中 \delta _{j} 和 \epsilon _{j} 是每次 E4 在有限精度限制下导致的小误差。当算法终止时,得到最终结果y=b^{x-\sum \delta _{j}}\prod _{j}(1+\epsilon _{j}) 。

有必要指出, 那个辅助表中 \log_{b}(2^{k}/(2^{k}-1)) 的值也只能是在有限精度下的近似值。但当基数 b 是 e 时(自然对数),计算机可以算出 \log_{b}(2^{k}/(2^{k}-1)) 更为精确的值。根据麦克劳林级数展开,\ln(1-x)=-x-\frac{x^{2}}{2}-\frac{x^{3}}{3}-\frac{x^{4}}{4}-... ,那么就有 \ln(2^{k}/(2^{k}-1))=-\ln(1-2^{-k})=2^{-k}+\frac{1}{2}2^{-2k}+\frac{1}{3}2^{-3k}+\frac{1}{4}2^{-4k}+... ,这个计算在电脑中可以通过简单的移位操作更加高效地完成。

29. [HM20 ] Let x be a real number greater than 1. (a) For what real number b > 1 is b\log_{b}x a minimum? (b) For what integer b > 1 is it a minimum? (c) For what integer b > 1 is (b+1)\log_{b}xa minimum?

解:

将 b\log_{b}x 改写成 \frac{b\ln{x}}{\ln{b}} 。

(a): 令 f(b) = \frac{b\ln{x}}{\ln{b}} ,则 f^{'}(b)=\frac{(\ln{b}-1)\ln{x}}{(\ln{b})^{2}} ,若要让 f(b) 取到最小值,则必须 f^{'}(b)=0 \Rightarrow \ln{b}=1\Rightarrow b=e ,再和其它 b 的取值比较,显然有 当 b = e 时 b\log_{b}x

有最小值。 

(b): 当 b 是大于 1 的整数时,其实就是看 \frac{b}{\ln{b}} 什么时候最小。显然,当 b 是大于 e 的整数时,随着 b 的增大,\frac{b}{\ln{b}} 也会相应增大。现在只需要比较下当 b = 2 和 b = 3 时 \frac{b}{\ln{b}}

的值哪个大就行。由于 \frac{2}{\ln{2}}-\frac{3}{\ln{3}}=\frac{2\ln{3}-3\ln{2}}{\ln{2}\ln{3}}=\frac{\ln{9}-\ln{8}}{\ln{2}\ln{3}}>0 ,可知当 b = 3 时 \frac{b}{\ln{b}}取到最小值,也就是 b\log_{b}x 在 b = 3 时取到最小值。 

(c): 由 (b+1)\log_{b}x=\frac{b+1}{\ln{b}}\ln{x} ,当 b 是大于 1 的整数时,其实就是看 \frac{b+1}{\ln{b}} 什么时候最小。貌似和上面一样, 当 b 是大于 e 的整数时,随着 b 的增大,\frac{b+1}{\ln{b}} 也会相应增大。而且比较 b = 2 和 b = 3 时的情况,也有 \frac{3}{\ln{2}}-\frac{4}{\ln{3}}=\frac{3\ln{3}-4\ln{2}}{\ln{2}\ln{3}}=\frac{\ln{27}-\ln{16}}{\ln{2}\ln{3}}>0 ,即 b =3 时 \frac{b+1}{\ln{b}}

 比 b = 2 时更小。微妙之处在于比较 b = 3 和 b = 4 的情况, 有 \frac{4}{\ln{3}}-\frac{5}{\ln{4}}=\frac{4\ln{4-5\ln{3}}}{\ln{3}\ln{4}}=\frac{\ln{256}-\ln{243}}{\ln{3}\ln{4}}>0 ,即 \frac{b+1}{\ln{b}} 当 b = 3 时 比 b = 4 时 ​​​更大。且 \frac{5}{\ln{4}}-\frac{6}{\ln{5}}=\frac{5\ln{5-6\ln{4}}}{\ln{4}\ln{5}}=\frac{\ln{3125}-\ln{4096}}{\ln{4}\ln{5}}<0 ,即 \frac{b+1}{\ln{b}} 当 b = 5 时 比 b = 4 时 更大。这说明 b = 4 才是 \frac{b+1}{\ln{b}}向上的拐点,所以可得 b = 4时 \frac{b+1}{\ln{b}}取到最小值,也就是 (b+1)\log_{b}x 在 b = 4 时取到最小值。

30. [12] Simplify the expression (\ln{x})^{\ln{x}/\ln{\ln{x}}}, assuming that x>1 and x\neq e.

解:

对这个表达式再取一次自然对数,就得到 \ln{(\ln{x})^{\ln{x}/\ln{\ln{x}}}}=(\ln{x}/\ln{\ln{x}})\ln{\ln{x}}=\ln{x},因此这个表达式可直接简化为 x

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To plot C versus S for 0 ≤ S ≤ 200, we can use the following MatLab code: ```matlab E = 100; % Exercise price T = 1; % Expiry date r = 0.05; % Risk-free interest rate sigma = 0.3; % Volatility S = linspace(0, 200, 1000); C = zeros(size(S)); for i = 1:numel(S) C(i) = EuropeanCall(S(i), E, r, sigma, T); end plot(S, C); xlabel('S'); ylabel('C'); title('European Call Option Price'); ``` This code first defines the parameters of the option, and then generates a range of values for S between 0 and 200 using the `linspace` function. For each value of S, the code calculates the option price using the `EuropeanCall` function (which can be obtained from various sources). Finally, the code plots the option price as a function of S using the `plot` function. To plot the value of C(S, t) at different values of t between t = 0 and t = T, we can modify the above code as follows: ```matlab E = 100; % Exercise price T = 1; % Expiry date r = 0.05; % Risk-free interest rate sigma = 0.3; % Volatility S = linspace(0, 200, 1000); t = linspace(0, T, 1000); C = zeros(numel(S), numel(t)); for i = 1:numel(S) for j = 1:numel(t) C(i, j) = EuropeanCall(S(i), E, r, sigma, T - t(j)); end end surf(S, t, C); xlabel('S'); ylabel('t'); zlabel('C'); title('European Call Option Price'); ``` This code generates a grid of values for S and t using `linspace`, and then calculates the option price for each combination of S and t using a nested loop. The option price is stored in a matrix `C`, and is plotted as a surface using the `surf` function. As t approaches T, the option price converges to the intrinsic value of the option, which is max(S - E, 0) for a call option. This is because as the expiry date approaches, the option has less time to move in the money, and its time value decreases.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值