简体   繁体   English

Function 使用相同的输入返回不同的结果

[英]Function Returning Different Results with Same Inputs

I'm working on a modeling function to model the motion of a train.我正在建模 function 到 model 火车的运动。 In my script, I have a check that runs a function three times.在我的脚本中,我有一个运行 function 三次的检查。 The second and third time it returns the same values, however on the first time, it returns different values.第二次和第三次返回相同的值,但第一次返回不同的值。

    dvdtA = lambda x, v: (1 / (Tmass + mw)) * (Ap * (rg / rw)) * (((P0gauge + Patm) * V0) / (V0 + (Ap * (rg / rw) * x)) - Patm) \
        -0.5 * Cd * pa * A * (v**2) - (Cr * Tmass * g)

    dvdtD = lambda v: (1 / Tmass) * (- 0.5 * Cd * pa * A * (v**2) - (Cr * Tmass * g))
    dxdt = lambda v: v
    Ft = lambda x: (1 / (Tmass + mw)) * (Ap * (rg / rw)) * (((P0gauge + Patm) * V0) / (V0 + (Ap * (rg / rw) * x)) - Patm)
    trainMotion = np.array([dvdtA, dvdtD, dxdt, Ft])

    # Checking design constraints ######################################
    if (Ht < 0.23) and (Wt < 0.2) and (rg/rw < 1):

        # Optimization #################################################
        if RK4(trainMotion, h, tspan, v0, x0, La, slipCheck) is not None:
            if RK4(trainMotion, h, tspan, v0, x0, La, slipCheck)[1] < tFinish:
                bestParams = np.array([r0, P0gauge, rg, Ls, rp, tankMat])
                bestODE = trainMotion
                bestLa = La
                bestSlipCheck = slipCheck
                bestTime = RK4(trainMotion, h, tspan, v0, x0, La, slipCheck)[1]

This is written inside a for loop that randomly selects a set of parameters then runs them through to generate the ODES you see at the beginning.这写在一个 for 循环中,该循环随机选择一组参数,然后运行它们以生成您在开始时看到的 ODES。 The error is happening underneath the "Optimization" area.该错误发生在“优化”区域下方。 The first call on RK4 generates a different set of results than the second and third, which both generate the exact same results. RK4 上的第一次调用生成的结果集与第二次和第三次不同,它们都生成完全相同的结果。 I can't figure out why the first time is different than the second and third.我不明白为什么第一次与第二次和第三次不同。 Furthermore, when I plug those "best[ODE/LA/etc] into the RK4 function again later on in the script, it fails the checks and returns None despite those parameters having passed the checks earlier.此外,当我稍后在脚本中再次将那些“最佳 [ODE/LA/etc] 插入 RK4 function 时,它未通过检查并返回 None,尽管这些参数之前已通过检查。

For reference, here's the RK4 function itself:作为参考,这里是 RK4 function 本身:

import math as mt
import numpy as np

def RungeKutta(fun, h, tspan, x0, y0, Ls, slipCheck):
    yArr = np.zeros([len(tspan), 2])
    yArr[0,1] = y0
    yArr[0,0] = x0
    
    for i in range(len(tspan) - 1):
        
        k1 = fun[2](yArr[i, 1])
        k2 = fun[2](yArr[i, 1] + k1 * (h/2))
        k3 = fun[2](yArr[i, 1] + k2 * (h/2))
        k4 = fun[2](yArr[i, 1] + k3 * h)

        yArr[i+1, 0] = yArr[i, 0] + h * (k1/6 + k2/3 + k3/3 + k4/6)
        
        if (fun[3](yArr[i+1, 0]) > slipCheck) or (yArr[i+1, 0] > 12.5):
            return None

        if yArr[i, 0] <= Ls:
            k1 = fun[0](yArr[i, 0], yArr[i, 1])
            k2 = fun[0](yArr[i, 0] + h/2, yArr[i, 1] + k1 * (h/2))
            k3 = fun[0](yArr[i, 0] + h/2, yArr[i, 1] + k2 * (h/2))
            k4 = fun[0](yArr[i, 0] + h, yArr[i, 1] + k3 * h)

            yArr[i+1, 1] = yArr[i, 1] + h * (k1/6 + k2/3 + k3/3 + k4/6)

        elif yArr[i, 0] > Ls:
            k1 = fun[1](yArr[i, 1])
            k2 = fun[1](yArr[i, 1] + k1 * (h/2))
            k3 = fun[1](yArr[i, 1] + k2 * (h/2))
            k4 = fun[1](yArr[i, 1] + k3 * h)
                   
            yArr[i+1, 1] = yArr[i, 1] + h * (k1/6 + k2/3 + k3/3 + k4/6)

        if i >= 3:
            if yArr[i+1, 1] < 0 and yArr[i+1, 0] > 10:
                tFinish = tspan[i]
                data = np.column_stack((yArr, tspan))
                return data, tFinish       

    return None

Let RK4 always return 2 values, unpack the values when calling RK4让RK4总是返回2个值,调用RK4时解包

    dvdtA = lambda x, v: (1 / (Tmass + mw)) * (Ap * (rg / rw)) * (((P0gauge + Patm) * V0) / (V0 + (Ap * (rg / rw) * x)) - Patm) \
        -0.5 * Cd * pa * A * (v**2) - (Cr * Tmass * g)

    dvdtD = lambda v: (1 / Tmass) * (- 0.5 * Cd * pa * A * (v**2) - (Cr * Tmass * g))
    dxdt = lambda v: v
    Ft = lambda x: (1 / (Tmass + mw)) * (Ap * (rg / rw)) * (((P0gauge + Patm) * V0) / (V0 + (Ap * (rg / rw) * x)) - Patm)
    trainMotion = np.array([dvdtA, dvdtD, dxdt, Ft])

    # Checking design constraints ######################################
    if (Ht < 0.23) and (Wt < 0.2) and (rg/rw < 1):

        # Optimization #################################################
        data, tFinishResult = RK4(trainMotion, h, tspan, v0, x0, La, slipCheck)
        if data is not None:
            if tFinishResult < tFinish:
                bestParams = np.array([r0, P0gauge, rg, Ls, rp, tankMat])
                bestODE = trainMotion
                bestLa = La
                bestSlipCheck = slipCheck
                bestTime = tFinishResult

The RK4 function: RK4 function:

import math as mt
import numpy as np

def RungeKutta(fun, h, tspan, x0, y0, Ls, slipCheck):
    yArr = np.zeros([len(tspan), 2])
    yArr[0,1] = y0
    yArr[0,0] = x0
    
    for i in range(len(tspan) - 1):
        
        k1 = fun[2](yArr[i, 1])
        k2 = fun[2](yArr[i, 1] + k1 * (h/2))
        k3 = fun[2](yArr[i, 1] + k2 * (h/2))
        k4 = fun[2](yArr[i, 1] + k3 * h)

        yArr[i+1, 0] = yArr[i, 0] + h * (k1/6 + k2/3 + k3/3 + k4/6)
        
        if (fun[3](yArr[i+1, 0]) > slipCheck) or (yArr[i+1, 0] > 12.5):
            return None,None

        if yArr[i, 0] <= Ls:
            k1 = fun[0](yArr[i, 0], yArr[i, 1])
            k2 = fun[0](yArr[i, 0] + h/2, yArr[i, 1] + k1 * (h/2))
            k3 = fun[0](yArr[i, 0] + h/2, yArr[i, 1] + k2 * (h/2))
            k4 = fun[0](yArr[i, 0] + h, yArr[i, 1] + k3 * h)

            yArr[i+1, 1] = yArr[i, 1] + h * (k1/6 + k2/3 + k3/3 + k4/6)

        elif yArr[i, 0] > Ls:
            k1 = fun[1](yArr[i, 1])
            k2 = fun[1](yArr[i, 1] + k1 * (h/2))
            k3 = fun[1](yArr[i, 1] + k2 * (h/2))
            k4 = fun[1](yArr[i, 1] + k3 * h)
                   
            yArr[i+1, 1] = yArr[i, 1] + h * (k1/6 + k2/3 + k3/3 + k4/6)

        if i >= 3:
            if yArr[i+1, 1] < 0 and yArr[i+1, 0] > 10:
                data = np.column_stack((yArr, tspan))
                tFinish = tspan[i]
                return data, tFinish       

    return None, None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 函数为不同的输入返回相同的值 - Function returning same values for supposedly different inputs 尽管 Python 中的输入相同,但 Function 返回不同的结果 - Function returning different result despite the same inputs in Python Pytorch BCELoss function 不同输出对应相同输入 - Pytorch BCELoss function different outputs for same inputs Python函数根据先前的输入返回不同的值 - Python function returning different values depending on prior inputs 函数为不同的参数返回相同的值 - Function returning same value for different arguments 如果在本地和云上运行,`requests` 会为同一脚本返回不同的结果 - `requests` returning different results for the same script if run both locally and on the cloud splprep 和 splrep 对 scipy 中的相同数据返回不同的结果 - splprep and splrep returning different results for the same data in scipy TensorFlow 相同的 Model 在 Colab 和本地返回不同的结果 - TensorFlow Same Model returning different results in Colab and local 为什么多项式回归对不同等级返回相同的结果? - Why is the polynomial regression returning the same results for different grades? PyTorch:nn.LSTM 对同一批中的相同输入输出不同的结果 - PyTorch: nn.LSTM output different results for the same inputs in the same batch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM