変分法って何にでも使えるなat SCI変分法って何にでも使えるな - 暇つぶし2ch■コピペモード□スレを通常表示□オプションモード□このスレッドのURL■項目テキスト26:ご冗談でしょう?名無しさん 24/07/22 21:06:15.39 DWpjDTWd.net 関数空間 C2[0,1] 上の汎関数 $$ J[y] = \int_0^1 \left(y''(x)^2 + y(x)^2 - 2xy(x)\right) dx $$ の極値問題を解け。境界条件は y(0)=y(1)=0 とする。 27:ご冗談でしょう?名無しさん 24/08/03 16:38:19.10 .net ライトフライヤーが強いね フラフラ運転→ブレーキ痕はなかったの 28:ご冗談でしょう?名無しさん 24/08/03 16:39:45.54 wNtHquFT.net 今思えば すみません 日和ってます 29:ご冗談でしょう?名無しさん 24/08/03 17:29:02.14 3a7tlBPK.net 何がいいかというとそこまで急ぐ理由はなんだろうな 視聴率取りたいからなんじゃないのに 30:ご冗談でしょう?名無しさん 24/08/03 17:35:52.72 .net >>26 import numpy as np from scipy.optimize import minimize def J(y, bc_type=None): """ Functional to be minimized. Args: y: Array representing the function y(x). bc_type: Optional string specifying the boundary condition type. - 'dirichlet': Enforces y(0) = y(1) = 0 (default). - None: No boundary condition enforced. Returns: The value of the functional J evaluated at y. """ y_prime = np.gradient(y) y_doubleprime = np.gradient(y_prime) return np.trapz(y_doubleprime**2 + y**2 - 2*y*y_prime, np.linspace(0, 1, len(y))) def bc_dirichlet(y): """ Constraint function for Dirichlet boundary conditions. Args: y: Array representing the function y(x). Returns: A list containing the constraint values (y(0) and y(1)). """ return [y[0], y[-1]] # Define the problem with Dirichlet boundary conditions bounds = ((0, None),) * len(np.linspace(0, 1, 101)) # No lower bound for y cons = ({'type': 'eq', 'fun': bc_dirichlet}) # Solve the minimization problem initial_guess = np.random.rand(101) # Random initial guess sol = minimize(J, initial_guess, method='SLSQP', bounds=bounds, constraints=cons) # Extract the solution y_opt = sol.x # Print the minimum value of the functional print("Minimum value of J:", J(y_opt)) # Plot the solution (optional) import matplotlib.pyplot as plt x = np.linspace(0, 1, len(y_opt)) plt.plot(x, y_opt) plt.xlabel("x") plt.ylabel("y(x)") plt.title("Solution of the minimization problem") plt.show() 次ページ最新レス表示レスジャンプ類似スレ一覧スレッドの検索話題のニュースおまかせリストオプションしおりを挟むスレッドに書込スレッドの一覧暇つぶし2ch