第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

我不知道為什么python會拋出這個(gè)錯(cuò)誤

我不知道為什么python會拋出這個(gè)錯(cuò)誤

牛魔王的故事 2022-06-22 17:02:24
我正在嘗試創(chuàng)建一個(gè)程序,該程序繪制自由落體中球的速度與暴露于阻力的球的速度,使得 F_drag = -Cv^2 其中 C 是常數(shù) (m*g)/100。我的輸入是 5 代表 m,5 代表 tf,0.1 代表 dt。import numpy as npimport matplotlib.pyplot as plt%matplotlib inlinem = float(input("Input the mass of the ball in kilograms: "))tf = float(input("Input a specified time of fall in seconds: "))dt = float(input("Input the time step for each calculation in seconds: "))imaxFloat = tf/dt   # The total number of time steps as a floating point number with remainderimax = int(round(imaxFloat))   # Converts the float to the nearest integer v0 = 0       # Velocity at t = 0g = 9.8       # Accleration due to gravityi = 0       # Initial counter valuei2 = 0     # Initial counter value 2c = (m*g)/100     # Constant in drag equationt1 = np.array([0])v1 = np.array([v0])t2 = np.array([0])v2 = np.array([v0])drag_a = ((m*g)-(c*v1*v1))/m     # Acceleration of ball with dragwhile i < imax:    t1 = np.append(t1, t1[i] + dt)    v1 = np.append(v1, v1[i] - g * dt )    i = i + 1while i2 < imax:    t2 = np.append(t2, t2[i] + dt)    v2 = np.append(v2, v2[i] - drag_a * dt)    i2 = i2 + 1plt.plot(t1, v1, label = "Neglecting air resistance")plt.plot(t2, v2, label = "With air resistance")Python 拋出此錯(cuò)誤:---------------------------------------------------------------------------IndexError                                Traceback (most recent call last)<ipython-input-6-10c7e3224e87> in <module>     30      31 while i2 < imax:---> 32     t2 = np.append(t2, t2[i] + dt)     33     v2 = np.append(v2, v2[i] - drag_a * dt)     34     i2 = i2 + 1IndexError: index 50 is out of bounds for axis 0 with size 1我一般需要幫助解決這個(gè)問題,但也需要找到解決這個(gè)錯(cuò)誤的方法。謝謝!
查看完整描述

2 回答

?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

你的 while 循環(huán)真的不應(yīng)該使用np.append. 它很慢。


In [119]: t1 = np.array([0]) 

     ...: i=0 

     ...: while i < 10: 

     ...:     t1 = np.append(t1, t1[i] + .1) 

     ...:     i += 1 

     ...:                                                                       

In [120]: t1                                                                    

Out[120]: array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])

列表追加更快:


In [121]: t1 = [0] 

     ...: i=0 

     ...: for i in range(10): 

     ...:     t1.append(t1[-1] + .1)     # t1[-1] the last, latest, value

     ...:      

     ...:                                                                       

In [122]: t1                                                                    

Out[122]: 

[0,

 0.1,

 0.2,

 0.30000000000000004,

 0.4,

 0.5,

 0.6,

 0.7,

 0.7999999999999999,

 0.8999999999999999,

 0.9999999999999999]

In [123]: np.array(_)                                                           

Out[123]: array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])

更好的是使用arange:


In [124]: np.arange(11)*0.1                                                     

Out[124]: array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])


In [127]: np.linspace(0,1,11)                                                   

Out[127]: array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])


查看完整回答
反對 回復(fù) 2022-06-22
?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

在第二個(gè) while 循環(huán)中,您應(yīng)該將其i2用作索引變量。由于在循環(huán)的第一次迭代中,t2[i]是真的t2[50],但當(dāng)時(shí)t2只有一個(gè)元素,所以索引超出了范圍。


import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline


m = float(input("Input the mass of the ball in kilograms: "))

tf = float(input("Input a specified time of fall in seconds: "))

dt = float(input("Input the time step for each calculation in seconds: "))


imaxFloat = tf/dt   # The total number of time steps as a floating point number with remainder

imax = int(round(imaxFloat))   # Converts the float to the nearest integer 


v0 = 0       # Velocity at t = 0

g = 9.8       # Accleration due to gravity

i = 0       # Initial counter value

i2 = 0     # Initial counter value 2

c = (m*g)/100     # Constant in drag equation


t1 = np.array([0])

v1 = np.array([v0])

t2 = np.array([0])

v2 = np.array([v0])


drag_a = ((m*g)-(c*v1*v1))/m     # Acceleration of ball with drag


while i < imax:

    t1 = np.append(t1, t1[i] + dt)

    v1 = np.append(v1, v1[i] - g * dt )

    i = i + 1


while i2 < imax:

    t2 = np.append(t2, t2[i2] + dt)

    v2 = np.append(v2, v2[i2] - drag_a * dt)

    i2 = i2 + 1


plt.plot(t1, v1, label = "Neglecting air resistance")

plt.plot(t2, v2, label = "With air resistance")


查看完整回答
反對 回復(fù) 2022-06-22
  • 2 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號