python中计算交集函数_物理计算中的Python函数简介

本文简要介绍了Python在物理计算中用于计算交集的函数,讲解了其在数据分析和机器学习等领域的实用价值。
摘要由CSDN通过智能技术生成

python中计算交集函数

Are you ready to move to the next level with your physics calculations in Python? If so, then you need to master the python function. I’m not an expert programmer, I’m just a normal human that likes physics and python.

您准备好使用Python进行物理计算了吗? 如果是这样,那么您需要掌握python函数。 我不是专业的程序员,我只是一个喜欢物理学和python的普通人。

I’m going to assume you have already made a numerical calculation in python. It doesn’t have to be something crazy complicated. Maybe it’s just the trajectory of a projectile with no air resistance. Actually, if you need a quick tutorial — I have this for you.

我将假设您已经在python中进行了数值计算。 不必是疯狂的复杂事情。 也许这只是没有空气阻力的弹丸的轨迹。 实际上,如果您需要快速的教程-我为您准备了这个。

OK, but what the heck is a function? It’s very similar to a math function. Something like this.

好的,但是函数到底是什么呢? 它与数学函数非常相似。 这样的事情。

For this math function, you can put in whatever value of x that you like and it “outputs” some value. A python function can be pretty much just like this. In fact, this COULD be a python function. Here’s what that would look like.

对于此数学函数,您可以输入所需的x值,然后“输出”一些值。 python函数几乎就是这样。 实际上,这可以是python函数。 这就是它的样子。

def g(x):
return(2*x**3-4*x)print(g(3))

Yes, this might be the most boring python program ever, but it’s a great place to start. Here are some important notes:

是的,这可能是有史以来最无聊的python程序,但这是一个很好的起点。 以下是一些重要说明:

  • The function needs a unique name. I can use “g” as long as there are no other functions or variables named “g”.

    该函数需要一个唯一的名称。 只要没有其他名为“ g”的函数或变量,我就可以使用“ g”。
  • In the definition (yes, you need the def part and the colon) you pick the input variable names. In this case, I’m using “x” but it could be anything as long as you use the same name in the body of the function.

    在定义中(是的,您需要def部分和冒号)选择输入变量名称。 在这种情况下,我使用的是“ x”,但是只要您在函数主体中使用相同的名称,它就可以是任何东西。

  • The output is the return() — whatever you put in the parenthesis will be the result of the function.

    输出是return() -放在括号中的任何内容都是该函数的结果。

  • This function has one input (x) and one output — but you could have as many inputs and outputs as you like.

    此功能有一个输入(x)和一个输出-但您可以根据需要选择任意数量的输入和输出。

Cool, but not super useful.

很酷,但不是超级有用。

A Projectile Motion Function

弹丸运动功能

How about another example that might be helpful? I’m going to make a function that takes the initial velocity vector for an object and the height above the ground. It then returns the “range” — the horizontal distance the object travels.

另一个可能有用的示例如何? 我将创建一个函数,该函数采用对象的初始速度矢量和地面上方的高度。 然后,它返回“范围”-物体行进的水平距离。

Although I could calculate this range with an analytical solution, I’m going to do it numerically instead. Just as a quick review, this is how to find out how far the object travels horizontally.

尽管我可以使用解析解决方案来计算此范围,但我将以数字方式进行计算。 就像快速回顾一样,这就是找出对象水平移动距离的方法。

  • Start with the initial position and velocity of the object. From the velocity and mass, I can find the object’s momentum.

    从物体的初始位置和速度开始。 从速度和质量,我可以找到物体的动量。
  • Break the motion into small time intervals. How about 0.01 seconds?

    将动作分成较小的时间间隔。 大约0.01秒?
  • Calculate the force on the object. In this case, it’s just going to be a constant gravitational force.

    计算作用在物体上的力。 在这种情况下,它将只是一个恒定的重力。
  • We can use this force and the current momentum to find the new momentum at the end of the short time interval.

    我们可以使用此力和当前动量在较短的时间间隔结束时找到新的动量。
  • Finally, with the momentum I can update the position of the object.

    最后,借助动量,我可以更新对象的位置。

Keep doing this loop until the object gets to the “ground”, which I will call y = 0 meters. Here is what this looks like as a function. (full code here)

继续执行此循环,直到物体到达“地面”为止,我将其称为y = 0米。 这是函数的外观。 ( 完整的代码在这里 )

Image for post

Now for comments:

现在征求意见:

  • Notice in line one, I name the function xrange(v0,h,m) . First, I’m pretty sure that range is already a function in one of the modules loaded in glowscript (yes, I’m using glowscript). Second, I am passing three variables into the function. Check this out, v0 is a vector and I don’t even need to declare it as that type.

    请注意,在第一行中,我将函数命名为xrange(v0,h,m) 。 首先,我非常确定range是已经在glowscript中加载的模块之一中的功能(是的,我正在使用glowscript)。 其次,我将三个变量传递给函数。 检查一下,v0是向量,我什至不需要将其声明为该类型。

  • In line 13, that’s like a whole program all by itself with the loop that does the numerical calculation.

    在第13行中,这就像一个完整的程序,它本身带有执行数值计算的循环。
  • The loop is until the position is no longer greater than zero. Since I created the position as a vector, r.y is the y-component of that position.

    循环直到位置不再大于零为止。 由于我将位置创建为矢量,因此ry是该位置的y分量。

  • Lines 14, 15, and 16 are all vector equations. Isn’t that cool? Since p is a vector, the right side is a vector operation.

    第14、15和16行都是矢量方程。 那不是很酷吗? 由于p是向量,因此右侧是向量运算。
  • Line 18 returns the final position. This function just has the scalar x-component of the position, but you could have this as just r — maybe you should try that and see what happens.

    第18行返回最终位置。 该函数仅具有位置的标量x分量,但您可以将其设为r-也许应该尝试一下,看看会发生什么。
  • Finally, in line 20 and 21 I just print the output of the function for a particular set of inputs. This is to make sure the thing is working.

    最后,在第20和21行中,我只是为一组特定的输入打印函数的输出。 这是为了确保一切正常。

Now let’s use this thing, shall we? I want to find the launch angle that produces the greatest range. Here’s the code to do that.

现在让我们使用这个东西,对吧? 我想找到产生最大射程的发射角度。 这是执行此操作的代码。

Image for post

It uses that same function (though, I did change the time interval from 0.01 to 0.001) from before. Now for some more notes:

它使用了与以前相同的功能(尽管我确实将时间间隔从0.01更改为0.001)。 现在更多注意事项:

  • Lines 20–24 are just the initial conditions. The mass doesn’t really change, but I’m going to use that in just a bit. Also, it’s easier to consider the launch velocity in terms of magnitude and angle — but I convert to a vector before calling the function.

    第20–24行只是初始条件。 质量并没有真正改变,但是我将使用它。 同样,更容易考虑幅度和角度的发射速度,但是在调用函数之前,我将其转换为向量。
  • Line 22 is the angle step. I’m going to call the function for some initial launch angle and then repeat it for a slightly higher angle.

    第22行是角度步长。 我将以某个初始启动角度调用该函数,然后以稍高的角度重复该函数。
  • Lines 26–27 are just for setting up the graph. Here is your python graph tutorial.

    第26–27行仅用于设置图形。 这是您的python图形教程。

  • Lines 29–33 are the loop to run the function with different launch angles. This includes finding the launch velocity vector (line 30) and plotting stuff (line 32).

    第29–33行是使用不同的启动角度运行功能的循环。 这包括找到发射速度矢量(第30行)和绘制填充物(第32行)。

Here is the output.

这是输出。

Image for post

Notice that the maximum range is at a launch angle of about 36 degrees — and NOT 45 degrees. Remember that the maximum range for a projectile is 45 degree only when you launch from flat ground onto flat ground. In this case, I launched the object 1 meter above the floor.

请注意,最大射程的发射角约为36度,而非45度。 请记住,仅当从平坦地面发射到平坦地面时,弹丸的最大射程为45度。 在这种情况下,我将物体发射到离地面1米的位置。

Yes, you could use some more sophisticated method to print out the maximum range, but just looking at the graph works fine too.

是的,您可以使用一些更复杂的方法来打印最大范围,但是仅查看图表也可以。

Homework

家庭作业

Now that you have a rough intro to python functions, see if you can modify the code to find the launch angle for the maximum range of a football kicked from the ground. Yes, you are going to have to include an air resistance force on the ball. Don’t worry, you can do this.

既然您已经大致了解了python函数,请查看是否可以修改代码以找到足球从地面踢出的最大射程的发射角度。 是的,您将必须在球上施加空气阻力。 不用担心,您可以这样做。

翻译自: https://medium.com/swlh/introduction-to-python-functions-in-physics-calculations-5cf2db74cabf

python中计算交集函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值