def P13(): print("---------------------------------") print("P13: Page 127 Problem 7") Q13_X = np.array([-3, 2, -1, 3, 1], dtype=float) Q13_Y = np.array([0, 5, -4, 12, 0], dtype=float) np2latex(np.vstack((Q13_X, Q13_Y)), "Top:X,Bot:Y") Q13_coef = coeffts(Q13_X, Q13_Y) np2latex(transpose(Q13_coef), "\\text{Newton Coef}")
def P11(): print("---------------------------------") print("P11: Page 126 Problem 1") Q11_X = np.array([-1.2, 0.3, 1.1]) Q11_Y = np.array([-5.76, -5.61, -3.69]) np2latex(np.vstack((Q11_X, Q11_Y)), "Top:X,Bot:Y") print(" \\item Neville's Method") Q11_X0_neville = neville(Q11_X, Q11_Y, 0) print(" At x = 0: y = "+str(round(Q11_X0_neville, 5))) print(" \\item Lagrange's Method") Q11_X0_lagrange = lagrange(Q11_X, Q11_Y, 0) print(" At x = 0: y = "+str(round(Q11_X0_lagrange, 5)))
def P12(): print("---------------------------------") print("P12: Page 126 Problem 6") #The program should return "divided difference table" # and "degree of the polynomial" respectively. #Hint: Look at page 113 of the book to learn how to # find the degree of a polynomial from the # divided difference table. Q12_X = np.array([-2, 1, 4, -1, 3, -4], dtype=float) Q12_Y = np.array([-1, 2, 59, 4, 24, -53], dtype=float) np2latex(np.vstack((Q12_X, Q12_Y)), "Top:X,Bot:Y") n=len(Q12_X) #make divided difference table dd = np.zeros([n,n]) dd[:,0] = Q12_Y for i in range(1,n): for o in range(1,i+1): #can recursively find divided difference from previous column dd[i,o] = (dd[i,o-1]-dd[o-1,o-1])/(Q12_X[i]-Q12_X[o-1]) np2latex(dd, "\\text{Divided Diff}") #last nonzero is the order print("Polynomial is of order " + str(max([i for i, arg in enumerate(dd.diagonal()) if arg != 0])))
def P7(): print("P7: Page 78 Problem 5") Q7_A = np.array([[4, -2, 1], [-2, 1, -1], [-2, 3, 6]], dtype=float) Q7_B = np.array([2, -1, 0], dtype=float) np2latex(Q7_A, "A") np2latex(transpose(Q7_B), "B") Q7_X = gausPivot(Q7_A, Q7_B) np2latex(transpose(Q7_X), "X")
def P10(): print("---------------------------------") print("P10: Page 82 Problem 19") for n in [2, 3, 4]: Q10_A = genQ19_A(n) Q10_B = genQ19_B(Q10_A) np2latex(Q10_A, "A_{"+str(n)+"}") np2latex(transpose(Q10_B), "B_{"+str(n)+"}") Q10_X = gausPivot(Q10_A, Q10_B) if Q10_X != "Matrix is singular": np2latex(transpose(Q10_X), "X_{"+str(n)+"}") print("")
def P8(): print("---------------------------------") print("P8: Page 78 Problem 7") Q8_A = np.array([[2, -1, 0, 0], [0, 0, -1, 1], [0, -1, 2, -1], [-1, 2, -1, 0]], dtype=float) Q8_B = np.array([1, 0, 0, 0], dtype=float) np2latex(Q8_A, "A") np2latex(transpose(Q8_B), "B") Q8_X = gausPivot(Q8_A, Q8_B) np2latex(transpose(Q8_X), "X")
def P9(): print("---------------------------------") print("P9: Page 79 Problem 12") #Use your program to solve the equations for k=10 and W=100. k = 10 W = 100 k1, k2, k3, k4, k5 = k, 2*k, k, k, 2*k W1, W2, W3 = 2*W, W, 2*W Q9_A = np.array([[k1+k2+k3+k5, -k3, -k5], [-k3, k3+k4, -k4], [-k5, -k4, k4+k5]], dtype=float) Q9_B = np.array([W1, W2, W3], dtype=float) np2latex(Q9_A, "A") np2latex(transpose(Q9_B), "B") Q9_X = gausPivot(Q9_A, Q9_B) np2latex(transpose(Q9_X), "X")
[-1.84, 3.03, 1.29],\ [-1.57, 5.25, 4.30]], dtype=float) Q1c_A = np.array([[ 2, -1, 0],\ [-1, 2, -1],\ [ 0, -1, 2]], dtype=float) Q1d_A = np.array([[4, 3, -1],\ [7, -2, 3],\ [5, -18, 13]], dtype=float) # Using my functions print("---------------------------------") for A, part in zip([Q1a_A,Q1b_A,Q1c_A,Q1d_A],\ ["a","b","c","d"]): print("Question 1 P 55", part) #print("A = \n",A) np2latex(A, "A") (LU, P) = LUdecomp(A) #print("L + U - I = \n",LU) np2latex(P, 'P') #print("P = \n",P) (L, U) = LUexpand(LU) np2latex(L, 'L') #print("L = \n",L) np2latex(U, 'U') #print("U = \n",U) #print("P^-1 * L * U =\n", np.dot(transpose(P),\ # np.dot(L,U))) #Show that LU works! np2latex(np.dot(transpose(P),np.dot(L,U)),\ 'P^T\cdot L\cdot U') (d, nonsing, cond) = LUnonsingular(A, LU, P) print("Det: ", d) print("Non-singular: ", nonsing)