質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

333閲覧

pythonで、if文の中にあるwhile文が上手く動作していないので、どうすればよいか教えていただけますでしょうか。

DAYANVICIEDO

総合スコア1

Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2023/11/30 01:10

ルンゲクッタ法を用い、化学反応の数値計算を行うプログラムです。
while文の中のif文で、t==2のときにAに値を代入したいのですが、できません。回答よろしくお願いいたします。

#中間体の生成を無視した場合
from sys import getsizeof
import numpy as np
import matplotlib.pyplot as plt

k1 = 1
k2 = 1
k3 = 1
k4 = 1

def reaction_equations(concentrations):
# Define the system of ordinary differential equations
# Modify this function according to your specific reaction system
A, B, C, D, E, F = concentrations
dAdt = - k1 * A * k4 * F
dBdt = (k1 * A) * (k4 * F) - k2 * B * k2 * B * k2 * C
dCdt = - k2 * C * k2 * B * k2 * B
dDdt = k2 * B * k2 * B * k2 * C
dEdt = k2 * B * k2 * B * k2 * C - k3 * E
dFdt = k3 * E - k4 * F * k1 * A

return [dAdt, dBdt, dCdt, dDdt, dEdt, dFdt]

def runge_kutta(dt, t_max, initial_concentrations):
# Perform the simulation using the fourth-order Runge-Kutta method
t = 0.0
concentrations = np.array(initial_concentrations)
time_points = [t]
concentration_points = [concentrations]

while t < t_max: if t == 2: A += 1.0 else: # Compute concentration changes using the fourth-order Runge-Kutta method k1 = reaction_equations(concentrations) k2 = reaction_equations(concentrations + 0.5 * dt * np.array(k1)) k3 = reaction_equations(concentrations + 0.5 * dt * np.array(k2)) k4 = reaction_equations(concentrations + 0.5 * dt * np.array(k3)) concentrations += (dt / 6.0) * (2 * np.array(k1) + 2 * np.array(k2) + 2 * np.array(k3) + 2 * np.array(k4)) t += dt time_points.append(t) concentration_points.append(concentrations.copy()) return time_points, np.array(concentration_points)

Example usage

dt = 1e-3 # Time step size
t_max = 5 # Maximum simulation time
initial_concentrations = [1.0, 0.0, 1.0, 0.0, 0.0, 1.0] # Initial concentrations of species A, B, C, D, E, F, G
time_points, concentration_points = runge_kutta(dt, t_max, initial_concentrations)
print(concentration_points[-1])

Plotting the concentration curves

plt.plot(time_points[1:], concentration_points[1:, 0], label='R1X', color=(200/255, 0/255, 100/255))
plt.plot(time_points[1:], concentration_points[1:, 1], label='R1PdX', color=(100/255, 0/255, 200/255))
plt.plot(time_points[1:], concentration_points[1:, 2], label='R2', color=(0/255, 100/255, 200/255))
plt.plot(time_points[1:], concentration_points[1:, 3], label='R1R2', color=(200/255, 100/255, 0/255))
plt.plot(time_points[1:], concentration_points[1:, 4], label='HPdX', color=(100/255, 200/255, 0/255))
plt.plot(time_points[1:], concentration_points[1:, 5], label='Pd(0)', color=(200/255, 200/255, 0/255))

plt.xlabel('Time')
plt.ylabel('Concentration')

plt.xscale('log')

plt.yscale('log')

plt.legend()
plt.show()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

melian

2023/11/30 01:30

dt = 1e-3 # Time step size とされていますが、10進数の 1e-3(0.001) は2進数では循環小数になります。そのため、while loop で t の値が 2.0 になることはありません。   : t = 1.9999999999998905 t = 2.0009999999998906   : t の値ではなく、整数のループカウンタを使うほうがよいかと思います。
winterboum

2023/11/30 02:22

pythonはインデント命ですから、<code> を使って 載せ直してください
winterboum

2023/11/30 05:27 編集

今回の修正を元に戻し、 「中間体の生成を無視した場合」 の行の上と 「Example usage」の行の上に ```(逆コーテーション3つ)だけの行を入れて。 んでもって、プレビュー確認してね
guest

回答2

0

ベストアンサー

コメントにも書きましたが、dt(0.001) は2進数では循環小数になってしまいますので、numpy.arange で予め time_points を作成しておいて、time_points をループカウンタとして使う方がよいかと思います。
A はおそらく concentrations[0] ではないでしょうか

python

1def runge_kutta(dt, t_max, initial_concentrations): 2 # Perform the simulation using the fourth-order Runge-Kutta method 3 t = 0.0 4 concentrations = np.array(initial_concentrations) 5 time_points = np.arange(t, t_max+dt, dt) 6 concentration_points = [concentrations] 7 8 for ti in time_points[:-1]: 9 if ti == 2.0: 10 concentrations[0] += 1.0 11 else: 12 # Compute concentration changes using the fourth-order Runge-Kutta method 13 k1 = reaction_equations(concentrations) 14 k2 = reaction_equations(concentrations + 0.5 * dt * np.array(k1)) 15 k3 = reaction_equations(concentrations + 0.5 * dt * np.array(k2)) 16 k4 = reaction_equations(concentrations + 0.5 * dt * np.array(k3)) 17 18 concentrations += (dt / 6.0) * (2 * np.array(k1) + 2 * np.array(k2) + 2 * np.array(k3) + 2 * np.array(k4)) 19 20 concentration_points.append(concentrations.copy()) 21 22 return time_points, np.array(concentration_points)

投稿2023/11/30 02:23

編集2023/11/30 02:32
melian

総合スコア19825

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

DAYANVICIEDO

2023/12/01 05:02

解決いたしました。本当にありがとうございます🙇
guest

0

いくつか気になったことがあります。

python

1while t < t_max: 2 if t == 2: 3 A += 1.0 4 else: 5 # Compute concentration changes using the fourth-order Runge-Kutta method 6 k1 = reaction_equations(concentrations) 7 k2 = reaction_equations(concentrations + 0.5 * dt * np.array(k1)) 8 k3 = reaction_equations(concentrations + 0.5 * dt * np.array(k2)) 9 k4 = reaction_equations(concentrations + 0.5 * dt * np.array(k3)) 10 11 concentrations += (dt / 6.0) * (2 * np.array(k1) + 2 * np.array(k2) + 2 * np.array(k3) + 2 * np.array(k4)) 12 t += dt 13 14 time_points.append(t) 15 concentration_points.append(concentrations.copy()) 16

・Aに値を入れていますが、どこでも使っていない。
・tが2のときに、k? やconcentrations の値を更新していない。
・そもそもAを初期化していないので、このコードはエラーになると思う。

投稿2023/11/30 01:36

TakaiY

総合スコア12774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問