LoginSignup
1
2

More than 3 years have passed since last update.

Python で数学グラフを表示してみる

Last updated at Posted at 2021-05-08

Jupyter を使ってやってみます。

べき乗

$y=x^5$

import numpy as np
import matplotlib.pyplot as plt

def exponentiation(x):
    a = 5
    return x**a  # xのa乗

x = np.linspace(0, 10)
y = exponentiation(x)  # y = f(x)

plt.plot(x, y)
plt.xlabel("x", size=14)
plt.ylabel("y", size=14)
plt.grid()
plt.show()

image.png

平方根

$y=\sqrt x$

import numpy as np
import matplotlib.pyplot as plt

def square_root(x):
    return np.sqrt(x)  # xの正の平方根

x = np.linspace(0, 100)
y = square_root(x)  # y = f(x)

plt.plot(x, y)
plt.xlabel("x", size=14)
plt.ylabel("y", size=14)
plt.grid()
plt.show()

image.png

多項式

$y=4x^3+3x^2+2x+1$

import numpy as np
import matplotlib.pyplot as plt

def polynomial(x):
    return 4*x**3 + 3*x**2 + 2*x +1

x = np.linspace(-10, 10)
y = polynomial(x)  # y = f(x)

plt.plot(x, y)
plt.xlabel("x", size=14)
plt.ylabel("y", size=14)
plt.grid()
plt.show()

image.png

三角関数

$y=\sin x$
$y=\cos x$
$y=\tan x$

import numpy as np
import matplotlib.pyplot as plt

def sin(x):
    return np.sin(x)   # sin(x)

def cos(x):
    return np.cos(x)   # cos(x)

def tan(x):
    return np.tan(x)   # tan(x)

x = np.linspace(-2*np.pi, 2*np.pi)  # xの範囲を指定
y_sin = sin(x)
y_cos = cos(x)
y_tan = tan(x)/np.pi/10

plt.plot(x, y_sin, label="sin")
plt.plot(x, y_cos, label="cos")
plt.plot(x, y_tan, label="tan")
plt.legend()

plt.xlabel("x", size=14)
plt.ylabel("y", size=14)
plt.grid()
plt.show()

image.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2