C ++中的Runge Kutta(RK4)二阶DE错误代码 [英] Runge Kutta (RK4) 2nd order DE in C++ ERROR CODE

查看:143
本文介绍了C ++中的Runge Kutta(RK4)二阶DE错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am receiving an error that I have never recognized before. For some background as to what this code is, I am writing a algorithm with Runge-Kutta to solve a second order differential equation (the angle of a pendulum in relation to time).

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

Even as I am typing this I already know there are probably many errors in this. This is part of my final project in my first ever coding course. Any help I can get, in the simplest language possible, could really help!

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



这是我的代码



Here is my code

#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. 您不能使用变量 time ,因为它不是宣布。请使用 timepassed

  1. You cannot use the variable time because it is not declared. Use timepassed instead.

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

You can call function only within other functions. You cannot initialize a variable with a call to a run-time function if you are not in a function. Move these lines similar to double v = dxdt(theta1, timepassed); inside the main.

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

Do not initialize variable at global scope (it is allowed but I would discourage it because of ordering issues).

这篇关于C ++中的Runge Kutta(RK4)二阶DE错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆