在Python中使用sec逆函数 [英] Using the inverse sec function in Python
问题描述
我正在创建一个程序,该程序从一定范围的高度和设定的初始速度计算出发射弹丸的最佳角度.在我需要使用的最后一个方程中,存在一个反秒函数,这会引起一些问题.
I am creating a program that calculates the optimum angles to fire a projectile from a range of heights and a set initial velocity. Within the final equation I need to utilise, there is an inverse sec function present that is causing some troubles.
我已经导入了数学并尝试使用asec(无论如何),但是数学似乎无法计算sec的反函数?我也了解sec(x)= 1/cos(x),但是当我将1/cos(x)代入方程式并代数求解x时,它变成了一个非真实的结果:/.
I have imported math and attempted to use asec(whatever) however it seems math can not calculate inverse sec functions? I also understand that sec(x) = 1/cos(x) but when I sub 1/cos(x) into the equation instead and algebraically solve for x it becomes a non real result :/.
我的代码如下:
print("This program calculates the optimum angles to launch a projectile from a given range of heights and a initial speed.")
x = input("Input file name containing list of heights (m): ")
f = open(x, "r")
for line in f:
heights = line
print("the heights you have selected are : ", heights)
f.close()
speed = float(input("Input your initial speed (m/s): "))
print("The initial speed you have selected is : ", speed)
ran0 = speed*speed/9.8
print(ran0)
f = open(x, "r")
for line in f:
heights = (line)
import math
angle = (math.asec(1+(ran0/float(heights))))/2
print(angle)
f.close()
所以我的主要问题是,有没有办法在不安装和导入其他东西的情况下找到python中任何东西的逆秒?我意识到这可能更多是基于数学的问题,而不是编码问题,但是可以提供任何帮助:).
So my main question is, is there any way to find the inverse sec of anything in python without installing and importing something else? I realise this may be more of a math based problem than a coding problem however any help is appreciated :).
推荐答案
假设我们正在寻找反正割角为θ的实数 x .然后我们有:
Let's say we're looking for real number x whose arcsecant is angle θ. Then we have:
θ = arcsec(x)
sec(θ) = x
1 / cos(θ) = x
cos(θ) = 1 / x
θ = arccos(1/x)
因此,根据这种推理,您可以将arcsecant函数编写为:
So with this reasoning, you can write your arcsecant function as:
from math import acos
def asec(x):
return acos(1/x)
这篇关于在Python中使用sec逆函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!