Answered step by step
Verified Expert Solution
Question
1 Approved Answer
By Octave , Who can help me? Computing - Newton-Raphson Method In this lab you will write a routine which implements the Newton-Raphson Method. Your
By Octave, Who can help me?
Computing - Newton-Raphson Method In this lab you will write a routine which implements the Newton-Raphson Method. Your m-file will accept as input a function, an initial value, a tolerance and a max number of iterations, and will output an estimate for the root within the tolerance, or an error message if the max number of iterations has been reached. Since the m-file accepts only the function and not its derivative, the m-file will need to compute the derivative symbolically. 1. (80 points) At the command line type pkg load symbolic but do not type in syms initpython as we did last time. Initialize a symbolic variable r: syms x and then follow the instructions below to write an m-file implementing the Newton-Raphson Method, run it, and drop a single properly-named diary file in Moodle. Name the m-file NewtonRaphsonMethod.m. (a) Your m-file must accept as input 1) a symbolic function f defined as (note - no anonymous functions this time - no @) f=x*3-2*x*2-6 2) an initial value, 3) a tolerance (use 10-8) and 4) the max number of iterations (use nmax = 100). The output (left unprinted by use of the semicolon) will be the approximate root and the number of iterations needed to calculate it. (b) Since x is a symbolic variable, our iterates will be t(1), t(2), .... In the m-file, below the function definition line and the documentation see Problem 2), initialize t: t(1)-initialvalue; (c) To make way for computing f(tn) we type fh=function_handle(f) so that f(tn) can be computed as fh(t(n)). (d) To find the derivative symbolically in the m-file we type syms x and df-diff(f,x), and similarly to fh we type dfh=function_handle(af). (e) Now we are ready for the loop. We start the loop at n = 2 since we already have t(1). In each iteration of the loop we i. check to see if dfh(t(n-1))==0 and if so print a clearly explained error message, otherwise ii. assign a value to In according to Equation 2.3.1, iii. check whether In is an actual root, and check whether En is an ap- proximate root using abs(t(n)-t (n-1)), and iv. generate a printed row of values of n and xn from your loop by us- ing appropriate printf statements taking into account the specified tolerance, as part of a table with column headers. (f) After the routine exits the loop for whichever reason, assign r = t(n) for the output and perform the usual n = nmax check. Remember that n = nmax does not necessarily mean that we don't have a good approximation! So, calculate f(a) for the user's sake and include a final printf statement "The best approximation to withinStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started