技術屋にゃん兵衛のてくてくらぼ by データウィズ [DataWith]

気の向くままソフトについて書いてます。バリバリエンジニアではないのであくまでも初心者目線で。

ChatGPT で Python のコードを書いてみた(1)

今回は ChatGPT でどれだけ正確なコードが生成されるか試してみた結果を書きます。

 

まずは、Python でいろいろ書いてみます。

良いコードを生成されるには与える「お題」を分かりやすく伝える必要があると思います。

 

今回のお題。

「座標(0, 0)から座標(x km, y km)まで、時速0kmから加速度0.5km/s2で移動するときのPythonのコードを生成して」

です。日本語ではうまく解釈できないこともあると聞いていたので、英語で次のように書きました。

「Generate Python code to move from (0, 0) to (x km, y km) by acceleration 0.5 m from zero speed」

単純に「到達するまでの時間を出して」とすると汎化計算式だけが出てくるような気がしたので、そこはあえてぼかしました。

 

その結果返ってきたものは、、、

<div style="padding: 10px; margin-bottom: 10px; border: 1px solid #333333;">
    

Here's an example Python code that simulates the movement of an object from (0,0) to (x km, y km) with an acceleration of 0.5 m/s^2 from zero speed:

import math

# Set the target coordinates (in kilometers)
x_target = 10
y_target = 5

# Set the initial conditions
x = 0
y = 0
vx = 0
vy = 0
ax = 0.5  # acceleration in x direction (m/s^2)
ay = 0.5  # acceleration in y direction (m/s^2)
dt = 0.01  # time step (in seconds)

# Loop until the target is reached
while x < x_target or y < y_target:
    # Calculate the new velocity
    vx += ax * dt
    vy += ay * dt

    # Calculate the new position
    x += vx * dt
    y += vy * dt

    # Print the current position
    print("x: {:.2f} km, y: {:.2f} km".format(x/1000, y/1000))

# Print the final time
time = math.sqrt*1

 

This code uses a while loop to update the velocity and position of the object in small time steps until it reaches the target coordinates. The velocity and position are updated using the acceleration and time step, which are both set beforehand. The final time taken to reach the target is calculated based on the distance and acceleration in each direction. The results are printed to the console.

Note that this is a simplified model and does not take into account factors such as air resistance or friction.</div> 

 

コメントだけでなく、解説や結果を出力するprint()文までついてかなり親切です。

動くかどうかは別として、素人が書くよりよほどしっかりした書き方になっています。

ターゲットの座標(x, y)も(10, 5)と仮に置いてくれています。

 

さて、実際に動くかやってみますか。

import math

から最後の

print

までをコピーして「acceleration.py」として保存します。

今回は Windows の C ドライブの下に Blog というフォルダーを作ってそこに保存します。

「スタートメニュー」⇒「Python」⇒「IDLE」で Python のシェルを起動します。

「File」⇒「Open」で「acceleration.py」を開きます。

 

IDLE で開いたところ

さて、いよいよ実行です。緊張の一瞬。

「Run」⇒「Run Module」を選択します。

その結果、、、

最初の結果は、おっと、指示を間違えたか。(でも、エラーは出てない)

加速度が「0.5m」になってた!

 

で、

「Generate Python code to move from (0, 0) to (x km, y km) by acceleration 0.5 km from zero speed」

に直して再生してみました。まあ、手で直してもいいんですけど。

 

<div style="padding: 10px; margin-bottom: 10px; border: 1px solid #333333;">

 

Here's an example Python code that simulates the movement of an object from (0,0) to (x km, y km) with an acceleration of 0.5 km/s^2 from zero speed:

 

import math

# Set the target coordinates (in kilometers)
x_target = 10
y_target = 5

# Convert the acceleration from km/s^2 to m/s^2
a = 0.5 * 1000

# Set the initial conditions
x = 0
y = 0
vx = 0
vy = 0
dt = 0.01  # time step (in seconds)

# Loop until the target is reached
while x < x_target or y < y_target:
    # Calculate the new velocity
    vx += a * dt
    vy += a * dt

    # Calculate the new position
    x += vx * dt
    y += vy * dt

    # Print the current position
    print("x: {:.2f} km, y: {:.2f} km".format(x, y))

# Print the final time
time = math.sqrt*2

 

This code is similar to the previous example, but the acceleration is converted from km/s^2 to m/s^2 before being used in the calculation. The results are also printed with a higher precision to show the decimal places.

Note that again, this is a simplified model and does not take into account factors such as air resistance or friction.

</div>

 

ここで驚いたのは、以前に実行したものを覚えてるんですよね。

「This code is similar to the previous example, but the acceleration is converted from km/s^2 to m/s^2 before being used in the calculation. 」

前のものと似ているけど、加速度の単位が m/毎秒毎秒 を km/毎秒毎秒 に変更されたよ、と。

うーむ。過去の実績もノウハウとして蓄積されるということか。

 

で、さっきと同じように IDLE で実行してみると、

正しい実行結果

きっちりなってます。

これからいろいろ試して、可能性を探ろうと思います。

プログラマが要らなくなる世界(いや、「誰でもプログラマの世界」)ももうすぐ。

 

ではでは。

 

 

 

*1:2 * x_target / ax)**2 + (2 * y_target / ay)**2)
print("Time taken: {:.2f} seconds".format(time

*2:2 * x_target / a)**2 + (2 * y_target / a)**2)
print("Time taken: {:.2f} seconds".format(time