1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
import math import numpy as np import matplotlib.pyplot as plt import requests import json from bs4 import BeautifulSoup from scipy.integrate import odeint
v_0 = float(input("请输入初速度大小: ")) k = float(input("请输入空气阻力系数: "))
url = 'http://www.cip.cc/' header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0'} html = requests.get(url, headers=header) bsObj = BeautifulSoup(html.content, 'lxml') msg = bsObj.find('div', class_="data kq-well") msg = [i for i in msg] city = msg[1]
GaoDeKey = '79e3576b80e0b544d0d17dd643e4654e' url = 'https://restapi.amap.com/v3/geocode/geo' params = {'key': GaoDeKey, 'address': city} res = requests.get(url, params) jd = json.loads(res.text) coords = jd['geocodes'][0]['location'] jd_value = coords.split(',') latitude = float(jd_value[1]) latitude = latitude/180*math.pi
g = 9.7803*(1+0.005324*((math.sin(latitude))**2)-0.000005*((math.sin(2*latitude))**2))
def simulate(v_1, theta, y_height): theta = math.radians(theta) v_x0 = v_1 * math.cos(theta) v_y0 = v_1 * math.sin(theta)
def motion1(w, t, k): v_x, v_y = w return[-k * v_x ** 2, -g - k * v_y ** 2] t = np.linspace(0, 20, 300000) track1 = odeint(motion1, (v_x0, v_y0), t, args=(k, )) v_x1 = [i for i in track1[:, 0]] v_y1 = [j for j in track1[:, 1] if j > 0] v_x1 = v_x1[:len(v_y1)] delta_t = (20 - 0) / 300000 i = 1 x = 0 y = y_height y_list1 = [y] x_list1 = [0] while i < len(v_y1) - 1: x += ((v_x1[i] + v_x1[i-1]) / 2) * delta_t y += ((v_y1[i] + v_y1[i-1]) / 2) * delta_t if y < 0: break x_list1.append(x) y_list1.append(y) i += 1
def motion2(w, t, k): v_x, v_y = w return[-k * v_x ** 2, -g + k * v_y ** 2]
track2 = odeint(motion2, (v_x1[-1], 0), t, args=(k, )) v_x2 = [i for i in track2[:, 0]] v_y2 = [j for j in track2[:, 1] if j < 0] j = 1 x = x_list1[-1] y = y_list1[-1] y_list2 = [y] x_list2 = [x] while j < len(v_y2) - 1: x += ((v_x2[j] + v_x2[j-1]) / 2) * delta_t y += ((v_y2[j] + v_y2[j-1]) / 2) * delta_t if y < 0: break x_list2.append(x) y_list2.append(y) j += 1 x_list = x_list1 + x_list2 y_list = y_list1 + y_list2 return x_list, y_list
def x_MaxTheta(v, height): x_max = [] for angle in range(1, 91, 1): x_list, y_list = simulate(v_1=v, theta=angle, y_height=height) x_max.append(x_list[-1]) return x_max
heght = float(input('输入初始高度:')) x_max = x_MaxTheta(v=v_0, height=heght) plt.plot(range(1, 91, 1), x_max, 'g-') plt.show() x = 0 j = 1 for i in x_max: j += 1 if i > x: x = i num = j - 1 print('当前初始条件下抛得最远的角度是{},最远距离是{}'.format(num, x)) print('输入要模拟的轨迹的抛出角度:') a = float(input()) x, y = simulate(v_1=v_0, theta=a, y_height=heght) plt.plot(x, y, 'r-') plt.show()
|