In this problem you will use Newton's Method to find the root of the function f(x)...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
In this problem you will use Newton's Method to find the root of the function f(x) = ax - cos(x), where a is a random scalar value between 2 and 8. Newton's Method is a "simple" method for finding roots of a function. This method takes an existing estimate for the root, xold, and improves it by adding a quantity Ax to obtain a new estimate: Xnew = Xold + Ax, where Ax= The formula for Ax was obtained by approximating f(x) by its tangent line near xold, then solving for the root of the tangent line, Xnew. The value Xnew constitutes a new and (in most cases) improved estimate for the true root, as depicted in the figure below, which shows data for f(x) = x - cos(x): 1.5 0.5 0 -0.5 -1 -1.5 -0.5 -f(x) Ytan (2) Fold Inew Itrue Ar 0 f(xold) f'(Xold) 0.5 1 1.5 This process can be repeated, each time recomputing Ax and replacing the old estimate with the new estimate. Your task is to write a function called NewtonRoot that finds the root of the specified function. Your function should take in an initial guess for the root of the function and then output the value of the root and the numer of iterations required for the alrogithm to converge. An outline of an algorithm using Newton's Method to compute the root of f(x) = ax - cos(x) is provided in the solution template below. : 1. Define f and its derivative fprime corresponding to f(x) = ax cos(x). Do this using anonymous functions so that you can easily evaluate f and fprime for any desired value of x. 2. Set the initial guess for the root (x) equal to the user input to the function. This is the first value of xold- 3. Use the formula above to compute Ax and Xnew- 4. Repeat step 3 until |Ax| < 10-8. This is a reasonable condition for terminating the loop because it means there isn't much change between successive guesses of the root. Note that on successive loops, Xnew from the previous loop should become old for the current loop. You can do this by defining a single variable x and setting up your algorithm properly. Your function should have two inputs: . The first input is the initial guess of the root. . The second input is the variable a, for defining f(x) = ax - cos (x) and f'(x) within the function. Your function should have two outputs: The first output is the value of the root. The second output is the number of iterations required to find the root. For example, if your loop only runs once, the numer of iterations is 1. Notes: Use a while loop in your function. Do not use an existing MATLAB solver to compute the root! In this implementation we only use a single variable, x, to keep track of the current root estimate instead of storing both the new and old values. This is because the old value is no longer needed once the new estimate 1 function [x,iter] NewtonRoot (init_guess, a) 2 3 % Define your anonymous functions and any other necessary variables here 4 % Be sure to use the input variable a properly 5 % Define f and its derivative fprime 6 f = @(x) a*x - cos(x); 7 fprime = @(x) a + sin(x); 8 9 % Initialize variables 10 tolerance = 1e-10; 11 iter = 0; 12 x = guess; 13 14 % Implement Newton's Method 15 while true 16 17 18 19 20 iter iter + 1; fx = f(x); fxprime delta_x = 21 22 23 24 end 25 26 root = x; 27 28 end 29 30 end = = x = x + delta_x; if abs (delta_x) < tolerance break; fprime (x); -fx/ fxprime; In this problem you will use Newton's Method to find the root of the function f(x) = ax - cos(x), where a is a random scalar value between 2 and 8. Newton's Method is a "simple" method for finding roots of a function. This method takes an existing estimate for the root, xold, and improves it by adding a quantity Ax to obtain a new estimate: Xnew = Xold + Ax, where Ax= The formula for Ax was obtained by approximating f(x) by its tangent line near xold, then solving for the root of the tangent line, Xnew. The value Xnew constitutes a new and (in most cases) improved estimate for the true root, as depicted in the figure below, which shows data for f(x) = x - cos(x): 1.5 0.5 0 -0.5 -1 -1.5 -0.5 -f(x) Ytan (2) Fold Inew Itrue Ar 0 f(xold) f'(Xold) 0.5 1 1.5 This process can be repeated, each time recomputing Ax and replacing the old estimate with the new estimate. Your task is to write a function called NewtonRoot that finds the root of the specified function. Your function should take in an initial guess for the root of the function and then output the value of the root and the numer of iterations required for the alrogithm to converge. An outline of an algorithm using Newton's Method to compute the root of f(x) = ax - cos(x) is provided in the solution template below. : 1. Define f and its derivative fprime corresponding to f(x) = ax cos(x). Do this using anonymous functions so that you can easily evaluate f and fprime for any desired value of x. 2. Set the initial guess for the root (x) equal to the user input to the function. This is the first value of xold- 3. Use the formula above to compute Ax and Xnew- 4. Repeat step 3 until |Ax| < 10-8. This is a reasonable condition for terminating the loop because it means there isn't much change between successive guesses of the root. Note that on successive loops, Xnew from the previous loop should become old for the current loop. You can do this by defining a single variable x and setting up your algorithm properly. Your function should have two inputs: . The first input is the initial guess of the root. . The second input is the variable a, for defining f(x) = ax - cos (x) and f'(x) within the function. Your function should have two outputs: The first output is the value of the root. The second output is the number of iterations required to find the root. For example, if your loop only runs once, the numer of iterations is 1. Notes: Use a while loop in your function. Do not use an existing MATLAB solver to compute the root! In this implementation we only use a single variable, x, to keep track of the current root estimate instead of storing both the new and old values. This is because the old value is no longer needed once the new estimate 1 function [x,iter] NewtonRoot (init_guess, a) 2 3 % Define your anonymous functions and any other necessary variables here 4 % Be sure to use the input variable a properly 5 % Define f and its derivative fprime 6 f = @(x) a*x - cos(x); 7 fprime = @(x) a + sin(x); 8 9 % Initialize variables 10 tolerance = 1e-10; 11 iter = 0; 12 x = guess; 13 14 % Implement Newton's Method 15 while true 16 17 18 19 20 iter iter + 1; fx = f(x); fxprime delta_x = 21 22 23 24 end 25 26 root = x; 27 28 end 29 30 end = = x = x + delta_x; if abs (delta_x) < tolerance break; fprime (x); -fx/ fxprime;
Expert Answer:
Answer rating: 100% (QA)
Solutions Step 1 In this problem we are tasked with implementing Newtons method in MATLAB to find the root of a given function The function is defined ... View the full answer
Related Book For
Numerical Methods For Engineers
ISBN: 9780071244299
5th Edition
Authors: Steven C. Chapra, Raymond P. Canale
Posted Date:
Students also viewed these mechanical engineering questions
-
Q1. You have identified a market opportunity for home media players that would cater for older members of the population. Many older people have difficulty in understanding the operating principles...
-
CANMNMM January of this year. (a) Each item will be held in a record. Describe all the data structures that must refer to these records to implement the required functionality. Describe all the...
-
Figure 20.3 describes the Medicare Part D prescription drug benefi t. Look at the Web page www.partd-medicare.com/ in your area and determine the marginal coinsurance rates applicable in each...
-
LRNA Company issued $380,000, 7%, 10-year bonds on January 1, 2019, for $407,968. This price resulted in an effective-interest rate of 6% on the bonds. Interest is payable annually on January 1. LRNA...
-
PepsiCos financial statements are presented in Appendix A. Financial statements for The Coca-Cola Company are presented in Appendix B. Instructions Based on information contained in these fi nancial...
-
Air at \(T_{1}=300 \mathrm{~K}, p_{1}=303 \mathrm{kPa}\), and \(V_{1}=0.5 \mathrm{~m} / \mathrm{s}\) enters the Venturi shown in Fig. P5.43. The air leaves at \(T_{2}=220 \mathrm{~K}\) and \(p_{2}=\)...
-
Huluduey Corporations comparative balance sheet for current assets and liabilities was as follows: Adjust net income of $160,000 for changes in operating assets and liabilities to arrive at net cash...
-
72 Question 1 (50 marks) a) Identify all zero-force members in the truss. [10 marks] b) Using the method of sections, compute the forces in the members BC, BF, EI and EF (identified by x in the...
-
Complete the forecasting worksheets for: Nave Average Moving Average Weighted Moving Average using the weights of .8, .15, and .05 with .8 being the most current, then .15, then .05 Expon A using an...
-
A) Since the economic life of the project is 12 years and the capital cost i= 40%, calculate the uniform value of these expenses. 8000 5600 4400 3200 2000 Is 1 2 3 4 6800 4-7 B 7 8 -400 9 -1600 10 11...
-
o pts Please answer the next question based on the following quotes on currency options contracts for Swiss francs (CHF), where each contract has 62.500 CHFs Exercise prices, call and put premiums...
-
Naz Sdn. Bhd. involves in a car maintenance program. Listed below are the business rules for the company. (1) (2) (3) (4) (5) (6) A customer can own many cars Some customers do not own cars. (a) (b)...
-
Required: 1. Include a logical data-flow diagram of the current dispatching system, 2. Propose improvement to the logical data-flow diagram to the dispatching system and state the expected benefits...
-
Katrina owns undeveloped land with an adjusted basis of $300,000. She exchanges it for other undeveloped land worth $750,000. Assume that Katrina holds the land as an investment. If an amount is...
-
Q2. Merve plans to start a bookstore business. She has obtained the industry ratios to prepare the balance sheet and forecasted a net income of $140,000. Complete the balance sheet using the ratios...
-
Use the information given about the angles and to find the exact value of: (a) sin( + ) (b) cos( + ) (c) sin( - ) (d) tan ( + ) (e) sin(2) (f) cos (2) (g) sin /2 (h) cos/2 cos = 4/5, 0 < < /2; cos =...
-
Roller bearings are subject to fatigue failure caused by large contacts loads F (Figure). The problem of finding the location of the maximum stress along the x axis can be shown to be equivalent to...
-
Economic formulas are available to compute annual payments for loans. Suppose that you borrow an amount of money P and agree to repay it in n annual payments at an interest rate of i. The formula to...
-
Develop well-structured programs to (a) Determine the square root of the sum of the square of the elements of a two-dimensional array (i.e., a matrix) and (b) Normalize a matrix by dividing each row...
-
Amherst Metal Works produces two types of metal lamps. Amherst manufactures 20,000 basic lamps and 5,000 designer lamps. Its simple costing system uses a single indirect-cost pool and allocates costs...
-
Amherst Metal Works produces two types of metal lamps. Amherst manufactures 20,000 basic lamps and 5,000 designer lamps. Its activity-based costing system uses two indirect-cost pools. One cost pool...
-
How do managers refine a costing system?
Foundations Of PyGTK Development GUI Creation With Python 2nd Edition - ISBN: 1484241789 - Free Book
Study smarter with the SolutionInn App