我收到一个以前从未认识到的错误。对于这段代码的背景知识,我正在用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);
}