1
1

Pythonでわかる予習シリーズ算数

Last updated at Posted at 2024-04-08
import matplotlib.pyplot as plt
# 水そうに赤い金魚と青い金魚が合わせて17匹います。赤い金魚は青い金魚よりも5匹多いです。青い金魚は何匹いますか。
x+5+x=17
x=6

# ベクトルの始点と終点の座標
x1, y1 = 0, 0
x2, y2 = 6, 0
x3, y3 = 0, 1
x4, y4 = 6+5, 0

# ベクトルをプロット
plt.quiver(x1, y1, x2, y2, angles='xy', scale_units='xy', scale=1, color='blue', label='Blue Vector')
plt.quiver(x3, y3, x4, y4, angles='xy', scale_units='xy', scale=1, color='red', label='Red Vector')

# 軸の設定
plt.xlim(-1, 17)
plt.ylim(-1, 3)

# 軸ラベルの追加
plt.xlabel('X')
plt.ylabel('Y')

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

# 凡例の表示
plt.legend()

# プロットの表示
plt.show()

image.png

import matplotlib.pyplot as plt
# A. B. Cの3人が、カードを合わせて40枚持っています。それぞれが持っているカードの枚数をくらべた ところ、AはBよりも6枚多く、CはAよりも4枚多いことがわかりました。A.B.Cはそれぞれカードを何枚持っていますか。
Bの答え8

# ベクトルの始点と終点を定義
A_start = [0, 0]
A_end = [4 + 8, 0]

B_start = [0, 1]
B_end = [8, 0]

C_start = [0, 2]
C_end = [6 + 8, 0]

# ベクトルをプロット
plt.figure()

plt.quiver(*A_start, *A_end, scale=1, scale_units='xy', angles='xy', color='r', label='A vector')
plt.quiver(*B_start, *B_end, scale=1, scale_units='xy', angles='xy', color='g', label='B vector')
plt.quiver(*C_start, *C_end, scale=1, scale_units='xy', angles='xy', color='b', label='C vector')

# グラフの設定
plt.xlim(-1, 20)
plt.ylim(-1, 5)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Vectors A, B, and C')
plt.grid(True)
plt.legend()

plt.show()

image.png

import numpy as np
import matplotlib.pyplot as plt
#Aさんが、150枚のお皿を洗うアルバイトをしました。お皿を1枚洗うと30円もらえますが、お皿を割ってしまうと、30円がもらえない上に、お皿代の100円を弁償しなければなりません。Aさんは150枚のうち何枚か割ってしまったので、もらえたのは3720円でした。何枚割ってしまいましたか。
# 連立方程式の式
def eq1(x):
    return 150 - x

def eq2(x):
    return (30 * x - 3720) / -100

# xの値の範囲を設定
x_values = np.linspace(0, 200, 400)  # 0から200の範囲を400分割

# 各方程式のy値を計算
y_values_eq1 = eq1(x_values)
y_values_eq2 = eq2(x_values)

# プロット
plt.figure(figsize=(8, 6))
plt.plot(x_values, y_values_eq1, label='x + y = 150')
plt.plot(x_values, y_values_eq2, label='30x - 100y = 3720')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plot of the System of Equations')
plt.legend()
plt.grid(True)
plt.show()

image.png

import numpy as np
import matplotlib.pyplot as plt
#ノート1冊の値段はボールペン1本の値段の2倍で、ノートを2冊とボールペンを5本買うと720円になり ます。ノート1冊、ボールペン1本の値段はそれぞれ何円ですか。

# 方程式を解く
A = np.array([[1, -2], [2, 5]])
b = np.array([0, 720])
solution = np.linalg.solve(A, b)
x = solution[0]
y = solution[1]

print("x =", x)
print("y =", y)

# プロットするためのデータ作成
y_values = np.linspace(0, 150, 400)
x_values = 2 * y_values

# グラフ描画
plt.plot(x_values, y_values, label='x = 2y')
plt.plot(np.linspace(0, 400, 400), (720 - 2 * np.linspace(0, 400, 400)) / 5, label='2x + 5y = 720')
plt.scatter(x, y, color='red', label='Intersection Point (x, y)')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plot of the 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
from mpl_toolkits.mplot3d import Axes3D
#だんごを1個とまんじゅうを1個買うと100円、まんじゅうを1個とどらやきを1個買うと150円、どらや きを1個とだんごを1個買うと130円になります。だんご1個、まんじゅう1個、どらやき1個の値段はそれぞれ何円ですか

# 方程式の係数
A = np.array([[1, 1, 0],
              [0, 1, 1],
              [1, 0, 1]])

# 定数項
b = np.array([100, 150, 130])

# 方程式を解く
solution = np.linalg.solve(A, b)
x, y, z = solution

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

# 方程式の平面をプロット
x_vals = np.linspace(0, 200, 100)
y_vals = np.linspace(0, 200, 100)
X, Y = np.meshgrid(x_vals, y_vals)
Z1 = 100 - X
Z2 = 150 - Y
Z3 = 130 - X

ax.plot_surface(X, Y, Z1, alpha=0.5, rstride=100, cstride=100)
ax.plot_surface(X, Y, Z2, alpha=0.5, rstride=100, cstride=100)
ax.plot_surface(X, Y, Z3, alpha=0.5, rstride=100, cstride=100)

# 解の点をプロット
ax.scatter(x, y, z, color='red')

# 軸ラベル
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

image.png

import matplotlib.pyplot as plt

# 直線の式
def line(x):
    return (25 - 2*x) / 3

# プロットする点の座標
points = [(2, 7), (5, 5), (8, 3), (11, 1)]

# 点のx座標とy座標を取得
x_coords = [point[0] for point in points]
y_coords = [point[1] for point in points]

# 直線のx座標を生成
x_values = list(range(0, 12))
y_values = [line(x) for x in x_values]

# グラフを描画
plt.plot(x_values, y_values, label='2x + 3y = 25')
plt.scatter(x_coords, y_coords, color='red', label='Points')

# プロットの設定
plt.title('Plot of line 2x + 3y = 25 and given points')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()

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

image.png

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# 40x+70y+90z=1300
x+y+z=20
(x,y,z)=(8,10,4)と(6,10,4)と(4,15,1)
# 方程式
def equation(x, y):
    return (1300 - 40*x - 70*y) / 90

# プロットする点の座標
points = [(8, 10, 4), (6, 10, 4), (4, 15, 1)]

# 点のx座標、y座標、z座標を取得
x_coords = [point[0] for point in points]
y_coords = [point[1] for point in points]
z_coords = [point[2] for point in points]

# 3Dプロットの設定
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 方程式をプロット
x = np.linspace(0, 20, 100)
y = np.linspace(0, 20, 100)
X, Y = np.meshgrid(x, y)
Z = equation(X, Y)
ax.plot_surface(X, Y, Z, alpha=0.5, rstride=100, cstride=100)

# 点をプロット
ax.scatter(x_coords, y_coords, z_coords, color='red')

# プロットの設定
ax.set_title('3D Plot of 40x + 70y + 90z = 1300 and given points')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

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

image.png

import matplotlib.pyplot as plt

def plot_points(b, a):
    x_values = [0, b, 2*b, 3*b, 4*b, 5*b, 6*b]
    y_values = [0, a, 0, a, 0, a, 0]
    plt.plot(x_values, y_values, marker='o')
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title('Plot of Points')
    plt.grid(True)
    plt.show()

b = 5  # value of b
a = 3  # value of a

plot_points(b, a)

image.png

import matplotlib.pyplot as plt
import numpy as np

A = 5

# 円の方程式
theta = np.linspace(0, 2*np.pi, 100)
x_circle = A * np.cos(theta)
y_circle = A * np.sin(theta)

# 点の座標
points = [(A, 0), (A, A), (0, 0), (0, A)]

# 直線の座標
line_points = [(A, 0), (0, A)]

# プロット
plt.figure(figsize=(8, 8))

# 円をプロット
plt.plot(x_circle, y_circle, label='Circle')

# 点をプロット
for point in points:
    plt.plot(point[0], point[1], 'ro')

# 線をプロット
for p1, p2 in zip(line_points[:-1], line_points[1:]):
    plt.plot([p1[0], p2[0]], [p1[1], p2[1]], 'g-')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Circle, points, and lines')
plt.axis('equal')
plt.legend()
plt.grid(True)
plt.show()

image.png

import numpy as np
import matplotlib.pyplot as plt
#2本のろうそくA、Bがあります。右のグラフは、AとBに同時に 火をつけてからの時間と、ろうそくの長さの関係を表したものです
# 方程式の定義
def equation1(x):
    return (-18/27) * x + 18

def equation2(x):
    return (-25/20) * x + 25

# xの値の範囲を定義
x_values = np.linspace(-10, 20, 400)

# yの値を計算
y_values1 = equation1(x_values)
y_values2 = equation2(x_values)

# グラフのプロット
plt.plot(x_values, y_values1, label='y=(-18/27)x+18')
plt.plot(x_values, y_values2, label='y=(-25/20)x+25')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid(True)

# 交点を見つける
idx = np.argwhere(np.isclose(y_values1, y_values2, atol=0.01)).flatten()
intersection_x = x_values[idx]
intersection_y = y_values1[idx]

plt.plot(intersection_x, intersection_y, 'ro')  # 交点をプロット
plt.annotate(f'Intersection ({intersection_x[0]:.2f}, {intersection_y[0]:.2f})', xy=(intersection_x[0], intersection_y[0]), xytext=(intersection_x[0]-3, intersection_y[0]+3), arrowprops=dict(facecolor='black', arrowstyle='->'))

plt.show()

image.png

import numpy as np
import matplotlib.pyplot as plt

# 角度をラジアンに変換
angle1 = 110 * np.pi / 180
angle2 = 160 * np.pi / 180

# r = 6
r = 6

# 座標の計算
x1 = r * np.cos(angle1)
y1 = r * np.sin(angle1)

x2 = r * np.cos(angle2)
y2 = r * np.sin(angle2)

# プロット
plt.plot([0, x1], [0, y1], label='(rcos110°, rsin110°)')
plt.plot([0, x2], [0, y2], label='(rcos160°, rsin160°)')

plt.scatter([x1, x2], [y1, y2], color='red')
plt.scatter([x1, x2], [0, 0], color='blue')

# ラベル付け
plt.text(x1, y1, '(rcos110°, rsin110°)', ha='right')
plt.text(x2, y2, '(rcos160°, rsin160°)', ha='left')
plt.text(x1, 0, '(rcos110°, 0)', ha='right')
plt.text(x2, 0, '(rcos160°, 0)', ha='left')

plt.xlabel('x')
plt.ylabel('y')
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.axis('equal')
plt.legend()
plt.title('Plot of points in Cartesian Coordinates')
plt.show()

image.png

import numpy as np
import matplotlib.pyplot as plt

# 直線の式: 100x + 50y + 10*2 = 470
# これを y = mx + b の形式に変換する
# 50y = -100x + 450
# y = (-100/50)x + (450/50)
# y = -2x + 9

# 直線の傾きと切片
m = -2
b = 9

# 直線のx値の範囲を設定
x_values = np.linspace(0, 6, 100)

# 直線のy値を計算
y_values = m * x_values + b

# 直線をプロット
plt.plot(x_values, y_values, label='100x + 50y + 10*2 = 470')

# 点 (4,1)、(3,3)、(2,5) をプロット
points = [(4, 1), (3, 3), (2, 5)]
for point in points:
    plt.scatter(point[0], point[1], color='red')
    plt.text(point[0] + 0.1, point[1] + 0.1, f'({point[0]},{point[1]})')

# グラフの設定
plt.xlabel('x')
plt.ylabel('y')
plt.title('Line Plot')
plt.grid(True)
plt.legend()

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

image.png

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