-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuppgifter_kapitel3.py
More file actions
executable file
·158 lines (93 loc) · 2.73 KB
/
uppgifter_kapitel3.py
File metadata and controls
executable file
·158 lines (93 loc) · 2.73 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
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
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 8 08:59:36 2021
@author: calvinsmith
"""
import matplotlib.pyplot as plt
import numpy as np
import math
#%% Uppgift 3.1.1 (REDOVISAD)
# 200 equally spaced points between 12 and 18
vec = np.linspace(12,18,200)
# 1) even sum
# from 0 through the whole vector with steplength 2
even_sum = sum(vec[0::2])
print(even_sum)
# 2) odd sum
# from 1 through the whole vector with steplength 2
odd_sum = sum(vec[1::2])
print(odd_sum)
# 3)
# from 10 through the whole vector with steplength 10
ten_sum = sum(vec[10::10])
print(ten_sum)
# 4)
index_sum = sum(vec[[2,5,19,92]])
print(index_sum)
#%% Uppgift 3.1.3 (REDOVISAD)
# 4 x 5 matris med slumpmässiga heltal mellan 0 och 11
mat = np.random.randint(12,size =(4,5))
# 1) row sum
row_sum = np.sum(mat, axis = 1)
# 2) col sum
col_sum = np.sum(mat,axis = 0)
# 3) col with even index
even_col_sum = np.sum(mat[:,[0,2,4]],axis = 0)
# 4) column mean
col_mean = np.mean(mat,axis = 0)
# 5)
mat_mean = sum(mat.reshape(20,1))/20
# 6)
mat_std = np.std(mat.reshape(20,1))
#%% 3.2.1 (REDOVISAD)
#
A = np.random.randint(1,100,size = (4,4))
B = np.random.randint(1,100,size = (4,4))
C = np.hstack((np.vstack((A,B)),np.vstack((B,A))))
D = np.diag(C).reshape((4,2))
#%% Uppgift 3.3.2 (REDOVISAD)
x = np.linspace(-6*math.pi,6*math.pi,100)
type = input("Choose plot type (same,separate or sub):")
if type == "same":
plt.plot(x,np.sin(x),'r',label = 'sin(x)')
plt.plot(x,np.cos(x),'b',label = 'cos(x)')
plt.plot(x,np.sin(x)/x,'g',label = 'sin(x)/x')
plt.legend()
plt.show()
elif type == "separate":
plt.plot(x,np.sin(x),'r')
plt.title('sin(x)')
plt.figure()
plt.plot(x,np.cos(x),'b')
plt.title('cos(x)')
plt.figure()
plt.plot(x,np.sin(x)/x,'g')
plt.title('sin(x)/x')
plt.show()
elif type == "sub":
plt.subplot(3,1,1)
plt.plot(x,np.sin(x),'r', label = 'sin(x)')
plt.legend()
plt.subplot(3,1,2)
plt.plot(x,np.cos(x),'b', label = 'cos(x)')
plt.legend()
plt.subplot(3,1,3)
plt.plot(x,np.sin(x)/x,'g',label = 'sin(x)/x')
plt.legend()
plt.tight_layout()
plt.show()
#%% Uppgift 3.4.2 (REDOVISAD)
def integ_trap(function,limits,step):
a = limits[0]
b = limits[1]
N = round((b-a)/step)
x = np.linspace(a,b,N)
approx = step*(np.sum(eval(function,{'x': x[0:N-1], 'np': np})) +
(eval(function,{'x':x[N-1],'np': np}) +
eval(function,{'x':x[0],'np': np}))/2)
return approx
from scipy import integrate
func = lambda x: np.sin(x)
actual = integrate.quad(func,-3,3)
print(actual[0])