# コメントです
print(1) # 1を表示
'''
コメント1
コメント2
'''
print('Control')
print('制御' + '工学')
print('制御工学' * 5)
print(' \' \" \\ ') # クォーテションやバックスラッシュの表示
print('制御\t 工学 \n' * 5) # タブと改行
len('せいぎょこうがく')
12
0b1010
0xA1
10 / 5
17 // 5
17 % 5
3 ** 2
x = 1
print(x)
print(type(x))
y = 1.0
print(y)
print(type(y))
msg = 'Control'
print(msg)
print(type(msg))
ok = True
print(ok)
print(type(ok))
z = int(y)
print(z)
print(type(z))
word = str(x)
print(word)
print(type(word))
data1 = [3, 5, 2, 4, 6, 1]
data1
data2 = [ [3, 5, 2], [4, 6, 1] ]
data2
data1[0]
data1[0:2]
data1[2:4]
data1 = [3, 5, 2, 4, 6, 1]
data1.append(8)
print(data1)
data1.insert(0, 8)
print(data1)
del data1[0]
print(data1)
data1.pop(0)
print(data1)
x = [ 1, 2, 3]
y = x
y[0] = 10
print(y) # [10, 2, 3]と出力される
print(x) # xの値も[10, 2, 3]に変更される
x = [ 1, 2, 3]
y = x.copy()
y[0] = 10
print(y) # [10, 2, 3]と出力される
print(x) # xの値は変更されずに[1, 2, 3]と出力される
tuple = (1,2,3,4) # tuple = 1,2,3,4 でもよい
print(tuple)
print(type(tuple))
tuple[0] = 5
data = [1, 2, 3, 4]
data[0] = 5
data
d = { 'linestyle': '-.', 'color': 'k' }
print(d)
print(d['linestyle'])
print(d['color'])
x = { 'linestyle':'--', 'label':'plt' }
d.update(x)
print(d)
x = 1
if x < 0:
print('x is negative')
elif x == 0:
print('x is zero')
else:
print('x is positive')
x ,y = -1, -1
if x < 0 and y < 0:
print('x and y are negative')
x, y = 1, -1
if x < 0 or y < 0:
print('x or y is negative')
x = 2
y = [1, 2, 3]
if x in y:
print('x is in y')
x = 0
y = [1, 2, 3]
if x not in y:
print('x is not in y')
for x in [0, 1, 2]:
print(x)
for x in range(0, 3):
print(x)
for i, word in enumerate(['a', 'b', 'c']):
print(i, word)
# プログラム1
for x in range(3):
print(x)
print('python')
# プログラム2
for x in range(3):
print(x)
print('python')
def say_hello():
print('こんにちは')
say_hello()
def subject(name):
print(name + '工学')
subject('制御')
def add(a, b):
c = a + b
return c
result = add(3, 5)
print(result)
def add(*args):
return args[0] + args[1]
value = [3, 5]
result = add(*value)
print(result)
def outer(a, b):
def inner(c):
return c * (a + b)
return inner
f = outer(1, 2)
r = f(3)
print(r)
f2 = outer(3, 4)
r2 = f2(3)
print(r2)
c = (lambda a, b: 2*a + 3*b)(1.0, 4.0)
print(c)
data1 = [1, 2, 3, 4, 5]
data2 = [10, 9, 8, 7, 6]
result = list(map(lambda a, b:2*a + 3*b, data1, data2))
print(result)
def linestyle_generator():
linestyle = ['-', '--', '-.', ':']
lineID = 0
while True:
yield linestyle[lineID]
lineID = (lineID + 1) % len(linestyle)
LS = linestyle_generator()
for i in range(5):
print(next(LS)) # LSの中の yield 部分を1回ずつ実行
t = (1, 2, 3, 4, 5)
r1 = [i for i in t]
print(r1)
r2 = [i for i in t if i % 2 == 0]
print(r2)
from numpy import sqrt
# for文を使って足し合わせる
s=0
for x in range(1,51):
s += x
print(sqrt(s))
# sumを使う
s = sum(range(1,51))
print(sqrt(s))
# generator式を使う
s = sum(x for x in range(1,51))
print(sqrt(s))
import numpy
import numpy as np
from numpy import sqrt
from numpy.linalg import *
import math
math.sin(0.5)
math.log10(2)
math.log(2)
math.pi
math.e
import numpy as np
A = np.array([ [1, 2], [-3, 4]])
print(A)
print(A.T)
B = np.abs(A)
print(B)
np.linalg.det(A)
np.linalg.matrix_rank(A)
x = np.array([1, 2])
print(x)
np.linalg.norm(x)
w, v = np.linalg.eig(A)
print('eigenvalue=',w)
print('eigenvector=\n',v)
C = np.linalg.inv(A)
print(C)
T = np.arange(0, 10, 1)
print(T)
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 4 * np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y, c='k')
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
# plt.savefig("matplot_plotexp.pdf", transparent=True, bbox_inches="tight", pad_inches=0.0)
fig, ax = plt.subplots(figsize=(3, 2.3))
ax.plot(x, y, c='k')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_xticks(np.linspace(0, 12, 7))
ax.set_yticks(np.linspace(-1, 1, 9))
ax.grid()
#fig.savefig("matplot_plotexp.pdf", transparent=True, bbox_inches="tight", pad_inches=0.0)
# fig, ax = plt.subplots(2,1)
fig, ax = plt.subplots(2,1, figsize=(6, 4))
x = np.arange(0, 4 * np.pi, 0.1)
y = np.sin(x)
z = np.cos(x)
w = y + z
ax[0].plot(x, y, ls='-', label='sin', c='k')
ax[0].plot(x, z, ls='-.', label='cos', c='k')
ax[0].set_xlabel('x')
ax[0].set_ylabel('y, z')
ax[0].set_xlim(0, 4*np.pi)
ax[0].grid()
ax[0].legend()
ax[1].plot(x, w, color='k', marker='.')
ax[1].set_xlabel('x')
ax[1].set_ylabel('w')
ax[1].set_xlim(0, 4*np.pi)
ax[1].grid(ls=':')
fig.tight_layout()
# fig.savefig("matplot_plotexp2.pdf", transparent=True, bbox_inches="tight", pad_inches=0.0)
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 13)
y1 = np.repeat(2, 12)
y2 = np.repeat(1, 12)
markers1 = [".", ",", "o", "v", "^", "<", ">", "1", "2", "3", "4", "8"]
markers2 = ["s", "p", "*", "h", "H", "+", "x", "D", "d", "|", "_", "$x$"]
fig, ax = plt.subplots(1,1, figsize=(6, 1))
for i in x-1:
ax.scatter(x[i], y1[i], color='k', s=100, marker=markers1[i])
ax.scatter(x[i], y2[i], color='k', s=100, marker=markers2[i])
ax.set_xticks(np.linspace(1, 12, 12))
ax.set_yticks([1,2])
# fig.savefig("markers.pdf", transparent=True, bbox_inches="tight", pad_inches=0.0)
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
def system(y, t):
if t < 10.0:
u = 0.0
else:
u = 1.0
dydt = (-y + u)/5.0
return dydt
y0 = 0.5
t = np.arange(0, 40, 0.04)
y = odeint(system, y0, t)
fig, ax = plt.subplots(figsize=(3, 2.3))
ax.plot(t, y, label='y', c='k')
ax.plot(t, 1 * (t>=10), ls='--', label='u', c='k')
ax.set_xlabel('t')
ax.set_ylabel('y, u')
ax.legend(loc='best')
ax.grid(ls=':')
# fig.savefig("scipy_demo.pdf", transparent=True, bbox_inches="tight", pad_inches=0.0)
import sympy as sp
sp.init_printing()
s = sp.Symbol('s')
root = sp.solve(2 * s**2 +5*s+3, s)
print(root)
root
f = sp.expand( (s+1)*(s+2)**2, s)
print(f)
f
g = sp.factor(f, s)
print(g)
g