Question: Regarding C programming, more specific threads: use Newton's method to practice programming with the C 1 1 thread library. Give a function f , say

Regarding C programming, more specific threads:
use Newton's method to practice programming with the C11 thread library. Give a function f, say f(x)= x^3-1, where x^3 denotes the third power of x, Newton's method iteratively strives to find a root of f. Starting with a value x_0, one sets x_{k+1}= x_k - f(x_k)/ f'(x_k). Convergence of this is unclear, and even if it does converge, it is unclear what root of f is approached.
sing the C11 thread library a program called newton that computes similar pictures for a given functions f(x)= x^d -1 for complex numbers with real and imaginary part between -2 and +2. The program should write the results as PPM files (see the description of PPM below) to the current working directory. The files should be named newton_attractors_xd.ppm and newton_convergence_xd.ppm, where d is replaced by the exponent of x that is used.
Your program should accept command line arguments
./newton -t5-l10007./newton -l1000-t57
The last argument is d, the exponent of x in x^d -1. The argument to -l gives the number of rows and columns for the output picture. The argument to -t gives the number of threads used to compute the pictures. For example, the above command computes images of size 1000x1000 for the polynomial x^7-1. It uses 5 threads for the Newton iteration.
Implement details: Iteration
If x_i is closer than 10^-3 to one of the roots of f(x), then abort the iteration.
Newton iteration does not converge for all points. To accommodate this, abort iteration if x_i is closer than 10^-3 to the origin, if the absolute value of its real or imaginary part is bigger than 10^10, or if the iteration takes 128 steps ore more. In the pictures treat these cases as if there was an additional zero of f(x) to which these iterations converge.
You have to run the iteration for each point until either of these criteria is met. In particular, the cut off for the number of iterations mentioned later may only be applied after completing the iteration. Implementation details: Simplifications
You may assume that the degree is small, i.e. less than 10, and hardcode optimal formulas case by case.
You may assume that the number of lines is not too large, i.e. less than 100000. It is advantageous to implement the writing step prior to the computation step, since you will have accessible feedback once the writing is in place. To guarantee meaningful input values in the writing thread add the following initialization to your computing threads:
for ( size_t cx =0; cx < nmb_lines; ++cx ){ attractor[cx]=0; convergence[cx]=0; }
Make sure that the attractor and convergence value 0 indeed is assigned meaning in your encoding. Computation
It remains to implement the computation in order to complete the program. Since you can use functionality from complex.h and you can hardcode your formula, this step is largely about finding an expression for the iteration step that is as efficient as possible. Recall that using cpow is not an option.
Inserting the Newton iteration step naively, you obtain x -(x^d -1)/(d*x^(d-1)). How can you simplify it?
When hardcoding the expression that you derive from this, the following syntax is convenient:
switch ( degree ){ case 1: STATEMENTS FOR DEGREE 1; break; case 2: STATEMENTS FOR DEGREE 2; break; case 3: STATEMENTS FOR DEGREE 3; break; // insert further cases default: fprintf(stderr, "unexpected degree
"); exit(1); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!