変分法って何にでも使えるな (44レス)
上下前次1-新
抽出解除 レス栞
リロード規制です。10分ほどで解除するので、他のブラウザへ避難してください。
26(2): 2024/07/22(月)21:06 ID:DWpjDTWd(3/3) AAS
関数空間 C2[0,1] 上の汎関数
$$
J[y] = \int_0^1 \left(y''(x)^2 + y(x)^2 - 2xy(x)\right) dx
$$
の極値問題を解け。境界条件は y(0)=y(1)=0 とする。
30(1): 2024/08/03(土)17:35 ID:??? AAS
>>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()
44: 08/07(木)13:16 ID:feqc8T9c(1) AAS
>>26
なんかお題がガチ過ぎて草。ま、こういうのはオイラー・ラグランジュ方程式で解くんだろ?って思ってググったら、やっぱりそうだった。普通のやつじゃなくて、高階導関数が入ってるやつね。
上下前次1-新書関写板覧索設栞歴
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ
ぬこの手 ぬこTOP 0.013s