C ++错误代码中的Runge Kutta(RK4)二阶DE

时间:2019-11-19 12:49:58

标签: c++ differential-equations

我收到一个以前从未认识到的错误。对于这段代码的背景知识,我正在用Runge-Kutta编写一种算法来求解二阶微分方程(摆相对于时间的角度)。

即使我键入此内容,我也已经知道可能存在许多错误。这是我有史以来第一个编码课程的最终项目的一部分。我能以最简单的语言获得的任何帮助都会真正地帮助您!

这是错误代码

second_order.cpp: In function ‘double dxdt(double, double)’:
second_order.cpp:16:17: error: invalid operands of types ‘double’ and ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ to binary ‘operator/’
    return theta/time;
                 ^

second_order.cpp: In function ‘int main()’:
second_order.cpp:51:22: error: cannot convert ‘time_t (*)(time_t*)throw () {aka long int (*)(long int*)throw ()}’ to ‘double’ for argument ‘1’ to ‘double dxdt(double, double)’
  kx1=dt*dxdt(time,x,v);
                      ^

second_order.cpp:52:28: error: cannot convert ‘time_t (*)(time_t*)throw () {aka long int (*)(long int*)throw ()}’ to ‘double’ for argument ‘1’ to ‘double dvdt(double, double, double, double)’
  kv1=dt*dvdt(time,x,v,coeff);
                            ^

second_order.cpp:53:22: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
  kx2=dt*dxdt(time+dt/2,x+kx1/2,v+kv1/2);
                      ^

second_order.cpp:54:22: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
  kv2=dt*dvdt(time+dt/2,x+kx1/2,v+kv1/2);
                      ^

second_order.cpp:55:22: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
  kx3=dt*dxdt(time+dt/2,x+kx2/2,v+kv2/2);
                      ^

second_order.cpp:56:22: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
  kv3=dt*dvdt(time+dt/2,x+kx2/2,v+kv2/2);
                      ^

second_order.cpp:57:19: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
  kx4=dt*dxdt(time+dt,x+kx3,v+kv3);
                   ^

second_order.cpp:58:19: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
  kv4=dt*dvdt(time+dt,x+kx3,v+kv3);
                   ^

make: *** [second_order.o] Error 1

这是我的代码

#include <iostream>
#include <cmath>

//dvdt=-(g/L)*sin(theta)
//v=dxdt

double theta1;
double h;
double L;
double g=9.8;
double coeff=-1*(g/L);
double timepassed;

double dxdt( double timepassed, double theta )
    {
        return theta/time;
    }
double v = dxdt(theta1, timepassed);

double x = theta1;

double dvdt( double theta, double coeff, double x, double v)
    {
        return coeff*sin(x);
    }
double d2xdt2 = dvdt(timepassed, theta1, v, coeff);

int main(){
   double theta;
   double theta1;
   double h;
   double L;
   double timepassed;
   double time1;

std::cout << "Please input initial angle (in decimal radians), length of the pendulum and the time desired. Click ENTER key after each value."<<"\n";
std::cin >> theta1;
std::cin >> L;  
std::cin >> timepassed;

double g=9.8;
double coeff=-1*(g/L);

double kx1,kv1;
double kx2, kv2;
double kx3, kv3;
double kx4, kv4;
double dt;
double x = theta1;

kx1=dt*dxdt(time,x,v);
kv1=dt*dvdt(time,x,v,coeff);
kx2=dt*dxdt(time+dt/2,x+kx1/2,v+kv1/2);
kv2=dt*dvdt(time+dt/2,x+kx1/2,v+kv1/2);
kx3=dt*dxdt(time+dt/2,x+kx2/2,v+kv2/2);
kv3=dt*dvdt(time+dt/2,x+kx2/2,v+kv2/2);
kx4=dt*dxdt(time+dt,x+kx3,v+kv3);
kv4=dt*dvdt(time+dt,x+kx3,v+kv3);
x = x + (1.0/6.0)*(kx1 + 2*kx2 + 2*kx3 + kx4);
v = v + (1.0/6.0)*(kx1 + 2*kv2 + 2*kv3 + kv4);
}

1 个答案:

答案 0 :(得分:3)

您的代码有几个问题:

  1. 由于未声明变量time,因此无法使用。请改用timepassed

  2. 您只能在其他函数中调用函数。如果您不在函数中,则无法通过调用运行时函数来初始化变量。将这些行类似于double v = dxdt(theta1, timepassed);内的main

  3. 请勿在全局范围内初始化变量(允许使用,但由于订购问题,我不鼓励使用此变量)。

  4. 在使用变量之前先对其进行初始化,否则会得到UB
  5. 您在不同的范围内声明了具有相同名称的变量,例如Lh。在L中使用main时,在全局范围内声明的L将为shadowed