In this exercise, we are tasked with implementing the Simposon’s rule to compute integrals.
where , is even and .
To make it amenable to be solved using the given sum function, we can transform the equation to
Translating this to Scheme, we arrive at:-
Comparing with the given integral function in the book, we get the following:-
As can be seen, we get the correct answer with just two points using Simpson’s rule compared to the trapezoidal rule which only approaches the answer as more points are added. This is because Simpson’s rule is a higher order approximation compared to the trapezoidal rule. Wiki Link
Exercise 1.30
In this exercise, we are tasked with rewriting sum so that it is evaluated recursively. It is dones as follows:-
Repeating the procedures with the new definition gives the same answers.
Exercise 1.31
This exercise tasks us with writing a product procedure analogous to the sum procedure from earlier. We simply follow the same format and replace addition with multiplication and 0 with 1.
Recursive procedure
We get the following recursive procedure:-
We can compute the factorial of a number by using the following code.
Also, we can compute the approximate value of using the given formula:-
Iterative procedure
Like how we expressed sum using an iterative procedure, we can also express product using an iterative procedure as follows:-
As can be seen, same results are obtained from both recursive and iterative procedure.
Exercise 1.32
In this exercise, we are tasked with showing that both sum and product can be shown as a special case of an accumulate procedure with the signature.
This can simply be done by extracting the differences between the two and encapsulating it in combiner and null-value. Multiplication and addition go into combiner. 1 and 0 go into null-value
Recursive procedure
The recursive procedure is as follows:-
Then, product and sum can be defined as follows:-
Iterative procedure
The iterative procedure is also simple. It is as follows:-
Same answers are obtained when the procedures are evaluated using the functions from previous exercises.
Exercise 1.33
In this section we are tasked with creating a version of accumulate called filtered-accumulate that only accumulates values satisfying a single argument predicate named filter.
Sum of squares of prime numbers
Using the fast-prime? from the previous section, we can create a function that sums the squares of prime numbers from a given range.
To validate this, we can look at the first few primes 2, 3, 5, 7, 11 and 13.
Product of coprimes
The product of coprimes of n can be computed as follows:-
To test it, let us check for the coprimes of number 13. Since 13 is a prime, the answer should be .
Exercise 1.34
In this exercise, we have the following function:-
If we ask the interpreter to evaluate (f f) we end up getting the following substitutions:-
Unfortunately, the statement (2 2) can’t be evaluated since 2 is not function. Thus, an error should be thrown by the interpreter. Let us verify this:-
Thus, application of 2 throws an error.
Exercise 1.35
In this exercise, we are simply tasked with utilizing the pre-defined fixed-point procedure to determine the golden ratio given by the formula . To perform that we use the following code.
Thus the golden-ratio is obtained.
Exercise 1.36
In this exercise, we are asked to modify fixed-point to print out intermediate results along the way and use it to compute the solution to . We modify the procedure as thus:-
Computation without average damping
First, we try to compute the solution to by using the direct relation with an initial guess of 2.
We see a lot of oscillation about the final solution with this undamped iteration. Thus, it takes a long time to converge.
Computation average damping
Next, we try to compute the solution to by using a modified relation with an initial guess of 2.
As can be seen, this damped relation prevents unwanted oscillations and lets the recurrence converge on right value quicker.
Exercise 1.37
In this exercise, we are tasked with creating a function cont-frac which can be used to compute continued fractions of the form
given the functions for generating and .
Recursive procedure
We define a function of form (cont-frac n d k) which computes continued fractions.
Next, we are told to compute where is the golden ratio using the relation and . This can be having n and d as (lambda (i) 1.0). To determine the number of steps required for 4 decimal places of accuracy, we perform repeated calculation to see when we reach .
Thus, we can see that it takes a 11-term finite continued fraction to compute the reciprocal of golden ratio to 4 decimal places.
Iterative procedure
The conversion of the procedure to be iterative cannot be done directly for this procedure since computing k-term finite continued fraction from a (k-1)-term finite continued fraction is not an easy task. However, we can compute the whole expression in the reverse direction staring with and use accumulated values. We get the following code in that case:-
Repeating with the same arguments as the recursive version gives the same results.
Exercise 1.38
In this exercise, we are tasked with computing , the base of natural logarithms using the cont-frac procedure from the previous exercise. We are given the fact that when is all 1 and are successfully , we get .
Thus, the value of is computed.
Exercise 1.39
In this exercise, we are required to compute the tangent function using the formula
where x is in radians. We define a procedure tan-cf as follows:-
Let us compare with the built-in tan function in Scheme.
As can be seen, the continued fraction representation gives an accurate computation of the tangent function.
Exercise 1.40
This exercise tasks us with creating a function cubic which can be used in conjunction with newtons-method to determine the zeroes of the cubic . This problem is fairly simple.
This can be used by Newton’s method to determine the roots.
Exercise 1.41
In this exercise, we are required to write a procedure double which takes a function and applies it twice. It can be done easily too.
Now, when we perform the following procedure,
Doubling 4 times, means inc would be applied times. The expected answer would be 21. To verify,
Exercise 1.42
In this exercise, we are tasked with writing a compose function that takes two one-argument functions f and g and gives a compound function
To test it, we try the given example in Scheme REPL.
Exercise 1.43
In this exercise, we are tasked with the writing a function repeated which would take a single argument function f and a number n and return a compound function n times. The procedure for this can be written using the same pattern as the quick exponentiation function from the previous sections as well as the compose and double functions from the previous exercises.
To test it, we run through the example cases.
Exercise 1.44
In this exercise, we are required to create a way of smoothing functions repeatedly. Applying smoothing to a function f produces a new function g such that . Smoothing can be performed repeatedly as well. We utilize the repeated function from the previous exercise to accomplish this. We keep a value of as used in the book.
To test this function, we take the sin function. We get the following numbers for x around 1.
x
sin(x)
1-damp
2-damp
3-damp
0.99997
0.8414547754
0.99998
0.8414601786
0.8414601786
0.99999
0.8414655817
0.8414655817
0.8414655817
1
0.8414709848
0.8414709848
0.8414709848
0.8414709847
1.00001
0.8414763878
0.8414763878
0.8414763877
1.00002
0.8414817907
0.8414817907
1.00003
0.8414871935
Let us test using our code.
As can be seen, correct answers are obtained.
Exercise 1.45
In this exercise, we are essentially tasked with tweaking the number of damps to ensure that the roots of polynomials converge when fixed-point iteration is used. To study this, let use the modified fixed-point function that outputs intermediate values in the fixed-point-of-transform function.
Let us create a root function which computes the n-th root of x and also takes starting guess and the number of damps used. We can utilize the repeated function from the previous exercise.
Let us find the roots of various powers. Let us start with square root with no damping
As can be seen, we see a repeating pattern. The process does not converge. Let us add one damping. If iteration converges, only the last value is given for brevity.
As can be seen, the 4-th root with 1 damp causes oscillation around the actual value. Thus, one average damp is not sufficient for 4-th root and above. Let us add one more damp and try again.
Thus, damping twice stops being effective from the 8-th root onwards. We can sort of see a relationship here. Let us verify this by checking if 3 damps stop being effective for the 16-th root onwards.
Thus, we have verified that for 16-th root onwards 3 damps are not sufficient. We can see the following pattern:-
Roots of 2-3 require 1 damp
Roots of 3-7 require 2 damps
Roots of 8-15 require 3 damps
Roots of 16-… require 4 damps
As n reaches powers of two, one more average damp is needed to compute root. Thus, the number of damps required can be computed using the floor function.
Let us create a generic n-th-root function using this knowledge.
To test it, let us find large roots of numbers.
It works!
Exercise 1.46
In this exercise, we are tasked with writing a generic procedure called iterative-improve which takes in two procedures, one two check if a guess is good enough and one to improve the guess. This procedure should return a procedure which, when applied to a guess should improve it iteratively. This is a simple task that can be accomplished using the following code.
Sqrt function
Let us consider the original sqrt function
It can be rewritten as follows by reusing some of its functions:-
Fixed-point iteration function
Let us reconsider the fixed-point iteration function.