-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_of_iterative_eigenvalues.py
146 lines (98 loc) · 4.42 KB
/
copy_of_iterative_eigenvalues.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
"""Copy of Iterative_eigenvalues.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1B0x_Cb_qw7I7GW96sEBcAD7uWgf8p9eZ
#**Lab 9 - Iterative eigenvalues and Markov chains**
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="Lab9"
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**"""
# This function approximates the dominant eigenvector of our matrix A.
def evect_approx1(x_0,k):
A = np.array([[1,1],[2,0]])
x = x_0
for j in range(k):
temp = np.matmul(A,x)
x = temp
return x
evect_approx1(np.array([1,9]),10)
"""**Problem 2**"""
# This function approximates the dominant eigenvalue of our matrix A.
def eval_approx1(x_0,k):
A = np.array([[1,1],[2,0]])
x_k = evect_approx1(x_0,k)
x_k1 = evect_approx1(x_0,k+1)
lambdaVal = x_k1[0]/x_k[0]
return lambdaVal
eval_approx1(np.array([1,9]),10)
"""**Problem 3**"""
# This function approximates the dominant eigenvalue and eigenvector of our matrix A using the normalized iterative process.
def norm_evect_approx1(x_0,k):
A = np.array([[1,1],[2,0]])
x = x_0
for j in range(k):
w = np.matmul(A,x)
v = w[0]/x[0]
x = w/np.linalg.norm(w)
return x, v
norm_evect_approx1(np.array([1,9]),10)
"""**Problem 4**"""
# This function approximates the dominant eigenvalue and eigenvector of an arbitrary matrix using the process described in Problem 4.
def norm_approx_gen(M,x_0,k):
x = x_0
for j in range(k):
w = np.matmul(M,x)
v = w[0]/x[0]
x = w/max(np.abs(w))
return x, v
norm_approx_gen(np.array([[2,4,6],[4,8,0],[1,2,9]]),np.array([1,5,-1]),10)
"""**Problem 5**"""
# This function approximates the dominant eigenvalue and eigenvector of an arbitrary matrix using the Rayleigh quotiend as described in Problem 5.
def ray_quotient(M,x_0,k):
x_k = norm_approx_gen(M,x_0,k)[0]
M1 = np.linalg.norm(M)
X1 = np.linalg.norm(x_k)
ray = (np.dot((np.dot(M1,X1)),X1))/(np.dot(X1,X1))
return ray
ray_quotient(np.array([[2,4,6],[4,8,0],[1,2,9]]),np.array([1,5,-1]),10)
"""**Problem 6**"""
# Replace all of the 0 values with the vectors requested in Problem 6.
#at [0]
x_vect_3= norm_approx_gen(np.array([[3,2,-2],[-1,1,4],[3,2,-5]]),np.array([1,1,1]),3)[0]
x_vect_4=norm_approx_gen(np.array([[3,2,-2],[-1,1,4],[3,2,-5]]),np.array([1,1,1]),4)[0]
"""**Problem 7**"""
# This function returns the number of subscribers to the different streaming services after month k.
def subscriber_vals(x_0,k):
A = np.array([[0.7,0.2],[0.3,0.8]])
x = x_0
for j in range(k):
temp = np.matmul(A,x)
x = temp
return x
subscriber_vals(np.array([95,102]),10)
"""**Problem 8**"""
# Replace all of the 0 values with the value requested in Problem 8.
netflix_subs6=subscriber_vals(np.array([0.6,0.4]),6)[1]
"""**Problem 9**"""
# Replace all of the 0 values with the matrix/vector/value requested in Problem 9.
trans_matrix=np.array([[.8,.5,.3,.2],[.05,.2,.1,.1],[.1,.1,.3,.1],[.05,.2,.3,.6]])
"""**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.
"""