hw2-partial-solution

.pdf

School

University of Illinois, Urbana Champaign *

*We aren’t endorsed by this school

Course

370

Subject

Mathematics

Date

Apr 3, 2024

Type

pdf

Pages

28

Uploaded by elgelie7

Report
Assignment Instructions Complete the following problems. Then save your notebook and submit it on Gradescope (as an .ipynb file). Make sure you run all code cells (including assertion cells) before saving and submitting; i.e., make sure all required outputs are visible in your submission. Hints: The problem parts are meant to build upon each other (i.e., your functions might call or copy code from earlier functions). Some functions have comments that describe the general process for my solutions. These comments may help you think about and structure your code solutions. There are, of course, many ways to solve these problems. We suggest you add code cells to test your code as you work on it (it is often easier to test code outside of a function). Just delete added cells before submitting. Google (especially stack overflow) is your friend for code questions. Part 0: Supplementary code Run the following cell to access supplementary functions that may help you complete this assignment. You can use the test cells below them to experiment and see what the functions do. In [55]: import numpy as np def chebyshev ( a , b , n ): """Returns a Chebyshev point distribution of n points in the domain [a, b]. Parameters ---------- a : float_like Lower bound of domain (inclusive) b : float_like Upper bound of domain (inclusive) n : integer Number of data points to return Returns ------- x : list_like List or array of data points (in ascending order) Examples -------- >>> chebyshev(2, 5, 4) array([5.0, 4.25, 2.7500000000000004, 2.0]) """
chebyshev: [5.0, 4.25, 2.7500000000000004, 2.0] f_true: 0.07067822452613834 Problem 1: Polynomial interpolation using the monomial basis We discussed in class that when performing polynomial interpolation, a linear system arises to solve for coefficients that express the interpolant in the particular basis used to build . This problem will have you code a solution for performing polynomial interpolation of the true function over the interval using the monomial basis. Use the Chebyshev points described in class. YOUR ANSWER HERE x = [ 0.5 * ( a + b ) + 0.5 * ( b - a ) * np . cos ( j * np . pi / ( n - 1 )) for j in range ( 0 , return x def f_true ( x ): """Returns the value of the true function at x. Parameters ---------- x : float_like Input x value Returns ------- y : float_like Output for f(x) Examples -------- >>> f_true(0.5) array([2. , 2.75, 4.25, 5. ]) 0.07067822452613834 """ return np . sin ( np . cos ( 3 * x )) In [56]: """Check the function""" # test chebyshev function a , b , n = 2 , 5 , 4 x_interpolate = chebyshev ( a , b , n ) print ( 'chebyshev: ' , x_interpolate ) # test f_true function x = 0.5 f = f_true ( x ) print ( 'f_true: ' , f )
1. 1. $$ \begin{bmatrix} c_0 \ c_1 \ c_2 \ c_3 \ \end{bmatrix} $$ Part 1a: Monomial basis (concept question) You will be asked to interpolate using a polynomial of degree with a monomial basis . Before coding your solution, answer the following concept questions in the next Markdown cell. 1. Write out the set of monomial basis vectors, , for your polynomial approximation function with . 2. Symbolically derive the matrix system for using this basis (with ). I.e., show the complete matrix equation. Tips for writing in Jupyter notebooks: Jupyter notebooks allow you to write in Markdown cells (like this one and the next one) and code cells (included later in this file). Markdown is a lightweight markup language that you can use to format text in Jupyter notebooks (and other plaintext text documents). A handy cheatsheet for Markdown syntax can be found here: https://www.markdownguide.org/cheat- sheet/ . One useful aspect of Markdown is that it allows the use of LaTeX for writing math. A handy intro to using LaTeX in Jupyter notebook Markdown cells can be found here: https://personal.math.ubc.ca/~pwalls/math-python/jupyter/latex/ . You can also click inside this cell to see how the matrix equation below (and the math above) can be written in Markdown using LaTeX:
Make sure you run the cell (using SHIFT + ENTER) once you are done editing to display the output. Part 1b: Monomial matrix Complete a function to return the matrix for polynomomial interpolation of degree in the domain (i.e., using a subspace of ) with a monomial basis . Hints: Use Chebyshev points (don't forget you need data points to interpolate a polynomial of degree ). Don't forget Python is 0-indexed. You can initialize a 2-d numpy array (i.e., matrix) of 0's using numpy's zeros() function: https://numpy.org/doc/stable/reference/generated/numpy.zeros.html . Here is a stack overflow post discussing one way to select (or replace) the row of a 2-d numpy array: https://stackoverflow.com/questions/26975769/modify-a-particular-row- column-of-a-numpy-array . You can iterate through the indices of a list AND its elements simultaneously using Python's enumerate() function: https://stackoverflow.com/questions/22171558/what-does- enumerate-mean In [64]: # don't forget to import any Python libraries required for your function... def A_monomial ( a , b , n ): """Returns the A matrix for polynomial interpolation of degree n in the domain [a, Parameters ---------- a : float_like Lower bound of domain (inclusive) b : float_like Upper bound of domain (inclusive) n : integer Polynomial degree Returns ------- A : array_like A matrix for polynomial interpolation satisfying Ac = f """ ### YOUR CODE HERE ### # get interpolation points (Chebyshev) x_inter = chebyshev ( a , b , n + 1 )
solution: [[ 1. 5. 25. 125. ] [ 1. 4.25 18.0625 76.765625] [ 1. 2.75 7.5625 20.796875] [ 1. 2. 4. 8. ]] output: [[ 1. 5. 25. 125. ] [ 1. 4.25 18.0625 76.765625] [ 1. 2.75 7.5625 20.796875] [ 1. 2. 4. 8. ]] TESTS PASSED Part 1c: Monomial solution Complete a function to return: the vector of coefficients for polynomomial interpolation of degree in the domain with a monomial basis , the matrix, the condition number of the matrix. The condition number of a matrix is a measure of how sensitive a matrix system is to perturbation error. The condition number is defined as and varies between and . The larger the condition number, the more sensitive the system is to perturbation. Requirements: Use numpy's linalg.inv() function to calculate the inverse of . Do NOT use numpy's linalg.solve() function for this exercise (or any other pre-built matrix solver). # build A matrix A = np . zeros (( n + 1 , n + 1 )) # initialize n+1 x n+1 matrix of 0's for row , x in enumerate ( x_inter ): # loop through each interpolation point (i.e., e # print(x, row_num) # for debugging row_val = np . zeros ( n + 1 ) # initialize n+1 vector of 0's for current row of for k in range ( n + 1 ): # loop through each column of A (i.e., each basis) row_val [ k ] = x ** k A [ row ] = row_val # update current row of A # # build A matrix (alternate) # A = np.zeros((n + 1, n + 1)) # for row_num, x in enumerate(x_interpolate): # loop through each interpolation po # A[row_num] = [x ** k for k in range(n + 1)] # an alternative 1-liner that c return A In [65]: """Check the function""" a , b , n = 2 , 5 , 3 A = A_monomial ( a , b , n ) # check output A_solution = np . array ([[ 1 , 5 , 25 , 125 ], [ 1 , 4.25 , 18.0625 , 76.765625 ], [ 1 , 2.75 , 7.562 print ( 'solution: ' , A_solution ) print ( 'output: ' , A ) np . testing . assert_allclose ( A , A_solution ) print ( 'TESTS PASSED' )
Use numpy's linalg.cond() function to calculate . Assume your f_true() function is globally defined and accessible within your function. In [70]: # don't forget to import any Python libraries required for your function... def solve_monomial ( a , b , n ): """Returns the solution to polynomial interpolation of degree n in the domain [a, This function assumes an f_true(x) function is globally available for calculating Parameters ---------- a : float_like Lower bound of domain (inclusive) b : float_like Upper bound of domain (inclusive) n : integer Polynomial degree Returns ------- c : array_like c vector of coefficients for polynomial interpolation satisfying Ac = f A : array_like A matrix for polynomial interpolation satisfying Ac = f condition : float_like Condition number for the A matrix """ ### YOUR CODE HERE ### # get interpolation points (Chebyshev) x_inter = chebyshev ( a , b , n + 1 ) # build A matrix (alternate) A = np . zeros (( n + 1 , n + 1 )) for row , x in enumerate ( x_inter ): # loop through each interpolation point (i.e., e A [ row ] = [ x ** k for k in range ( n + 1 )] # an alternative 1-liner that can rep # get f (vector of true function values for interpolation points) f = [ f_true ( x ) for x in x_inter ] # solve matrix system c = np . linalg . inv ( A ) . dot ( f ) # get cond(A) condition = np . linalg . cond ( A ) return c , A , condition In [71]: """Check the function""" a , b , n = 2 , 5 , 3 c , A , condition = solve_monomial ( a , b , n ) # check output c_solution = np . array ([ 27.99638068 , - 26.57603744 , 8.04437345 , - 0.7753138 ]) A_solution = np . array ([[ 1 , 5 , 25 , 125 ], [ 1 , 4.25 , 18.0625 , 76.765625 ], [ 1 , 2.75 , 7.562 condition_solution = 5004.3550034210
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help