Incorrect use of np.where

前端 未结 1 1749
情深已故
情深已故 2021-01-29 07:31

So I am trying to calculate an error by using two step sizes, one twice the size of the other. I set up two temperature arrays which are being calculated using two different ste

1条回答
  •  鱼传尺愫
    2021-01-29 08:15

    First, looping over the same temperatures which are spaced by dt but then doubling dt makes no sense. It makes more sense to compare to the analytical solution as attempted in your previous post.

    To print the value of the result at a certain temperature you may - under the condition that you know the value is actually inside the array - index the array by the condition.

    E.g.

    t = np.array([1,55,77])
    x = np.array([1,2,3])
    print x[t == 55]
    

    In this case:

    import numpy as np 
    
    r = 0.024 #cooling rate per minute
    te_init = 90.0
    te_surr = 17
    
    def f(te):
        return -r*(te - te_surr) # the derivative
    
    dt_values = [0.05, 0.025, 0.01, 0.005]
    
    for dt in dt_values:
        t = np.arange(0,100,dt) #time
        te = np.zeros(len(t)) #temperature array
        te[0] = te_init 
    
        for i in range(1,len(t)): #eulers method for computing temperature 
            p1=f(te[i-1])
            p2 =f(te[i-1]+dt*p1)
            te[i] = te[i-1] + (p1 + p2)*(dt/2) 
            te_ana = te_surr - (te_surr - te_init)*np.exp(-r*t)
    
        print dt, 
        print te[t==10] - te_ana[t==10]
    

    0 讨论(0)
提交回复
热议问题