How do I get the best values of certain parameters that will give me the highest velocity

34 Views Asked by At

I have 2 ODE equations, which I solved using the RK-4 method, and I am trying to find the best values for r1, r2, and r3, which will return the best value for v3. I have been trying to maximise the velocity for a while now, and I keep getting errors. This was my last code attempt.

import numpy as np
import matplotlib.pyplot as plt


y_n = []

def calculate_v3(r3, r, r2, phi_dot, theta_dot, phi, theta):
        v3 = np.sqrt(r3**2 * theta2_dot**2 + (r - r2)**2 * theta1_dot**2 + 2 * r3 * (r - r2) * theta1_dot * theta2_dot * np.cos(theta_2 - theta_1))

    return v3

def RHS(t, y, r1, r2, r3, r):
    # ... (Same RHS function as before)
    
    m2 = p * A * r
    m1 = 5000
    m3 = 100
    theta_1, theta_2, w1, w2 = y

    f0 = w1
    f1 = w2

    r = 1  # Define 'r' appropriately

    f2 = (
        (g * np.sin(theta_1) * ((m1 * r1) - (m2 * r) - (m3 * r2))) +
        (m3 * r2) * (g * np.sin(theta_2) * np.cos(theta_1 - theta_2) - (r3 * w2**2 * np.sin(theta_1 - theta_2)) - (r2 * w1**2 * np.cos(theta_1 - theta_2) * np.sin(theta_1 - theta_2)))
    ) / (
        (m1 * r1**2) + (m2 * (((r/2) - r2)**2 + (r**2/12))) + (m3 * r2**2 * np.sin(theta_1 - theta_2)**2)
    )

    f3 = ((r3/r2) * ((w1**2 * np.sin(theta_1 - theta_2)) - (f2 * np.cos(theta_1 - theta_2)))) - (g * np.sin(theta_2))

    return np.array([f0, f1, f2, f3])


def RK4(t, y, h, f, r1, r2, r3, r):
    #Runge Kutta standard calculations
    k1 = f(t, y, r1, r2, r3, r)
    k2 = f(t + h/2, y + h/2 * k1, r1, r2, r3, r)
    k3 = f(t + h/2, y + h/2 * k2, r1, r2, r3, r)
    k4 = f(t + h, y + h * k3, r1, r2, r3, r)

    return 1/6*(k1 + 2 * k2 + 2 * k3 + k4)


def main():
    global y_n
    # Specify time interval and number of domain subintervals
    t0 = 0 
    T = 0.71
    n = 100
    # Initial conditions
    y0 = [np.pi/4, 0, 0, 0]
    
    t_n = [t0]
    y_n = [np.array(y0)]  
    
    #Step size
    h  = 0.01   
    
    while t_n[-1] < T:
        #Keep going until T is reached.
        #Store numerical solutions into y_n
        y_n.append(y_n[-1] + h * RK4(t_n[-1], y_n[-1], h, RHS))
        t_n.append(t_n[-1] + h)
        
        
    
    
    print(y_n)
    
    # Define bounds for r1, r2, r3, and r
    r1_min = 0.5
    r1_max = 1
    r2_min = 3*r1_min
    r2_max = 3*r1_max
    r3_min = 0.3
    r3_max = r2_max
    r_min = r1_min + r2_min
    r_max = r1_max + r2_max

    # Initialize variables to store the maximum velocity and corresponding parameters
    max_velocity = 0.0
    best_parameters = None

    # Iterate through parameter combinations within the specified bounds
    for r1 in np.linspace(r1_min, r1_max, num=100):
        for r2 in np.linspace(r2_min, r2_max, num=100):
            for r3 in np.linspace(r3_min, r3_max, num=100):
                for r in np.linspace(r_min, r_max, num=100):
                # ... (Rest of your code remains the same)
                # Calculate velocity for the current parameter combination
                    v3 = calculate_v3(r3, r, r2, y_n[-1][3], y_n[-1][1], varphi_dot, y_n[-1][2], y_n[-1][0])

                # Check if this velocity is greater than the current maximum
                    if v3 > max_velocity:
                        max_velocity = v3
                        best_parameters = (r1, r2, r3, r)
                   

    # Print the maximum velocity and the corresponding parameters
    print("Maximum Velocity:", max_velocity)
    print("Best Parameters (r1, r2, r3, r):", best_parameters)

main()


This is the error that I got after running it:

TypeError                                 Traceback (most recent call last)
<ipython-input-8-ba5ebab27a6e> in <module>
    103     print("Best Parameters (r1, r2, r3, r):", best_parameters)
    104 
--> 105 main()

<ipython-input-8-ba5ebab27a6e> in main()
     62         #Keep going until T is reached.
     63         #Store numerical solutions into y_n
---> 64         y_n.append(y_n[-1] + h * RK4(t_n[-1], y_n[-1], h, RHS))
     65         t_n.append(t_n[-1] + h)
     66 

TypeError: RK4() missing 4 required positional arguments: 'r1', 'r2', 'r3', and 'r'

Any help would be great.

0

There are 0 best solutions below