-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCours 11.py
More file actions
114 lines (97 loc) · 3.18 KB
/
Copy pathCours 11.py
File metadata and controls
114 lines (97 loc) · 3.18 KB
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
import numpy as np
#Create a vector from multiple input
#First method:
nel=int(input("Enter the number of elements in the vector: "))
lvec=[]
for i in range(nel):
comp = input("Enter the value of component {} ".format(i))
lvec.append(float(comp))
vec = np.array(lvec)#To create a vector from a list
print(vec)
#2nd method:
nel=int(input("Enter the number of elements in the vector: "))
vec=np.zeros(nel)
for i in range(nel):
comp = input("Enter the value of component {} ".format(i))
vec[i]=float(comp)
print(vec)
#3d method:
lin=input("Enter the components of a vector in a line: ")
smooth=lin.split()
vec=np.float_(smooth)#To put all values as float from a list
print("List: {} ".format(smooth))
print("Vector: {} ".format(vec))
#Create a matrix from multiple input
#First method:
mat=np.zeros((4,3))
for i in range(4):
for j in range(3):
mat[i,j]=float(input("Introduce the value of the element ({}, {}): ".format(i,j)))
print(mat)
#Exercise 1
d=int(input("Enter the dimension: "))
A=np.zeros((d,d))
B=np.zeros((d,d))
for i in range (d):
for j in range(d):
A[i,j]=float(input("Enter the value of the element ({}, {}) for the matrix A: ".format(i, j)))
B[i,j]=float(input("Enter the value of the element ({}, {}) for the matrix B: ".format(i, j)))
print(A)
print(B)
C=np.matmul(A, B)
print(C)
D=np.linalg.inv(C)
print(D)
A1=np.linalg.inv(A)
B1=np.linalg.inv(B)
print(np.matmul(A1, B1))
if np.allclose(D, np.matmul(A1, B1)):#np.allclose to compare 2 matrix
print("its equal")
else:
print("its not equal")
#Exercise 2
A=[[1, 1], [1, 2]]
B=[[4, 1], [3, 1]]
C=[[24, 7], [31, 9]]
A1=np.linalg.inv(A)
B1=np.linalg.inv(B)
X=np.linalg.inv(A)@C@np.linalg.inv(B)
print(X)
print(A@X@B)#@ to multiply 2 or more matrix
#Exercise 3
import math as m
list1=[2.07E-5, 2.62E-7, 3.22E-5, 2.59E-4, 4.87E-5, 1.19E-4, 3.95E-5]
vec=np.array(list)
list2=[]
for element in list:
list2.append(round(-1*m.log10(element), 8))
print (list2)
#Exercise 4
vector=np.linspace(1.0E-9, 5.0E-9, 21)
print(vector)
#Exercise 5
def function(x, x0, s):
res=round((1/m.sqrt(2*m.pi))*m.exp((-1/2)*((x-x0)**2)/s**2), 5)
return res
x0=float(input("Enter the value of x0: "))
s=float(input("Enter the value of s: "))
x_in=float(input("Enter the value of the initial x: "))
x_fi=float(input("Enter the value of the final x: "))
n=int(input("How many x would you like to try ?: "))
X=np.linspace(x_in, x_fi, n, 3)
Y=[]
for element in X:
Y.append(function(element, x0, s))
for i in range(len(X)):
print(round(X[i], 3), " ", Y[i])
#Exercise 6
import statistics as stat
temp_mar = [13.8, 13.3, 13.9, 15.0, 16.4, 20.0, 21.9, 22.3, 22.0, 21.2, 18.8, 16.0]
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December']
temp=np.array(temp_mar)
print(round(stat.mean(temp), 1))
ind_min=temp_mar.index(min(temp))
print(f"{months[ind_min]} has the coldest sea surface and its temperature is {min(temp)}")
ind_max=temp_mar.index(max(temp))
print(f"{months[ind_max]} has the warmest sea surface and its temperature is {max(temp)}")