beta = 1.0 # Boundary value at y = L # Guessed values s = [1.0, 1.5] z0 = np.zeros(2) dpdx_list = [-5.0, -2.5, -1.0, 0.0, 1.0, 2.5, 5.0] legends = [] for dpdx in dpdx_list: phi = [] for svalue in s: z0[1] = svalue z = rk4(f, z0, y) phi.append(z[-1, 0] - beta) # Compute correct initial guess s_star = (s[0] * phi[1] - s[1] * phi[0]) / (phi[1] - phi[0]) z0[1] = s_star # Solve the initial value problem which is a solution to the boundary value problem z = rk4(f, z0, y) plot(z[:, 0], y, '-.') legends.append('rk4: dp=' + str(dpdx)) # Plot the analytical solution plot(u_a(y, dpdx), y, ':') legends.append('exa: dp=' + str(dpdx))
""" def func(Theta, t): theta = Theta[0] dtheta = Theta[1] if dtheta >=0: out = np.array([dtheta, math.cos(theta) - mu*math.sin(theta)]) else: out = np.array([dtheta, math.cos(theta) + mu*math.sin(theta)]) return out mu = 0.4 Tau = np.linspace(0, 10, 101) Theta_0 = np.array([0, 0]) THETA = rk4(func, Theta_0, Tau) animatePendulum(THETA[:,0], Tau, l=1)
from ODEschemes import euler, heun, rk4 import numpy as np N=20 L = 1.0 y = np.linspace(0,L,N+1) def f(z, t, dpdx=0.0): """RHS for Couette-Posieulle flow""" zout = np.zeros_like(z) zout[:] = [z[1], -dpdx] return zout s0=1.0 z0=np.zeros(2) z0[1] = s0 z = rk4(f, z0, y) phi0 =z[]
figure(figsize=(20, 8)) # hold('on') LNWDT = 2 FNT = 18 rcParams['lines.linewidth'] = LNWDT rcParams['font.size'] = FNT # computing and plotting # smooth ball with drag for i in range(0, N2): z0 = np.zeros(4) z0[2] = v0 * np.cos(angle[i]) z0[3] = v0 * np.sin(angle[i]) z = rk4(f, z0, time) plot(z[:, 0], z[:, 1], ':', color=line_color[i]) legends.append('angle=' + str(alfa[i]) + ', smooth ball') # golf ball with drag for i in range(0, N2): z0 = np.zeros(4) z0[2] = v0 * np.cos(angle[i]) z0[3] = v0 * np.sin(angle[i]) z = rk4(f2, z0, time) plot(z[:, 0], z[:, 1], '-.', color=line_color[i]) legends.append('angle=' + str(alfa[i]) + ', golf ball') # golf ball with drag and lift for i in range(0, N2):
# main program starts here T = 10 # end of simulation N = 20 # no of time steps time = np.linspace(0, T, N+1) z0=np.zeros(2) z0[0] = 2.0 ze = euler(f, z0, time) # compute response with constant CD using Euler's method ze2 = euler(f2, z0, time) # compute response with varying CD using Euler's method zh = heun(f, z0, time) # compute response with constant CD using Heun's method zh2 = heun(f2, z0, time) # compute response with varying CD using Heun's method zrk4 = rk4(f, z0, time) # compute response with constant CD using RK4 zrk4_2 = rk4(f2, z0, time) # compute response with varying CD using RK4 k1 = np.sqrt(g*4*rho_s*d/(3*rho_f*CD)) k2 = np.sqrt(3*rho_f*g*CD/(4*rho_s*d)) v_a = k1*np.tanh(k2*time) # compute response with constant CD using analytical solution # plotting legends=[] line_type=['-',':','.','-.',':','.','-.'] plot(time, v_a, line_type[0]) legends.append('Analytical (constant CD)') plot(time, ze[:,1], line_type[1])
a semicircle: theta'' = - mu*(theta')^2 +g/r(cos(theta) - mu*sin(theta)) """ def func(Theta, t): theta = Theta[0] dtheta = Theta[1] if dtheta >= 0: out = np.array([dtheta, math.cos(theta) - mu * math.sin(theta)]) else: out = np.array([dtheta, math.cos(theta) + mu * math.sin(theta)]) return out mu = 0.4 Tau = np.linspace(0, 10, 101) Theta_0 = np.array([0, 0]) THETA = rk4(func, Theta_0, Tau) animatePendulum(THETA[:, 0], Tau, l=1)