如何计算二维弹丸运动中的空气阻力 - C#中的计算科学 [英] How to calculate force of air resistance in 2D projectile motion - Computational Science in C#
问题描述
我正在编写一个程序,该程序将在以特定初始速度以特定角度投球的 100 秒间隔内输出各种统计数据.我在计算空气阻力时遇到问题.我在下面有我的代码.级别 1 已解决,女巫正在对没有空气阻力的 2D 弹丸运动进行建模.我被困在 2 级女巫是同样的事情,但我需要考虑空气阻力.
I am writing a program that will output a table of various stats on a 100th of a second interval of throwing a ball at a specific angle with a specific initial speed. I am having trouble calculating the air resistance. I have my Code below. Level 1 is solved, witch is modeling the 2D projectile motion without air resistance. I am stuck on level 2 witch is the same thing, but I need to factor in air resistance.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework2ProjectileMotion
{
class Program
{
private const double interval = 0.001;
private const int mass = 5;
private const double initialSpeed = 10;
private const double angle = Math.PI / 4;
private const double startTime = 0;
private void CalcAll(StreamWriter sw, double drag)
{
double speed = initialSpeed;
double xPos = 0;
double pXPos = 0;
double yPos = 0;
double pYPos = 0;
double zPos = 0;
double pZPos = 0;
double distance = 0;
double xVel = 10 * Math.Cos(angle);
double yVel = 0;
double zVel = 10 * Math.Sin(angle);
double xAcc = 0;
double yAcc = 0;
double zAcc = -9.8;
double mAcc = 9.8;
double xAir = 0;
double yAir = 0;
double zAir = 0;
double air = 0;
double slope = 0;
double vector = 0;
sw.WriteLine("Time\tx\ty\tz\tDistance\tvx\tvy\tvz\tSpeed\tax\tay\taz\tm_Acc\n" + startTime + "\t" + xPos + "\t" + yPos + "\t" + zPos + "\t" + distance + "\t" + xVel + "\t" + yVel + "\t" + zVel + "\t" + speed + "\t" + xAcc + "\t" + yAcc + "\t" + zAcc + "\t" + mAcc);
for (double i=startTime+interval;zPos>=0;i+=interval)
{
air = drag * speed * speed;
if (xPos == 0)
{
slope = 1;
}
else
{
slope = (zPos - pZPos) / (xPos - pXPos);
}
vector = Math.Atan(slope);
xAir = air * Math.Cos(vector);
zAir = air * Math.Sin(vector);
xAcc = ((xAcc * mass) - xAir) / mass;
yAcc = ((yAcc * mass) - yAir) / mass;
zAcc = ((zAcc * mass) - zAir) / mass;
mAcc = Math.Sqrt(xAcc * xAcc + zAcc * zAcc);
xVel = xVel + (xAcc * interval);
yVel = yVel + (yAcc * interval);
zVel = zVel + (zAcc * interval);
speed = Math.Sqrt(xVel * xVel + zVel * zVel);
pXPos = xPos;
pYPos = yPos;
pZPos = zPos;
xPos = xPos + xVel * interval;
yPos = yPos + yVel * interval;
zPos = zPos + zVel * interval;
distance = Math.Sqrt(xPos * xPos + zPos * zPos);
sw.WriteLine(i + "\t" + xPos + "\t" + yPos + "\t" + zPos + "\t" + distance + "\t" + xVel + "\t" + yVel + "\t" + zVel + "\t" + speed + "\t" + xAcc + "\t" + yAcc + "\t" + zAcc + "\t" + mAcc);
}
sw.Close();
}
private void Level1()
{
StreamWriter writer = File.CreateText("Level1.txt");
CalcAll(writer, 0);
writer.Close();
}
private void Level2(double dragCoe)
{
StreamWriter writer = File.CreateText("Level2.txt");
CalcAll(writer, dragCoe);
writer.Close();
}
static void Main(string[] args)
{
Program run = new Program();
//run.Level1();
run.Level2(0.3);
}
}
}
这是前几个输出的图像以及水平与垂直的图表:
Here is an image of the first few outputs and a graph of the horizontal vs the vertical:
推荐答案
我用来计算阻力的公式是使用向量:
The formula I use to calculate drag is using vectors:
Fd = -.5*rho*|V|.*V*Cd*A;
或:
Vt = sqrt((2*m*g)/(Cd*A*rho));
y = Vt^2/(2*g)*ln((V0^2+Vt^2)/((V^2*Vt^2));
x = (Vt^2/g)*ln((Vt^2+g*U0*t)/Vt^2);
哪里,
Vt 是终端速度
g 是重力
m 是质量
U0 是初始 x 速度 (initialSpeed*cos(launchAngle))
U0 is the initial x velocity (initialSpeed*cos(launchAngle))
V0 为初始垂直速度
rho 是空气密度
V 是速度矢量
|V|是速度矢量的绝对值
|V| is the absolute value of the velocity vector
Cd 是阻力系数(球体为 0.47)
Cd is the drag coefficient (0.47 for spheres)
A是截面积(球体半径^2*pi)
A is the cross sectional area (radius of sphere^2*pi)
请记住,Fd 是一种力量:
acceleration = (Force of gravity + force of drag )/ mass
因为:
F = ma
a = F/m
Fnet = Fd+Fg;
a = Fnet/m
这篇关于如何计算二维弹丸运动中的空气阻力 - C#中的计算科学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!