炮弹模拟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了炮弹模拟相关的知识,希望对你有一定的参考价值。

我在当前程序方面遇到麻烦。

  1. 确切的速度似乎还不错,但是在第一秒的50秒处的实验速度应该是45.144,而模拟应该花费10.21秒而不是10.18秒,我的for循环有问题吗?我刚刚开始学习C ++,所以在该主题的大多数领域我都不是很好。
/*
 Suppose a cannonball is propelled vertically into the air with a starting 
 velocity of v0. A physics
 the textbook would give the position of the ball after t seconds as:
 (1) s(t) = −0.5 · g · t^2 + v0 · t
 where g is the gravitational force of the earth, and g = 9.81 m/sec2
 .

 We will confirm equation 1 by a simulation. In our simulation, we will consider how the ball
 moves in very short time intervals ∆t. In a short time interval, the velocity v is nearly constant,
 and we can compute the distance the ball moves as ∆s = v · ∆t.
 In our program, we can set a variable deltaT = 0.01 and update the position by

 s = s + v * deltaT;

 The velocity changes constantly: it is reduced by the gravitational force of the earth. In a short
 time interval, v decreases by g · ∆t, and we must keep the velocity updated as

 v = v - g * deltaT;

 In the next iteration the new velocity is used to update the distance.
 Now run the simulation until the cannonball falls back to the earth. Set the initial velocity
 to a value between 50 m/s and 150 m/s. Update the position and velocity 100 times per second,
 but only print out the experimental position every full second, along with the value from the exact
 formula (equation 1), for comparison. At the end, print the final time t when the ball hits the
 ground.

 1. How The Program Works
 The program prompts the user for the initial velocity; if the velocity given is not between 50 and
 150 m/s (inclusive), keep asking the user until a valid value is input. Allow the user to run as many
 simulations as they want.

 2. Requirements
 • Your program should be named cannonball.cpp and compile on erdos.
 • If the user enters an invalid initial velocity, continue to prompt until a valid number is
 provided.
 • Print out the initial velocity for each simulation.
 • For each elapsed second, print out the experimental position and the exact position.
 • At the end of the simulation, print out the final (i.e., total) time t.
 • Ask the user if they wish to run another simulation.
*/

/*
 10/10/2019
 This program runs a simulation of a cannonball getting shot out of a cannon. It calcuates the ball's height at various times.
*/

#include <iostream>
using namespace std;

int main()
{
    const double deltaT = 0.01, G = 9.81;
    double v, vExact, t = 0, totT=0, s1 = 0, s2, temp;
    char choice;
    bool again = true;

    do{
        cout << "Enter an initial velocity between 50 and 150m/s." << endl;
        cin >> v;
        while (v < 50 || v > 150)
        {
            cout << "Invalid entry, try again." << endl;
            cin >> v;
        }
        cout << "Begin simulation: " << endl << "v0 = " << v << "m/s" << endl;
        if (v > 50 || v < 150)
        {
            vExact = v;
            do{
                for (int i = 0; i <100; i++)
                {
                    v = v - G * deltaT;
                    s1 = s1 + v * deltaT;
                    t += 0.01;
                    if (s1 > -.25 && s1 <= 0)
                        totT = t;
                }
                s2 = ((-0.5)*G*(t*t))+(vExact*t);
                if(s1 > 0)
                    cout << "The experimental position at " << t << " seconds  is " << s1 << "m" << endl;
                if(s2 > 0)
                    cout << "The exact (formula) position at " << t << " seconds is " << s2 << "m" << endl;
                temp = s1;
            } while (temp > 0 && s2 > 0);
            cout << "Total time = " << totT << endl;
            cout << "Run another simulation? [Y]es or [N]o" << endl;
            cin >> choice;
            switch (choice)
            {
                case 'Y':
                    again = true;
                    t = 0;
                    s1 = 0;
                    break;
                case 'N':
                    again = false;
                    break;
                default:
                    again = false;
                    break;
            }
       }
    }while (again);

    return 0;
}
答案

您的代码是正确的人。只是某些行的位置错误。

确保您具有以下两个顺序:

s1 = s1 + v * deltaT; 
v = v - G * deltaT;

还有:

//if (s1 >(-0.25) && s1 <= 0)

更改上面的一行:

if (s1 >(-0.25) )
    totT = t;
另一答案
亚当,我有同样的问题。我的作业要在一个小时内完成。你知道了吗?

以上是关于炮弹模拟的主要内容,如果未能解决你的问题,请参考以下文章

Java基础五

java之方法和数组

java之方法和数组

java之方法和数组

Java基础Day5

AC日记——导弹拦截 洛谷 P1020 (dp+模拟)