代写COSC2500/COSC7500—Semester 2, 2021代写留学生R语言
- 首页 >> CSCOSC2500/COSC7500—Semester 2, 2021
Exercises—due 11:00 am Thursday 9th September
Required
The grade of 1–5 awarded for these exercises is based on the required exercises. If all of the required exercises are completed correctly, a grade of 5 will be obtained.
R2.1 Implement the bisection method, fixed-point iteration, and Newton’s method. Compare these for the following tasks:
a) Find the first two positive roots of f(x) = cos(x).
b) Find both roots of f(x) = (x − 1) 3 (x + 2).
You can compare the methods by (a) the number of iterations required to find the roots to some target accuracy and also (b) by the difficulty of choos-ing starting points or intervals that give convergence.
Note that Sauer provides code for bisection and fixed point iteration (Pro-gram 1.1 and Program 1.2). You can use these, or write your own code, or use other code.
Checklist
Did you search for the roots using the required methods?
Did you show the results for each method?
Did you explain how you did it?
Did you compare the methods?
Did you discuss any difficulties?
Did you discuss adjusting fixed-point iteration when needed?
Did you discuss any unusual results?
Did you include code where appropriate?
Did you reference the source of any code you didn’t write?
R2.2 Implement Golden Section Search, Successive Parabolic Interpolation, and Newton’s method for finding minima. Compare these for the following task: find all maxima and minima of
You can compare the methods by (a) the number of iterations required to find the maxima and minima to some target accuracy and also (b) by the difficulty of choosing starting points or intervals that give convergence.
Note that Sauer provides code for golden section search and successive parabolic interpolation iteration (Program 13.1 and Program 13.2). You can use these, or write your own code, or use other code.
Checklist
Did you search for the maxima and minima using the required methods?
Did you show the results for each method?
Did you explain how you did it?
Did you compare the methods?
Did you discuss any difficulties or unusual results?
Did you include code where appropriate?
Did you reference the source of any code you didn’t write?
R2.3 Discuss the code given below. (You are not expected to run this code.)
Note: What kind of things can you write about for a question like this? You might like to answer some of the following questions: What does the code do? What method does it use? Does it appear to be correct? Is there anything unusual or interesting about it?
In this particular case, why might there be code for both Newton’s method and bisection?
Checklist—Your discussion should include:
The general function of the code.
Methods used by the code.
Unusual features of the code.
Does the code appear to be correct? Why?
Are there any problems with the code?
Can you suggest any improvements to the code?
1 function z = find_eq (T ,a , z )
2 % find_eq .m - find equilibrium position along z axis
3 %
4 % Usage :
5 % z = find_eq (T ,a );
6 % z = find_eq (T ,a , initial_guess );
7 % where T = T - matrix , a = field vector .
8
9 newton = true ;
10
11 z_precision = 1e -4;
12 short_distance = 1e -5;
13 zpoints = 45;
14
15 if nargin < 3
16 z = 0;
17 end
18
19 z_old = z + 10* z_precision ;
20
21 if newton
22
23 % Newton ’s method
24 while abs (z - z_old ) > z_precision
25
26 a2 = translate_z (a , z );
27 a3 = translate_z ( a2 , short_distance );
28
29 p = T * a2 ;
30 f1 = force_z ( a2 , p );
31
32 p = T * a3 ;
33 f2 = force_z ( a3 , p );
34
35 dz = short_distance * f1 /( f1 - f2 );
36
37 z_old = z ;
38
39 z = z + dz ;
40
41 end
42
43 else % end of if newton
44
45 % Bisection method
46
47 % Need initial guess
48
49 % Guess the radius
50 size_T = size ( T );
51 radius = size_T (1)/(2* pi );
52
53 z = linspace ( - radius ,3* radius , zpoints );
54
55 fz = zeros ( size ( z ));
56
57 for nz = 1: zpoints
58
59 a2 = translate_z (a , z ( nz ));
60
61 p = T * a2 ;
62 fz ( nz ) = force_z ( a2 , p );
63
64 if fz ( nz ) < 0
65 z1 = z ( nz -1);
66 z2 = z ( nz );
67 f1 = fz ( nz -1);
68 f2 = fz ( nz );
69 break
70 end
71
72 end
73
74 if f1 == 0
75 z = z1 ;
76 return
77 end
78
79 if nz == zpoints
80 error ( ’ Starting points for bisection not found ’ );
81 end
82
83 % Now the actual bisection search
84
85 while z2 - z1 > z_precision
86
87 z3 = ( z1 + z2 )/2;
88
89 a2 = translate_z (a , z3 );
90
91 p = T * a2 ;
92 f3 = force_z ( a2 , p );
93
94 if f3 == 0
95 z = z3 ;
96 break
97 end
98 if f1 * f3 < 0
99 z1 = z3 ;
100 f1 = f3 ;
101 else
102 z2 = z3 ;
103 f2 = f3 ;
104 end
105
106 end
107
108 z = z3 ;
109
110 end % end of if bisection ( ie not newton )
111
112 return
Additional
Attempts at these exercises can earn additional marks, but will not count towards the grade of 1–5 for the exercises. Completing all of these exercises does not mean that 4 marks will be obtained—the marks depend on the quality of the answers. It is possible to earn all 4 marks without completing all of these additional exercises.
A2.1 How are methods for root-finding affected by error? Try adding random errors such as you used in the Module 1 exercises.
A2.2 For fixed-point iteration to converge to a fixed point, the absolute value of the slope need to be less than 1. If we are using fixed-point iteration to find a root (rather than being interested in the fixed-point as such), what restrictions does this place on the original function? Is it possible to trans-form. a function for which the required conditions are not satisfied into one where they are satisfied? Is this possible for only some functions, or for all continuous functions?
A2.3 How are methods for finding minima affected by error? Try adding random errors such as you used in the Module 1 exercises.
A2.4 Find the (absolute, not local) maximum of the function calculated by spikey fn() (provided in spikey fn.m).44 COSC2500/COSC7500—Semester 2, 2021
A2.5 Implement one of the methods from this module in a compiled language of your choice. Compare the performance of your compiled code with your Matlab version.
A2.6 Write code that automatically chooses starting points or intervals for one or more of the methods for root-finding or optimisation in R2.1 or R2.2 that will successfully find at least one root or minimum for each of the cases in those questions. Find a function that it will fail for.
A2.7 Implement a steepest descent method to find minima of a function of two variables (i.e., z = f(x, y)).