C++ 创建一个C++;使用Euler';s法

C++ 创建一个C++;使用Euler';s法,c++,differential-equations,C++,Differential Equations,我试图计算由方程描述的速度的时间历程: dV/dt=g− (C_d/m)*V^2。g=9.81,m=1.0,C_d=1.5。 要做到这一点,我需要创建一个程序在C++中使用欧拉显式方法来数值求解方程。我试图用delta_t=0.05、0.1和0.2秒这三种不同的步长来计算从t=0到t=1秒的速度。然后你应该显示你对解析解的误差百分比:V(t)=sqrt((m*g)/C\d)*tanh(sqrt((g*C\d)/m)*t. 我的问题是我不知道如何在不同的时间间隔内多次迭代Euler方法。到目前为止

我试图计算由方程描述的速度的时间历程: dV/dt=g− (C_d/m)*V^2。g=9.81,m=1.0,C_d=1.5。 要做到这一点,我需要创建一个程序在C++中使用欧拉显式方法来数值求解方程。我试图用delta_t=0.05、0.1和0.2秒这三种不同的步长来计算从t=0到t=1秒的速度。然后你应该显示你对解析解的误差百分比:V(t)=sqrt((m*g)/C\d)*tanh(sqrt((g*C\d)/m)*t. 我的问题是我不知道如何在不同的时间间隔内多次迭代Euler方法。到目前为止,我已经解出了解析方程,但我不确定从这里可以走到哪里。如果有人能帮我指出正确的方向,我将不胜感激

#include <iomanip>
#include <cmath>
#include <math.h>

using namespace std;

int main() {

    double m = 1.0;     // units in [kg] 
    double g = 9.81;    // units in [m/s^2]
    double C_d = 1.5;   // units in [kg/m]  
    double t;           // units in [s]
    double v;           // units in [m/s]


    cout << "The velocity will be examined from the time t = 0 to t = 1 seconds." << endl;
    cout << "Please select either 0.05, 0.1, or 0.2 to be the time interval:" << endl;
    cin >> t;
    cout << "You have chosen the time interval of: " << t << " seconds." << endl;


    v = sqrt((m * g) / C_d) * tanh(sqrt((g * C_d) / m) * t);

    cout << "The velecity at a time of "<< t << " seconds is equal to: " << v << " m/s." << endl;


    return 0;

} ```
#包括
#包括
#包括
使用名称空间std;
int main(){
双m=1.0;//单位[kg]
双g=9.81;//单位为[m/s^2]
双C_d=1.5;//单位[kg/m]
双t;//单位为[s]
双v;//单位为[m/s]

cout如果您想以A的增量在t上迭代,用每个t计算公式的结果,您将编写一个for循环

#include <iostream>

int main()
{
    double m = 1.0;     // units in [kg] 
    double g = 9.81;    // units in [m/s^2]
    double C_d = 1.5;   // units in [kg/m]

    std::cout << "The velocity will be examined from the time t = 0 to t = 1 seconds." << std::endl;
    std::cout << "Please select the time interval:" << std::endl;
    std::cout << "1: 0.05" << std::endl;
    std::cout << "2: 0.1" << std::endl;
    std::cout << "3: 0.2" << std::endl;

    double A = 0; // increment in for loop
    int x;
    std::cin >> x;
    switch (x) { // check what the input is equal to
        case 1: A = 0.05; break;
        case 2: A = 0.1; break;
        case 3: A = 0.2; break;
        default: std::cout << "Unknown option!" << std::endl; return 1;
    }
    std::cout << "You have chosen the time interval of: " << A << " seconds." << std::endl;

    std::cout << "Results of V(t):" << std::endl;
    // this initializes a variable t as 0, 
    //and while t is lower than or equal to 1, 
    //it will increment it by a and execute the logic within the scope of the loop.
    for (double t = 0; t < (1 + A); t += A) {
        std::cout << "at t = " << t << ": " << sqrt((m*g) / C_d) * tanh(sqrt((g*C_d) / m) * t) << std::endl;
    }

    return 0;
}
#包括
int main()
{
双m=1.0;//单位[kg]
双g=9.81;//单位为[m/s^2]
双C_d=1.5;//单位[kg/m]

std::cout我的问题是,我不知道如何在不同的时间间隔内多次迭代Euler的方法。到目前为止,我已经解决了分析方程,这就是函数的用途。编写一个函数,获取所需的变量,然后使用不同的参数多次调用它@NathanOliver我应该在int main()的外部还是内部执行此操作?该函数需要在
main
@NathanOliver之外定义,这是我的想法。我是否正确地假设我需要通过if语句迭代方程?我将结果存储在数组中,然后您可以使用for循环访问每个结果并显示其方差。