-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_of_lu_decompositions.py
128 lines (91 loc) · 3.5 KB
/
copy_of_lu_decompositions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
"""Copy of LU_decompositions.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1EJuFl2wiXz9N0AS8pVd2J16l-d90h-7Q
#**Lab 5 - LU decompositions and Gaussian elimination**
Enter your code in the spaces provided. Do not change any of the variable names or function names that are already provided for you. In places where we specify the name of the return value, make sure that your code produces the a value with the correct name.
"""
# Do not edit this cell.
LabID="Lab5"
try:
from graderHelp import ISGRADEPLOT
except ImportError:
ISGRADEPLOT = True
"""**Enter your name, section number, and BYU NetID**"""
# Enter your first and last names in between the quotation marks.
first_name="Madison"
last_name="Wozniak"
# Enter your Math 215 section number in between the quotation marks.
section_number="001"
# Enter your BYU NetID in between the quotation marks. NOT YOUR BYU ID NUMBER!
BYUNetID="mwozniak"
"""**Import NumPy**"""
import numpy as np
"""**Problem 1**"""
def augment(A,b):
matrixA = A
vectorB = b
return np.column_stack((matrixA,vectorB))
c = np.array([[3,2,1,1],[1,-2,1,1],[5,0,1,5]])
b = np.array([-1,3,2])
augment(c,b)
"""**Problem 2**"""
def first_column_zeros(A):
B=np.copy(A)
for i in range(1,len(B)):
B[i] = B[i] - (B[i,0]/B[0,0])*B[0]
return B
A = np.array([[2,1,3,1],[1,2,-1,2.5],[4,2,-1,1]])
first_column_zeros(A)
"""**Problem 3**"""
def row_echelon(A,b):
A = augment(A,b)
for i in range(len(A)):
A[i:,i:] = first_column_zeros(A[i:,i:])
return A
A = np.array([[3.0,1.,-2.],[1.,2.,-5.],[2.,-4.,1.]])
b = np.array([1.1,2,-3])
row_echelon(A,b)
"""**Problem 4**"""
def LU_decomposition(A):
U=np.copy(A)
L=np.identity(len(A))
for j in range(0,len(A)):
for i in range(j+1,len(A)):
L[i,j] = U[i,j]/U[j,j]
U[i,] = U[i,] - (L[i,j]*U[j,])
return L,U
"""**Problem 5**"""
def forward_substitution(L,b): # Accepts a lower triangular square matrix L and a vector b, solves Ly=b for y.
n = len(b)
y = np.zeros(np.shape(b))
for i in range(n):
y[i] = (b[i]-np.dot(y,L[i,]))/L[i,i]
return y
L = np.array([[1,0,0], [3,1,0],[-1.1,2,1]])
b = np.array([-2.1,1,-1])
forward_substitution(L,b)
"""**Problem 6**"""
def back_substitution(U,y):
n = len(y)
x = np.zeros(np.shape(y))
for j in range(n)[::-1]:
x[j] = (y[j]-np.dot(x,U[j,]))/U[j,j]
return x
U = np.array([[2,-3.1,1],[0,1,3],[0,0,4]])
y = np.array([1,-2.1,3])
back_substitution(U,y)
"""**Problem 7**"""
def LU_solver(A,b):
L,U = LU_decomposition(A)
y = forward_substitution(L,b)
x = back_substitution(U,y)
return x
A = np.array([[3,1,-2],[1.5,2,-5],[2,-4,1]])
b = np.array([1.1,3,-2])
LU_solver(A,b)
"""**STOP! BEFORE YOU SUBMIT THIS LAB:** Go to the "Runtime" menu at the top of this page, and select "Restart and run all". If any of the cells produce error messages, you will either need to fix the error(s) or delete the code that is causing the error(s). Then use "Restart and run all" again to see if there are any new errors. Repeat this until no new error messages show up.
**You are not ready to submit until you are able to select "Restart and run all" without any new error messages showing up. Your code will not be able to be graded if there are any error messages.**
To submit your lab for grading you must first download it to your compute as .py file. In the "File" menu select "Download .py". The resulting file can then be uploaded to http://www.math.byu.edu:30000 for grading.
"""