0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonでわかる基礎問題精講数学1a

Posted at

1番

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# x, yの範囲を設定
x_range = np.linspace(-2, 2, 100)
y_range = np.linspace(-2, 2, 100)

# (x+y)^3 の計算
X, Y = np.meshgrid(x_range, y_range)
Z1 = (X + Y) ** 3

# (x-y)^3 の計算
Z2 = (X - Y) ** 3

# 3次元プロット
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# (x+y)^3 のプロット
ax.plot_surface(X, Y, Z1, alpha=0.5, cmap='viridis')

# (x-y)^3 のプロット
ax.plot_surface(X, Y, Z2, alpha=0.5, cmap='plasma')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('(x+y)^3 vs (x-y)^3')

plt.show()




image.png
(x+y)^3は青色、(x-y)^3は紫色

import numpy as np
import matplotlib.pyplot as plt

# 関数定義
def equation_4(x, y):
    return 1 + x + y + x*y

def equation_11(x):
    return np.abs(x) - 1

def equation_12(x):
    return np.sqrt((x - 2)**2) + np.sqrt((x + 1)**2)

# xの範囲
x_values = np.linspace(-10, 10, 400)

# 式をプロット
plt.figure(figsize=(12, 4))

# 4番のプロット
plt.subplot(1, 3, 1)
plt.title('Equation 4')
X, Y = np.meshgrid(x_values, x_values)
Z = equation_4(X, Y)
plt.contourf(X, Y, Z, levels=50)
plt.colorbar()

# 11番のプロット
plt.subplot(1, 3, 2)
plt.title('Equation 11')
plt.plot(x_values, equation_11(x_values))

# 12番のプロット
plt.subplot(1, 3, 3)
plt.title('Equation 12')
plt.plot(x_values, equation_12(x_values))

plt.show()


image.png

13番

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 変数の範囲
m_values = np.linspace(0, 10, 100)
n_values = np.linspace(0, 10, 100)
M, N = np.meshgrid(m_values, n_values)

# 式を計算
Z = M + 2 * np.sqrt(M * N) + N

# 3Dプロット
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(M, N, Z, cmap='viridis')

# 軸ラベル
ax.set_xlabel('m')
ax.set_ylabel('n')
ax.set_zlabel('(√m + √n)^2')

plt.title('(√m + √n)^2')
plt.show()



image.png

import numpy as np
import matplotlib.pyplot as plt
from sympy import symbols, Eq, solve

# 方程式の定義
x = symbols('x')
eq1 = Eq(3*x - 1, 5)
eq2 = Eq(2*x - 3, 5)

# 方程式を解く
sol1 = solve(eq1, x)
sol2 = solve(eq2, x)

# 解を表示
print("Solution 1:", sol1)
print("Solution 2:", sol2)

# グラフの描画
x_vals = np.linspace(-10, 10, 400)
y1_vals = 3*x_vals - 1
y2_vals = 2*x_vals - 3

plt.plot(x_vals, y1_vals, label='3x - 1 = 5')
plt.plot(x_vals, y2_vals, label='2x - 3 = 5')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Graph of Equations')
plt.axhline(0, color='black',linewidth=0.5)
plt.axvline(0, color='black',linewidth=0.5)
plt.grid(color = 'gray', linestyle = '--', linewidth = 0.5)
plt.legend()
plt.show()


image.png

import numpy as np
import matplotlib.pyplot as plt

def square_complete(a, b, c):
    # 2次方程式を平方完成する
    vertex_x = -b / (2 * a)
    vertex_y = a * vertex_x**2 + b * vertex_x + c
    return (a, vertex_x, vertex_y)

def plot_quadratic(a, b, c):
    # 2次方程式をプロットする
    x = np.linspace(-10, 10, 400)
    y = a * x**2 + b * x + c
    plt.plot(x, y, label=f"${a}x^2 + {b}x + {c}$")
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Quadratic Function')
    plt.grid(True)
    plt.legend()

def plot_min_point(a, b, c):
    # 2次方程式の最小値を求めてプロットする
    vertex_x = -b / (2 * a)
    vertex_y = a * vertex_x**2 + b * vertex_x + c
    plt.plot(vertex_x, vertex_y, 'ro', label='Minimum Point')
    plt.legend()

# 係数を設定
a = 1
b = -3
c = 2

# 平方完成
a_complete, vertex_x, vertex_y = square_complete(a, b, c)

# プロット
plot_quadratic(a, b, c)
plot_min_point(a, b, c)

plt.show()

print(f"The minimum value of the quadratic function is {vertex_y} at x = {vertex_x}")



image.png

35番


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 関数の定義
def y(x, a):
    return x**2 + 2*a*x

# プロット範囲の設定
x_values = np.linspace(-10, 10, 100)
a_values = np.linspace(-10, 10, 100)
X, A = np.meshgrid(x_values, a_values)
Y = y(X, A)

# プロット
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, A, Y, cmap='viridis')

# 軸ラベルの設定
ax.set_xlabel('x')
ax.set_ylabel('a')
ax.set_zlabel('y')

plt.title('y = x**2 + 2*a*x')

plt.show()


image.png

37番

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.optimize import minimize
from sympy import symbols, diff, hessian

# 関数の定義
def func(x):
    return x[0]**2 - 2*x[0]*x[1] + 2*x[1]**2 + 2*x[0] - 4*x[1] + 3

# 最小化するための初期推測値
initial_guess = [0, 0]

# 最小化の実行
result = minimize(func, initial_guess)

# 最小値
min_value = result.fun
print("Minimum value:", min_value)

# ヘッセ行列の定義
x, y = symbols('x y')
hessian_matrix = hessian(x**2 - 2*x*y + 2*y**2 + 2*x - 4*y + 3, (x, y))

# ヘッセ行列の計算
hessian_at_min = np.array(hessian_matrix.subs({x: result.x[0], y: result.x[1]})).astype(np.float64)
print("Hessian matrix at minimum:")
print(hessian_at_min)

# 3次元プロット
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x_vals = np.linspace(-5, 5, 100)
y_vals = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x_vals, y_vals)
z = func([x, y])

ax.plot_surface(x, y, z, cmap='viridis', alpha=0.7)
ax.scatter(result.x[0], result.x[1], min_value, color='red', s=100, label='Minimum')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.title('3D Plot of the Function')
plt.legend()
plt.show()



image.png

64番


import numpy as np
import matplotlib.pyplot as plt

# 角度の範囲を設定(0から2πまで、ステップサイズは0.01)
angles_radians = np.arange(0, 2*np.pi, 0.01)

# 三角関数を計算
sin_values = np.sin(angles_radians)
cos_values = np.cos(angles_radians)
sum_of_squares = np.sin(angles_radians)**2 + np.cos(angles_radians)**2

# グラフを描画
plt.figure(figsize=(10, 6))

plt.plot(angles_radians, sin_values, label='sinθ')
plt.plot(angles_radians, cos_values, label='cosθ')
plt.plot(angles_radians, sum_of_squares, label='sin^2(θ) + cos^2(θ)')

plt.title('Trigonometric Functions')
plt.xlabel('Angle (radians)')
plt.ylabel('Value')
plt.legend()
plt.grid(True)

plt.show()



image.png

90番
import matplotlib.pyplot as plt
import numpy as np

# 作成する整数の範囲を指定
x = np.arange(-6, 7)

# 式を定義
y = 5*x**2 + 16*x + 3

# プロット
plt.plot(x, y, marker='o')

# グリッドを表示
plt.grid(True)

# 軸ラベルを設定
plt.xlabel('x')
plt.ylabel('y')

# グラフを表示
plt.show()



image.png

import numpy as np
import matplotlib.pyplot as plt

# 等式の式を定義
def equation(x, y):
    return 2/x + 1/y

# 点の座標
points = [(4, 2), (3, 3), (1, -1)]

# 点をプロット
for point in points:
    plt.plot(point[0], point[1], 'bo')
    plt.text(point[0], point[1], f'({point[0]}, {point[1]})')

# グラフを描画
x = np.linspace(0.1, 5, 400)
y = np.linspace(-5, 5, 400)
X, Y = np.meshgrid(x, y)
Z = equation(X, Y)
plt.contour(X, Y, Z, levels=[1], colors='r')

# グラフの表示
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plot of Points and Equation')
plt.grid(True)
plt.show()

image.png

127番


import numpy as np
import matplotlib.pyplot as plt

n_values = np.arange(1, 21)  # n = 1 から 20 まで
expression_1_values = 10 * n_values / ((n_values + 5) * (4 + n_values))
expression_2_values = 1 + (4 - n_values) / (n_values * (n_values + 6))

plt.plot(n_values, expression_1_values, label=r'$\frac{10n}{(n+5)(4+n)}$')
plt.plot(n_values, expression_2_values, label=r'$1 + \frac{(4-n)}{n(n+6)}$')
plt.xlabel('n')
plt.ylabel('Value')
plt.title('Values of the Expressions for Natural Numbers')
plt.legend()
plt.grid(True)
plt.show()

image.png



0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?