Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the objective function must take a single double as its argument and return a double. You can find the this function, and a test harness,

image text in transcribedimage text in transcribed

the objective function must take a single double as its argument and return a double. You can find the this function, and a test harness, on the course website at Bi section.cpp, or on the bitbucket repo. This implementation of the bisection algorithm has several shortcoming. Your task is to make as many of the following improvements as possible. Make this change to your code. Verify it works by finding the maximum of two different objective func- tion (a) The termination criterion is that the interval con- taining the maximum is less that 10-6. Different ap- plications may require different bounds. Introduce a global variable for this bound, the value of which the user can choose 3 Algorithm 2: Newton The Bisection method just described is quite robust: pro- viding that the function is continuous, it will find an approximation of its maximum in the desired interval However, there are much faster methods, the most im- portant being Neuton's Method for Optimisation: choose an initial guess to and set 1+1 = -f'()/(a) for k = 0,1,2,.... (b) To verify the efficiency of an optimisation algorithms we need to record some basic statistics of its opera- CTA tion. At the very least, we should count the number of iterations (i.e., steps through the loop) that were required.? Modify your coxkt so that a variable which records the number of iterations is passed (by reference) the bisection function. This should be reported by the main() function. What data type will you use for this new variable? Why? Implement this method. Note that it is different from Bisection in that one only provides a single initial guess, and also that we must provide both f' and '", 4 Assignment (c) It is possible that the while loop in the bisection function des not terminate. To avoid an infinite loop,ack an argument to the bisection () function that controls the maximum number of iterations the algorithm will take Write a programme that implements the Newton Al- orithm, and also implements the refinements in parts (a) (d): (i) The user should be prompted for a tolerence and a maximum number of iterations. . In your codde, it should have a default value (in the argument list) of 10; Theiser should be prompted for their preferred (ii) The algorithms shoukl iterate until the absolute value of the difference between two sixessive iter- ations is less than this tolerence, or the number of iteration exced the maximum. (iii) The code should output the estimate produced by Newton's Algorithm, and the number of iterations required A suitable warning message should be generated if the convergence is not achieved. (d) The program provided with this lab finds the maxi- mum of the objective function f(x) = -21-2x + 4 in the interval (-1,3). To make our Bisection() im- plementation more general, we would like to pass the name of the objective function to be optimised as one of the parameters. This is reasonably easy in C++. Just add something like the following to the param- eter list: double ObjFn(double) (iv) Test your code by finding the local maximum of the function 9(x) = x + sin(2x) in the interval (-1,2], with an initial guess of 20 = 0.5. Then : Submit your code though the labs section of Blackboard by noon, Thursday 13 Feb. you give the name of the objective function you would like optimised as an argument to the bi- section function; In C++ Variable local to the function, or code block, where it is defined. However, if the variable is defined outside of the main() function, then it is global, and shared by all function. It is very rare that the use of global variables is good practice; it reduce modularity and complicates code re-ine. We are using one here just so that we know how they work. Typically, the most expensive part of the computation is the function call to f(). So we should really count the number of times that evaluated. the objective function must take a single double as its argument and return a double You can find the this function, and a test harness, on the course website at Bi section.cpp, or on the bitbucket repo. This implementation of the bisection algorithm has several shortcoming, Your task is to make as many of the following improvements as possible. Make this change to your code. Verify it works by finding the maximum of two different objective fino tions. 3 Algorithm 2: Newton (a) The termination criterion is that the interval con- taining the maximum is less that 10-6. Different ap- plications may require different bound. Introduce a global variable for this bound, the value of which the US can chaxe, The Bisection method just described is quite robust: pro- viding that the function is continuous, it will find an approximation of its maximum in the desired interval. However, there are much faster methods, the most im- portant being Neuton's Method for Optimisation: choose an initial guess to, and set (b) To verify the efficiency of an optimisation algorithms we need to record some basic statistics of its opera- tion. At the very least, we should count the number of iterations (ie, steps through the loop) that were required.2 2X+1 = -f()/ (a) for k=0,1,2,.... Modify your cock so that a variable which records the number of iterations is passed (by reference) the bisection function. This should be reported by the main() function. What data type will you use for this new variable? Why? Implement this method. Note that it is different from Bisection in that one only provides a single initial guess, and also that we must provide both land / 4 Assignment (c) It is possible that the while loop in the bisection function does not terminate. To avoid an infinite loop, acki an argument to the bisection () function that controls the maximum number of iterations the algorithm will take Write a programme that implements the Newton Al- gorithm, and also implements the refinements in parts (a)-(d): (i) The user should be prompted for a tolerence and a maximum number of iterations. (ii) The algorithms shoukl iterate until the absolute value of the difference bewteen two sucessive iter- ations is less than this tolerence, or the number of iterations exceeds the maximum. In your code, it should have a default value in the argument list) of 10; The ser should be prompted for their preferred value; A suitable warning message should be generated if the convergence is not achieved (d) The program provided with this lab finds the maxi- mum of the objective function f(x) = -2-2 +4. in the interval (-1,3). To make our Bisection() im- plementation more general, we would like to pass the name of the objective function to be optimised as one of the parameters. This is reasonably easy in C++. Just add something like the following to the param- eter list: double ObjFn(double) Then: (iii) The code should output the estimate produced by Newton's Algorithm, and the number of iterations required. (iv) Test your code by finding the local maximum of the function 9(x) = + sin(2x) in the interval (-1,2], with an initial guess of Xo = 0.5. Submit your code though the labs section of Blackboard by noon, Thursday 13 Feb. you give the name of the objective function you would like optimised as an argument to the bi- section function; in C++ & variable local to the function, or code block, where it is defined. However, if the variable is defined outside of the main() function, then global, and shared by all functions. It is very rare that the use of global variables is good practice; it reduce modularity and complicates code re-e. We are using one here just so that we know how they work. Typically, the most expensive part of the computation is the function call to f(). So we shoukl relly count the number of times that evaluated. the objective function must take a single double as its argument and return a double. You can find the this function, and a test harness, on the course website at Bi section.cpp, or on the bitbucket repo. This implementation of the bisection algorithm has several shortcoming. Your task is to make as many of the following improvements as possible. Make this change to your code. Verify it works by finding the maximum of two different objective func- tion (a) The termination criterion is that the interval con- taining the maximum is less that 10-6. Different ap- plications may require different bounds. Introduce a global variable for this bound, the value of which the user can choose 3 Algorithm 2: Newton The Bisection method just described is quite robust: pro- viding that the function is continuous, it will find an approximation of its maximum in the desired interval However, there are much faster methods, the most im- portant being Neuton's Method for Optimisation: choose an initial guess to and set 1+1 = -f'()/(a) for k = 0,1,2,.... (b) To verify the efficiency of an optimisation algorithms we need to record some basic statistics of its opera- CTA tion. At the very least, we should count the number of iterations (i.e., steps through the loop) that were required.? Modify your coxkt so that a variable which records the number of iterations is passed (by reference) the bisection function. This should be reported by the main() function. What data type will you use for this new variable? Why? Implement this method. Note that it is different from Bisection in that one only provides a single initial guess, and also that we must provide both f' and '", 4 Assignment (c) It is possible that the while loop in the bisection function des not terminate. To avoid an infinite loop,ack an argument to the bisection () function that controls the maximum number of iterations the algorithm will take Write a programme that implements the Newton Al- orithm, and also implements the refinements in parts (a) (d): (i) The user should be prompted for a tolerence and a maximum number of iterations. . In your codde, it should have a default value (in the argument list) of 10; Theiser should be prompted for their preferred (ii) The algorithms shoukl iterate until the absolute value of the difference between two sixessive iter- ations is less than this tolerence, or the number of iteration exced the maximum. (iii) The code should output the estimate produced by Newton's Algorithm, and the number of iterations required A suitable warning message should be generated if the convergence is not achieved. (d) The program provided with this lab finds the maxi- mum of the objective function f(x) = -21-2x + 4 in the interval (-1,3). To make our Bisection() im- plementation more general, we would like to pass the name of the objective function to be optimised as one of the parameters. This is reasonably easy in C++. Just add something like the following to the param- eter list: double ObjFn(double) (iv) Test your code by finding the local maximum of the function 9(x) = x + sin(2x) in the interval (-1,2], with an initial guess of 20 = 0.5. Then : Submit your code though the labs section of Blackboard by noon, Thursday 13 Feb. you give the name of the objective function you would like optimised as an argument to the bi- section function; In C++ Variable local to the function, or code block, where it is defined. However, if the variable is defined outside of the main() function, then it is global, and shared by all function. It is very rare that the use of global variables is good practice; it reduce modularity and complicates code re-ine. We are using one here just so that we know how they work. Typically, the most expensive part of the computation is the function call to f(). So we should really count the number of times that evaluated. the objective function must take a single double as its argument and return a double You can find the this function, and a test harness, on the course website at Bi section.cpp, or on the bitbucket repo. This implementation of the bisection algorithm has several shortcoming, Your task is to make as many of the following improvements as possible. Make this change to your code. Verify it works by finding the maximum of two different objective fino tions. 3 Algorithm 2: Newton (a) The termination criterion is that the interval con- taining the maximum is less that 10-6. Different ap- plications may require different bound. Introduce a global variable for this bound, the value of which the US can chaxe, The Bisection method just described is quite robust: pro- viding that the function is continuous, it will find an approximation of its maximum in the desired interval. However, there are much faster methods, the most im- portant being Neuton's Method for Optimisation: choose an initial guess to, and set (b) To verify the efficiency of an optimisation algorithms we need to record some basic statistics of its opera- tion. At the very least, we should count the number of iterations (ie, steps through the loop) that were required.2 2X+1 = -f()/ (a) for k=0,1,2,.... Modify your cock so that a variable which records the number of iterations is passed (by reference) the bisection function. This should be reported by the main() function. What data type will you use for this new variable? Why? Implement this method. Note that it is different from Bisection in that one only provides a single initial guess, and also that we must provide both land / 4 Assignment (c) It is possible that the while loop in the bisection function does not terminate. To avoid an infinite loop, acki an argument to the bisection () function that controls the maximum number of iterations the algorithm will take Write a programme that implements the Newton Al- gorithm, and also implements the refinements in parts (a)-(d): (i) The user should be prompted for a tolerence and a maximum number of iterations. (ii) The algorithms shoukl iterate until the absolute value of the difference bewteen two sucessive iter- ations is less than this tolerence, or the number of iterations exceeds the maximum. In your code, it should have a default value in the argument list) of 10; The ser should be prompted for their preferred value; A suitable warning message should be generated if the convergence is not achieved (d) The program provided with this lab finds the maxi- mum of the objective function f(x) = -2-2 +4. in the interval (-1,3). To make our Bisection() im- plementation more general, we would like to pass the name of the objective function to be optimised as one of the parameters. This is reasonably easy in C++. Just add something like the following to the param- eter list: double ObjFn(double) Then: (iii) The code should output the estimate produced by Newton's Algorithm, and the number of iterations required. (iv) Test your code by finding the local maximum of the function 9(x) = + sin(2x) in the interval (-1,2], with an initial guess of Xo = 0.5. Submit your code though the labs section of Blackboard by noon, Thursday 13 Feb. you give the name of the objective function you would like optimised as an argument to the bi- section function; in C++ & variable local to the function, or code block, where it is defined. However, if the variable is defined outside of the main() function, then global, and shared by all functions. It is very rare that the use of global variables is good practice; it reduce modularity and complicates code re-e. We are using one here just so that we know how they work. Typically, the most expensive part of the computation is the function call to f(). So we shoukl relly count the number of times that evaluated

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions