Section 9 Assignments
Subsection 9.1 Assignment Set 1
#Assignment set 1 #Numerical Analysis #September 5, 2019 ############################################## #Exercise 1 ############################################## #Useful commands: #inline(), plot(), abs(), max() ############################################## #A) Plot the functions #f(x) = sin(x) #T7(x) = x - x^3/3! + x^5/5! - x^7/7! #on the same set of axes. # #B) Then, given the domain you've chosen, calculate the maximum error. # #C)Finally, find the largest interval you can so that the maximum absolute true #error is less than 0.1. ############################################## #Exercise 2 ############################################## #Useful commands: #sym, syms, diff, factorial, function_handle, plot ############################################## #A) Write a loop that constructs the degree 7 Taylor expansion of #f(x) = sin(x) #about the point a = 0. # #B) Rewrite your loop so that it can find the Taylor series of degree n about #any center a for f(x) = sin(x). # #C) Rewrite your loop so that it can take an arbitrary function, produce the #degree n Taylor polynomial, and plot the function and the Taylor polynomial #near a. ################################################ #Exercise 3 (optional) ################################################ #Modify your answer to Exercise 2 so that given an error tolerance, #the program produces the largest domain near a for which the maximum error is #less than that tolerance. The output should be both the Taylor polynomial #and a domain where the approximation is good.
Subsection 9.2 Assignment Set 2
#Assignment set 2 #Numerical Analysis #September 13, 2019 ############################################## #Exercise 1 ############################################## ############################################## #useful commands: #for/endfor, while/endwhile, break, @, if/elseif/else/endif ############################################## #A) Plot the function f(x) = x^6 - x - 1. Use your plot #to construct reasonable brackets for the roots of f. # #B) Use the bisection method to compute the roots of f. Set #your tolerance so that the approximation is good to six #significant figures. ############################################## #Exercise 2 ############################################## #Useful commands: #function, return, for, while, break ############################################## #Write a function that takes as input a function, #a bracket [a,b], a tolerance, and a maximum number #of iterations and produces an approximation to the #root in the interval [a,b]. You should consider #adding a logic check to make sure that a root really #is inside the bracket before executing. ################################################ #Exercise 3 (optional) ################################################ #Rewrite the function in Exercise 2 so that the user specifies #a desired number of significant figures instead of a tolerance.
Subsection 9.3 Assignment set 3
################################################ #Exercise 1 ################################################ #Find an approximation for the square root of 2 using #the method of false position. ################################################ #Exercise 2 ################################################ #Find an approximation for the square root of 2 using #the Newton-Raphson method. Compare the number of steps and #the running time with Exercise 1. ################################################ #Exercise 3 ################################################ #Find a function for which your bisection solver runs faster than #your false position solver. Explain what you think is happening. ################################################ #Exercise 4 ################################################ #Find a function that gives you extremely slow convergence #in Newton's method. Explain what you think is happening.
Subsection 9.4 Assignment set 4
################################################ #Exercise 1 ################################################ #Use Newtons' method to find the roots of #f(x) = x^6 - 9x^3 + 2x - 10 ################################################ #Exercise 2 ################################################ #Repeat exercise 1 but using the secant method. ################################################ #Exercise 3 ################################################ #Turn your secant and Newton's method routines into funtions. #You should build in failure checks (that is, the x's get huge #or the answers don't converge). ################################################ #Exercise 4 ################################################ #Write a hybrid method that first tries the secant method to find a root #and then uses the bisection method if the secant method fails.
Subsection 9.5 Assignment set 5 (including challenge set)
################################################ #Exercise 1 (challenge) ################################################ #Implement synthetic division. Input should be an array of #numbers representing the coefficients and one number representing #the point to be evaluated. Output should be the quotient and #the function value. ################################################ #Exercise 2 (challenge) ################################################ #Use the synthetic division method and Newton's method to factor the polynomial #f(x) = x^3 - 2x^2 - 5x + 6 completely. ################################################ #Exercise 3 ################################################ #Find the Lagrange interpolating polynomial for the points #(2,8), (4,2), (8,0.125). #Plot the polynomial and the function f(x) = 2^(5-x), #which generates the points, on the same graph. #Is the Lagrange polynomial a good interpolant? #Is it a good extrapolant? ################################################ #Exercise 4 ################################################ #Find the Lagrange interpolating function for the points #(-3, 4/10), (-2, 4/5), (-1, 2), (0,4), (1, 2), (2, 4/5), (3, 4/10) #These points come from the function f(x) = 1/(x^2 + 1). #What happens? Is the Lagrange polynomial a good approximation #for f? Why or why not?
Subsection 9.6 Assignment set 6 - Newton polynomials
################################################ #Exercise 1 ################################################ #Write a script that produces the coefficients to the Newton #polynomial corresponding to the points (1,1), (2,3), (5,10), (6, -2) #and then produces the polynomial. Verify that the polynomial is #correct by plotting the points and the polynomial on the same axes. ################################################ #Exercise 2 ################################################ #Write a function that takes a matrix containing n #interpolation points and produces the corresponding Newton #polynomial.
Subsection 9.7 Assignment set 7 - Cubic splines
################################################3 #Exercise 1 ################################################ #Compute the cubic spline function that interpolates the points #(-1, 3), (0,1), (1, 3). Plot your function and the points. #################################################### #Exercise 2 ################################################### #Extend your previous result by appending the point (2,0). Pay attention to the organization #of the matrix that you use to find the spline coefficients. ######################################################### #Exercise 3 ########################################################## #Use array appending to build a script that will compute and plot the spline interpolation for #an arbitrary number of points. ############################################################ #Exercise 4 ########################################################### #Sample the function f(x) = 1/(x^2 + 16) with an odd number of equally spaced points. #Compare the graph of the original function, the graph of the unique polynomial interpolating function through #the sample points, and the spline interpolating function through the sample points.
Subsection 9.8 Assignment 8 - Numerical integration
############################################################# #Exercise 1 ################################################################ #Write a function that implements the n segment trapezoid rule #for inputs consisting of an interval (a,b), a number of segments n, #and an anonymous function f. Then wrap that function into a driver #that takes the above inputs plus a tolerance and increases the #number of segments until a given tolerance is met (in absolute #relative error). Compute the area under the function e^(-x^2) from # x=0 to x=2. ############################################################### #Exercise 2 ################################################################# #Write a function that implements Richardson's first order #extrapolation of the trapezoid rule. Compare the speed of #convergence with the trapezoid rule for the function x^2 sin(2x) #on the interval [0,10]. ################################################################ #Exercise 3 ################################################################## #Write a function that implements Simpson's 1/3 rule. Then create a #driver function that takes the same inputs plus a tolerance and #increases the number of segments until the tolerance is met (in #absolute relative error).
Subsection 9.9 Assignment 9 - Fourier series
############################################################## #Exercise 1 ############################################################## #Compute the Fourier coefficients of the function y = x on the #interval [-pi, pi] (this is an example of a sawtooth wave). #It will be easiest to use the trapz function to compute #the trapezoid rule approximations of the sample point vectors. ############################################################## #Exercise 2 ############################################################## #Construct a finite Fourier series approximation for the previous #exercise, and plot it against your function. If you increase the #number of terms, what happens to the approximation near the #discontinuous points?