def beam_ode(x, y): # y = [y, dy/dx, d2y/dx2, d3y/dx3] w = 10.0 EI = 20000.0 dydx = y[1] d2ydx2 = y[2] d3ydx3 = y[3] d4ydx4 = w / EI return [dydx, d2ydx2, d3ydx3, d4ydx4] def shooting_method(): L = 5.0 # Initial conditions at x=0: y=0, d2y/dx2=0 # Guess dy/dx(0) and d3y/dx3(0) from scipy.integrate import solve_ivp # Use secant method to satisfy y(L)=0 and y''(L)=0 # Simplified: for this problem, analytical solution exists. # Numerical approach: def residual(guess): # guess = [dy/dx(0), d3y/dx3(0)] sol = solve_ivp(beam_ode, (0, L), [0, guess[0], 0, guess[1]], t_eval=[L]) return [sol.y[0, -1], sol.y[2, -1]] # y(L) and y''(L)
slope, intercept = lin_regress(strain, stress) print(f"Linear (Young's modulus): slope:.1f MPa")
# Using linearity: find correct guess via linear combination # Two trial guesses sol1 = solve_ivp(beam_ode, (0, L), [0, 0, 0, 1], t_eval=[L]) sol2 = solve_ivp(beam_ode, (0, L), [0, 1, 0, 0], t_eval=[L]) Numerical Methods In Engineering With Python 3 Solutions
We solve by converting to 1st-order system.
def d_deflection(x): return 3 x**2 - 12 x + 11 def beam_ode(x, y): # y = [y, dy/dx,
t_euler, T_euler = euler(cooling, 100, 0, 60, 2) t_rk4, T_rk4 = rk4(cooling, 100, 0, 60, 2)
[ EI \fracd^4ydx^4 = w ]
# Back substitution x = np.zeros(n) for i in range(n-1, -1, -1): x[i] = (b[i] - np.dot(A[i, i+1:], x[i+1:])) / A[i, i] return x A = np.array([[2, -1, 0], [-1, 2, -1], [0, -1, 1]], dtype=float) b = np.array([1, 0, 0]) solution = gauss_elim(A.copy(), b.copy()) print("Forces in truss members:", solution) 3. Curve Fitting & Interpolation Least Squares Linear & Polynomial Regression from numpy.polynomial import Polynomial def lin_regress(x, y): n = len(x) sum_x = np.sum(x) sum_y = np.sum(y) sum_xy = np.sum(x * y) sum_x2 = np.sum(x**2)
print(f"Temp after 60s (Euler): T_euler[-1]:.2f°C") print(f"Temp after 60s (RK4): T_rk4[-1]:.2f°C") Problem: Simply supported beam, uniformly distributed load ( w = 10 , \textkN/m ), length ( L = 5 , \textm ), ( EI = 20000 , \textkN·m^2 ). Find maximum deflection using numerical integration of the ODE: Curve Fitting & Interpolation Least Squares Linear &
p = poly_fit(strain, stress, 2) print(f"Quadratic fit: p") Central Difference & Simpson’s Rule def central_diff(f, x, h=1e-5): return (f(x + h) - f(x - h)) / (2 * h) def simpsons_rule(f, a, b, n): """n must be even""" if n % 2 != 0: raise ValueError("n must be even") h = (b - a) / n x = np.linspace(a, b, n+1) fx = f(x) integral = fx[0] + fx[-1] integral += 4 * np.sum(fx[1:-1:2]) integral += 2 * np.sum(fx[2:-2:2]) return integral * h / 3 Example: velocity from acceleration def acceleration(t): return 9.81 * np.sin(np.radians(30)) # inclined plane Derivative of position def position(t): return 0.5 * 9.81 * np.sin(np.radians(30)) * t**2
I’ll develop a structured guide for (based on the popular textbook by Jaan Kiusalaas), including concept summaries + Python solutions for key engineering numerical methods.