CS1101C Lab 6
The deadline for this lab is Friday 17 October 2008, 23:59:59
hours.
Preliminary — Finding Roots of the sinc Function
During your lecture on Functions and Modularity, the computation of the sinc function was discussed. In this lab assignment, you are tasked to find the location of the roots of the sinc function using different root finding techniques.
Question 1 (Guessing the Location of Roots)
Section 4.7 of Etter's Engineering Problem Solving with C describes a methodology of finding possible roots of a polynomial within some given interval.
The program ch4_7.c is given here. Identify parts of the program which need to be modified such that the program now find roots of the sinc function.
You are to retain all other function names and not rewrite the entire code yourself.
Take note of the following:
- Use the M_PI constant (which is available from the math.h header file) as the value of pi.
- Set the tolerance at 0 to be EPSILON, a constant of the value 1.0e-8. That is to say, if the value of x is within a tolerance of EPSILON to the right or left of 0, then sinc(x) evaluates to the value of 1.
- For consistency, replace all hard-coded 0.1e-4 within the program ch4_7.c with EPSILON.
Test your program with the following sample run.
Enter interval limits a, b (a<b):
-5.5
5.5
Enter step size:
1
Root detected at -5.000
Root detected at -4.000
Root detected at -3.000
Root detected at -2.000
Root detected at -1.000
Root detected at 1.000
Root detected at 2.000
Root detected at 3.000
Root detected at 4.000
Root detected at 5.000
Enter interval limits a, b (a<b):
-5.3
5.3
Enter step size:
1
Root detected at -4.800
Root detected at -3.800
Root detected at -2.800
Root detected at -1.800
Root detected at -0.800
Root detected at 1.200
Root detected at 2.200
Root detected at 3.200
Root detected at 4.200
Root detected at 5.000
| |
The name of your C program file must be called lab6q1.c,
files with any other name will not be marked.
Question 2 (Finding Roots Using the Bisection Method)
Observe that the method above only gives a rough gauge of the location of a possible root within a given interval.
This is because the approximation is simply given by the mid-point of the interval.
As such the output of the two sample runs can be different for different interval limits a and b.
The above method can be refined to give a close approximation to the root. This method is called the Bisection method. For a given interval [a,b] and a function f,
- Find the mid-point p of the two intervals a and b;
- If f(p) is close to 0 (again we use the tolerance of EPSILON), then p is the approximate root.
- Otherwise, if f(a) and f(p) have different signs, set b to be p and repeat step 1;
- Else, set a to be p and repeat step 1.
Implement the Bisection method in a function find_root so that given the interval limits a and b, the function returns the value of the approximate root.
From the check_roots function of lab6q1.c, if a root
exists in an interval, the function find_root is invoked to output a more accurate value of the root. The root values should be printed to 12 decimal places.
The sample run is as follows:
Enter interval limits a, b (a<b):
-5.5
5.5
Enter step size:
1
Root detected at -5.000000000000
Root detected at -4.000000000000
Root detected at -3.000000000000
Root detected at -2.000000000000
Root detected at -1.000000000000
Root detected at 1.000000000000
Root detected at 2.000000000000
Root detected at 3.000000000000
Root detected at 4.000000000000
Root detected at 5.000000000000
Enter interval limits a, b (a<b):
-5.3
5.3
Enter step size:
1
Root detected at -5.000000047684
Root detected at -3.999999988079
Root detected at -2.999999988079
Root detected at -1.999999988079
Root detected at -1.000000002980
Root detected at 0.999999997020
Root detected at 2.000000011921
Root detected at 3.000000011921
Root detected at 4.000000011921
Root detected at 5.000000000000
| |
The name of your C program file must be called lab6q2.c,
files with any other name will not be marked.
Question 3 (Finding Roots Using the Secant Method)
In the Bisection method above, we use the mid-point p of the interval [a,b] as an approximate to the root, and repeat with subsequently smaller intervals until f(p) is close to 0. As such the value of p converges closer and closer to the true value of the root.
A generally faster way of convergence is to use the Secant method (see problem 30 of Etter), in which a more accurate estimate of the root location is usually the intersection of a straight line through the function values with the x-axis. In this respect p is now the intersection point and can be computed as
(a*f(b) - b*f(a)) / (f(b) - f(a)).
Modify the find_root function of program lab6q2.c such that the Secant method is now used to find the roots. You should modularize the computation of the intersection point.
The sample output is given below.
Enter interval limits a, b (a<b):
-5.5
5.5
Enter step size:
1
Root detected at -5.000000001940
Root detected at -4.000000006141
Root detected at -3.000000019529
Root detected at -2.000000010457
Root detected at -1.000000004709
Root detected at 1.000000004709
Root detected at 2.000000010457
Root detected at 3.000000019529
Root detected at 4.000000006141
Root detected at 5.000000001940
Enter interval limits a, b (a<b):
-5.3
5.3
Enter step size:
1
Root detected at -5.000000000001
Root detected at -4.000000003786
Root detected at -3.000000002464
Root detected at -2.000000006970
Root detected at -1.000000007891
Root detected at 1.000000009108
Root detected at 2.000000001602
Root detected at 3.000000002385
Root detected at 4.000000000917
Root detected at 5.000000001890
| |
The name of your C program file must be called lab6q3.c,
files with any other name will not be marked.
Question 4 (Finding Roots Using Newton-Raphson Method)
The line that best approximates the graph of the function at a point on its graph is the tangent line to the graph at that point.
Using this line instead of the secant line gives rise to the Newton-Raphson method. You may google to find out more about the method.
Begin by setting p to be the initial intersection point from the Secant Method. The approximation is given by the following method.
- If f(p) is close to 0, then p is the approximate root.
- Otherwise, set the new p value to be
p - [f(p)/f_dev(p)] where f_dev is the first derivative of f
and repeat step 1.
Work out the derivative of the sinc function and write a separate function sinc_dev.
Be mindful of the case sinc_dev(0).
The sample output is given below.
Enter interval limits a, b (a<b):
-5.5
5.5
Enter step size:
1
Root detected at -5.000000000000
Root detected at -4.000000000000
Root detected at -2.999999999987
Root detected at -1.999999994066
Root detected at -1.000000000000
Root detected at 1.000000000000
Root detected at 1.999999994066
Root detected at 2.999999999987
Root detected at 4.000000000000
Root detected at 5.000000000000
Enter interval limits a, b (a<b):
-5.3
5.3
Enter step size:
1
Root detected at -4.999999999982
Root detected at -3.999999999998
Root detected at -2.999999977538
Root detected at -2.000000000000
Root detected at -1.000000000000
Root detected at -2.999999999188
Root detected at 2.000000000000
Root detected at 2.999999999993
Root detected at 4.000000000000
Root detected at 4.999999998586
| |
The name of your C program file must be called lab6q4.c,
files with any other name will not be marked.
Ponder: Inspect the root values carefully. Why does such a
"phenomenon" happen?
Homework (No need to submit)
Modify the find_root function in the programs of Questions 2, 3 and 4 such that it not only returns the approximate root, but also the number of iterations required for finding the root.
Compare the three root finding methods and find out which has a generally faster convergence.
Note and Ponder
- Assume that all user input is valid unless otherwise stated.
- If your program does not work as you expect (logical errors), use
extra printf statements to print out all the values of your
variables to aid in your debugging.
- Since this is not a Math course, you may post your queries on finding the derivative of sinc in the IVLE forum.
- Most importantly, have lots of fun programming!
This document, index.html, has been accessed 28 times since 25-Jun-24 11:57:13 +08.
This is the 1st time it has been accessed today.
A total of 21 different hosts have accessed this document in the
last 444 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 6 times.
If you're interested, complete statistics for
this document are also available, including breakdowns by top-level
domain, host name, and date.