Matlab assignment. Please see instruction file attached and supplemental code files
MATH 209 Spring 2018 MATLAB ASSIGNMENT Due Saturday, June 30 at 11:59pm. Instructions: Assigments will be submitted electronically using the dropbox in D2L. Save your function to a file called newton bisect.m, and upload this file to the dropbox. D2L will automatically stop accepting submissions at 11:59pm on the due date. Create a function in MATLAB called newton bisect which combines New- ton’s method and the bisection method in a way that uses the best features of each. The algorithm works as follows: 1. Start with an interval [a1, b1] that contains a root of the function f . As your first approximation x1, select x1 = a1. 2. Perform an iteration of Newton’s method on x1 to obtain x2. • If x2 is between a1 and b1, then set either [a2, b2] = [a1, x2] or [a2, b2] = [x2, b1] depending on which contains the root of the func- tion. • If x2 is not between a1 and b1, then use x2 = a1 + b1 2 instead, and set either [a2, b2] = [a1, x2] or [a2, b2] = [x2, b1] depending on which contains the root of the function. 3. Repeat step 2 using x2 and the interval [a2, b2]. Example: Consider the function f(x) = 8x3 − 32x2 + 36x− 19 on the interval [2, 3]. f(2) = −11, and f(3) = 17, so the function has a root between 2 and 3. Apply Newton’s Method to f with x1 = 2 to obtain x2 = 19 4 , but 19 4 is not between 2 and 3, so instead we set x2 = 5 2 (the midpoint of 2 and 3). f( 52 ) = −4, so the root must be between 5 2 and 3 (i.e. [a2, b2] = [ 5 2 , 3]). Now apply Newton’s Method to f using x2 = 5 2 to obtain x3 = 69 26 . Since 69 26 is between 5 2 and 3, we will use this value for x3. f( 6926 ) = 1520 2197 , so the root must be between 5 2 and 69 26 . Continue by applying Newton’s Method to x3 = 69 26 . 1 Your function should take 6 inputs: • f:The function whose root you are trying to find • fp: The derivative of the above function • a: The lower limit of your starting interval • b:The upper limit of your starting interval • tol: A tolerance - how small you want the interval to be • nmax: A maximum number of iterations Your function must have four outputs: • xn: The approximation of the root • an: The lower limit of the final interval • bn: The upper limit of the final interval • n: The number of iterations used • ex: An estimate of the error in your approximation: |xn − xn−1| Your function should stop if either the difference between successive values of x is less than tol, or nmax iterations have been completed. The first line of your function should be function [ xn , an , bn , n , ex ] = newton b i s ec t ( f , df , a1 , b1 , to l , nmax ) Examples of functions that perform Newton’s method and the bisection method have been provided in D2L as a starting point. You may use any code from these functions to build your function. If you use code obtained from any other source, it must be clearly cited in a comment in your function. You may work with other students on the assignment, but your submission must be your own work. Students submitting identical MATLAB files will be given a grade of 0, and will be reported for academic misconduct. 2 You will be graded on the following criteria: • 10 Marks Does your program provide the correct output? Your function must properly perform the algorithm described, and give the correct values. Some test cases for the function are provided at the end of this document. You can see if your function gives the same output. Your function will be tested against these and additional inputs to see if it works correctly. • 4 Marks Proper documentation of your function. At the beginning of your function, you must write a description of the inputs and output of the function, and how to use it. Each action performed by your function should be preceded by a comment line describing what is happening. • 3 Marks Error checking / data validation In addition to performing the algorithm, your function should also do the following: – If there is not a root of the function between a and b, then display a message indicating this, and stop the program. – If a maximum number of iterations is not specified, you should per- form a maximum of 10 iterations. – If a tolerance is not specified, your function should assume a tolerance of 0.01. – If nmax iterations are completed, then in addition to the outputs, your function should display the following message: The maximum number of iterations has been reached. • 3 marks Does your program use more memory or computations than are necessary? Hints: – Don’t store more numbers than you need to between calculations. If you are using any vectors in your function, then you are using more memory than is necessary for this program. – Don’t repeat identical calculations. The only operations that should happen inside a for or while loop are those that change each time you run through the loop. – The most time-consuming operation is evaluating a function. Do not evaluate f or fp more times than is necessary. 3 Here is a sample of what should happen when you call your function: >> format long >> syms x >> f ( x)=8∗xˆ3−32∗xˆ2+36∗x−19; >> fp=d i f f ( f ) ; >> [ xn , an , bn , n , ex ] = newton b i sec t ( f , fp , 2 , 3 , 0 . 0 0 1 , 1 0 ) xn = 2.633822784908562 an = 2.500000000000000 bn = 2.633822784908562 n = 4 ex = 3.592120383184572 e−04 >> f ( x)=xˆ3+3∗x−5; >> fp=d i f f ( f ) ; >> [ xn , an , bn , n , ex ] = newtbis ( f , fp , −5 ,5 ,0 .01 ,5 ) The maximum number o f i t e r a t i o n s has been reached xn = 1.165785214010182 an = −0.466936963004882 bn = 1.165785214010182 n = 5 ex = 0.146822534337383 >> f ( x)=xˆ3+3∗x−5; >> fp=d i f f ( f ) ; >> [ xn , an , bn , n , ex ] = newtbis ( f , fp , −5 ,5 ,0 .01 ,10) xn = 1.154171497365509 an = −0.466936963004882 bn = 1.154171497365509 n = 7 ex = 6.642999423811524 e−05 4 (Note: for the above example, if you did the data validation portion, you should be able to enter [xn,an,bn,n,ex] = newtbis(f,fp,−5,5) and get the same result. >> f ( x)=exp(2∗x)−20∗x ˆ5 ; >> fp=d i f f ( f ) ; >> [ xn , an , bn , n , ex ] = newtbis ( f , fp , 1 , 7 , 0 . 0 0 1 , 1 0 ) xn = 5.960934169163164 an = 5.500000000000000 bn = 5.960934169163164 n = 9 ex = 1.127101109910100 e−04 5