Question: import numpy as np import scipy.linalg as linalg #import matplotlib.pyplot as plt #------------------------------------------ def vandermonde_matrix(n, datafile=vector_data.txt): #read in the data points from datafile you can

import numpy as np import scipy.linalg as linalg #import matplotlib.pyplot as plt
#------------------------------------------ def vandermonde_matrix(n, datafile="vector_data.txt"):
#read in the data points from datafile you can use np.genfromtxt() z = np.genfromtxt(datafile) m = len(z);
# ## generate the matrix using only one loop # #first, initialize the matrix using np.ones() V =
# then loop to generate V
return V
#------------------------------------------ def vandermonde_LS_solve(V, b): """ solve the least square problem V*x =b using two methods, lstsq() and normal equation. return the two solutions as a tuple (xls, xneqn), where xls is the solution x obtained from lstsq(), and xneqn is the solution x obtained from solving normal equation """ # #call linalg.lstsq(), note if you call as xls=linalg.lstsq( some vatiables ), then you can #not return xls directly (since it is a tuple that contains more items than the required solution) #
# #call linalg.solve() to solve the normal equation #
return xls, xneqn
Prob. 2 Given a vector z that contains all distinct elements 21,22,.. ,zm Generate an nx m, (n > m) Vandermonde-type matrix V that has the following form ,2n-2 2n-2 That is, the j-th column of V is z2k, for k = 0, 1, , n-1; J = 1, in Python the index for z starts from 0 and ends at n - 1.) You code should use only one loop to generate the matrix. That is, you cannot use another loop for the individual element of the matrix. In order to generate the matrix using only one loop, you need to generate it row by row in a vectorized manner, and observe the relationship between two adjacent rows The second task is to solve a least square problem Vb for x, where b is a given vector of suitable length. Solve Vb using two methods: (1) By calling directly the lstsq) function from scipy.linalg; (2) By forming the normal equation. VTVr = Vrb. (you can use . T for transpose and np.dot for product, in order to compute VV and VTb), then call the solve function from scipy.linalg to solve the normal equation. Complete the script p2-vandermonde.py In this script, the two solutions are compared by the norm() function from scipy.linalg. Pay attention to the difference between the two solutions: When the condition number of V is large, (if V is a Vandermonde matrix, usually its condition number, cond(V), is large), the two solutions can be significantly different. The script also tests on matrices that are better conditioned, for which the two solutions should be numerically identical. ...m. (note that
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
